首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bool isnan(…)函数的定点实现)在C++中

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

Stack Overflow用户
提问于 2013-05-03 22:29:43
回答 2查看 313关注 0票数 4

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

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

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

有人能告诉我如何继续吗?或者提供一些参考资料来继续进行?

谢谢

更新固定点头

代码语言:javascript
复制
#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__
EN

回答 2

Stack Overflow用户

发布于 2013-05-04 01:22:32

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

您确定需要NAN、INF或其他任何类型的通用支持吗?在可以生成这些值的操作上显式地将其实现为单独的标志可能就足够了。

THen,你使用定点运算,你必须非常了解你的数据,以避免乘法或除法上的溢出或下溢。因此,你的算法必须以一种避免这些特殊条件的方式来编写。

除此之外,即使是使用double:一旦你的算法中有一个特殊的值,它们就像病毒一样传播,结果是非常无用的。

As结论:在我看来,在你的fixedpoint类中显式地实现它是对处理能力的重大浪费,因为你必须在每个定点操作中添加条件。和条件语句对DSP或µC的深cpu流水线来说是毒药。

票数 3
EN

Stack Overflow用户

发布于 2013-05-03 22:41:57

你能给我们举个例子说明你所说的固定点是什么意思吗?它是作为一个类实现的吗?它是固定的字节数,还是支持8,16,32,64位数字?如何表示负值?

根据这些因素,您可以实现几种可能的不同方式。IEEE浮点数之所以能幸免于难,是因为浮点数是以一种特殊的格式编码的,允许根据位模式设置标志。在定点实现中,这可能是不可能的。但是,如果它是一个类,您可以为该类定义算术运算符,然后将结果数字设置为nan。

更新

查看代码,您似乎只是将信息填充到值中。因此,最好的方法可能是在类中设置一个isnan标志,并从适当的数学操作中设置它,然后在执行这些操作之前检查它,以便isnan propogate。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16361323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档