gpt4 book ai didi

c++ - 函数 bool isnan( … ) 在 C++ 中的定点实现

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:54:55 25 4
gpt4 key购买 nike

我正在用 C++ 进行定点实现,我正在尝试定义“非数字”并支持函数 bool isnan( … ) 如果数字不是数字则返回 true,否则返回 false。

有人能给我一些关于如何定义“非数字”并在我的定点数学实现中实现函数 bool isnan( … ) 的想法吗。

我读过有关 C++ Nan 的资料,但我无法获得有关如何手动定义和创建函数 nan() 以在定点实现中使用它的任何来源或引用。

有人可以告诉我如何进行或提供一些引用以进行吗?

谢谢

更新定点 header

#ifndef __fixed_point_header_h__
#define __fixed_point_header_h__
#include <boost/operators.hpp>
#include <boost/assert.hpp>
#endif
namespace fp {
template<typename FP, unsigned char I, unsigned char F>
class fixed_point: boost::ordered_field_operators<fp::fixed_point<FP, I, F> >
{
//compute the power of 2 at compile time by template recursion
template<int P,typename T = void>
struct power2
{
static const long long value = 2 * power2<P-1,T>::value;
};

template <typename P>
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<typename T> fixed_point(T value) : fixed_((FP)value << F)
{
BOOST_CONCEPT_ASSERT((boost::Integer<T>));
}
//floating point to fixed point
fixed_point(float value) :fixed_((FP)(value * power2<F>::value)){ }
fixed_point(double value) : fixed_((FP)(value * power2<F>::value)) { }
fixed_point(long double value) : fixed_((FP)(value * power2<F>::value)) { }
/// Copy constructor,explicit definition
fixed_point(fixed_point<FP, I, F> const& rhs): fixed_(rhs.fixed_)
{ }

// copy-and-swap idiom.

fp::fixed_point<FP, I, F> & operator =(fp::fixed_point<FP, I, F> const& rhs)
{
fp::fixed_point<FP, I, F> 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<FP, I, F> & rhs)
{
std::swap(fixed_, rhs.fixed_);
}
bool operator <(
/// Right hand side.
fp::fixed_point<FP, I, F> const& rhs) const
{
return fixed_ < rhs.fixed_; //return by value
}

bool operator ==(
/// Right hand side.
fp::fixed_point<FP, I, F> const& rhs) const
{
return fixed_ == rhs.fixed_; //return by value
}
// Addition.
fp::fixed_point<FP, I, F> & operator +=(fp::fixed_point<FP, I, F> const& summation)
{
fixed_ += summation.fixed_;
return *this; //! /return A reference to this object.
}
/// Subtraction.
fp::fixed_point<FP, I, F> & operator -=(fp::fixed_point<FP, I, F> const& subtraction)
{
fixed_ -= subtraction.fixed_;
return *this; // return A reference to this object.
}
// Multiplication.
fp::fixed_point<FP, I, F> & operator *=(fp::fixed_point<FP, I, F> const& factor)
{
fixed_ = ( fixed_ * (factor.fixed_ >> F) ) +
( ( fixed_ * (factor.fixed_ & (power2<F>::value-1) ) ) >> F );
return *this; //return A reference to this object.
}
/// Division.
fp::fixed_point<FP, I, F> & operator /=(fp::fixed_point<FP, I, F> const& divisor)
{
fp::fixed_point<FP, I, F> 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__

最佳答案

通常定点数学用于没有 FPU 的嵌入式硬件。大多数情况下,这种硬件也缺乏程序或数据空间或/和处理能力。

你确定你需要 NAN、INF 或其他什么的通用支持吗?在可以产生这些值的操作上将其作为单独的标志显式实现可能就足够了。

那么当你使用定点算法时,你必须非常了解你的数据以避免乘法或除法上溢或下溢。因此,您的算法必须以一种无论如何都避免这些特殊条件的方式编写。

除此之外,即使使用 double:一旦你的算法中有这些特殊值之一,它们就会像病毒一样传播,结果毫无用处。

作为结论:在我的意见中,在您的定点类中显式实现此方法会严重浪费处理能力,因为您必须向每个定点操作添加条件。条件语句对 DSP 或 µC 的深度 CPU 流水线来说是毒药。

关于c++ - 函数 bool isnan( … ) 在 C++ 中的定点实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16361323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com