我在c中进行定点实现,我正在尝试定义“not-a-number”并支持函数bool isnan(…),如果数字不是-a,则返回true,否则返回false.
有人可以给我一些如何定义“非数字”的想法,并在我的定点数学实现中实现一个函数bool isnan(…).
我已经阅读了有关C Nan的内容,但我无法获得有关如何手动定义和创建函数nan()的任何来源或参考,以便在定点实现中使用它.
有人可以告诉我如何继续或提供一些参考继续吗?
谢谢
更新固定点标头
#ifndef __fixed_point_header_h__
#define __fixed_point_header_h__
#include
#include
#endif
namespace fp {
template
class fixed_point: boost::ordered_field_operators<:fixed_point i f> >
{
//compute the power of 2 at compile time by template recursion
template
struct power2
{
static const long long value = 2 * power2::value;
};
template
struct power2<0, P>
{
static const long long value = 1;
};
fixed_point(FP value,bool): fixed_(value){ } // initializer list
public:
typedef FP base_type; /// fixed point base type of this fixed_point class.
static const unsigned char integer_bit_count = I; /// integer part bit count.
static const unsigned char fractional_bit_count = F; /// fractional part bit count.
fixed_point(){ } /// Default constructor.
//Integer to Fixed point
template fixed_point(T value) : fixed_((FP)value << F)
{
BOOST_CONCEPT_ASSERT((boost::Integer));
}
//floating point to fixed point
fixed_point(float value) :fixed_((FP)(value * power2::value)){ }
fixed_point(double value) : fixed_((FP)(value * power2::value)) { }
fixed_point(long double value) : fixed_((FP)(value * power2::value)) { }
/// Copy constructor,explicit definition
fixed_point(fixed_point const& rhs): fixed_(rhs.fixed_)
{ }
// copy-and-swap idiom.
fp::fixed_point & operator =(fp::fixed_point const& rhs)
{
fp::fixed_point temp(rhs); // First, make a copy of the right-hand side
swap(temp); //swapping the copied(old) data the new data.
return *this; //return by reference
}
/// Exchanges the elements of two fixed_point objects.
void swap(fp::fixed_point & rhs)
{
std::swap(fixed_, rhs.fixed_);
}
bool operator
/// Right hand side.
fp::fixed_point const& rhs) const
{
return fixed_ < rhs.fixed_; //return by value
}
bool operator ==(
/// Right hand side.
fp::fixed_point const& rhs) const
{
return fixed_ == rhs.fixed_; //return by value
}
// Addition.
fp::fixed_point & operator +=(fp::fixed_point const& summation)
{
fixed_ += summation.fixed_;
return *this; //! /return A reference to this object.
}
/// Subtraction.
fp::fixed_point & operator -=(fp::fixed_point const& subtraction)
{
fixed_ -= subtraction.fixed_;
return *this; // return A reference to this object.
}
// Multiplication.
fp::fixed_point & operator *=(fp::fixed_point const& factor)
{
fixed_ = ( fixed_ * (factor.fixed_ >> F) ) +
( ( fixed_ * (factor.fixed_ & (power2::value-1) ) ) >> F );
return *this; //return A reference to this object.
}
/// Division.
fp::fixed_point & operator /=(fp::fixed_point const& divisor)
{
fp::fixed_point fp_z=1;
fp_z.fixed_ = ( (fp_z.fixed_) << (F-2) ) / ( divisor.fixed_ >> (2) );
*this *= fp_z;
return *this; //return A reference to this object
}
private:
/// The value in fixed point format.
FP fixed_;
};
} // namespace fmpl
#endif
#endif // __fixed_point_header__