简体   繁体   English

定点实现中的浮点异常错误

[英]floating point exception error in fixed point implementation

helo all i implemented a header file for doing the fixedpoint arthimethic operations, which is shown below, it does multiplication and divison operations. 我全部实现了用于执行定点关节置换术的头文件,如下所示,它执行乘法和除法运算。

#ifndef __fixed_point_header_h__
#define __fixed_point_header_h__
#include <boost/operators.hpp>
#include <limits>
namespace fp {
    // FP The fixed point(base) type, should be an integer type 
    // I  Integer part bit count
    // F  fractional part bit count 
    template<typename FP, unsigned char I, unsigned char F = std::numeric_limits<FP>::digits - I>

class fixed_point: boost::ordered_field_operators<fp::fixed_point<FP, I, F>  > 
{

//As the fixed_point class needs 2 to the power of P in several parts for floating point conversions, template recursion using template metaprogramming is used to calculate 2 to the power of F at compile time itself.

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;
    };

// Initializing constructor.
//! This constructor takes a value of type FP and initializes the //internal representation of fixed_point<FP, I, F> with it.
    fixed_point(FP value,bool): fixed_(value){ } // initializer list

public:
typedef FP base_type; ///  fixed point base type of this fixed_point cass.
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.

//Conversion by constructors.
//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(
        /// The right hand side.
        fixed_point<FP, I, F> const& rhs)
        //: fixed_(rhs.fixed_)
    {
        fixed_ = rhs.fixed_;
    }
// Copy assignment operator.
    fp::fixed_point<FP, I, F> & operator =(fp::fixed_point<FP, I, F> const& rhs)
    {
    fp::fixed_point<FP, I, F> temp(rhs); 
    swap(temp); 
    return *this;  //return by reference
    }
    /// Exchanges the elements of two fixed_point objects.
    void swap(
        /// The right hand side.
        fp::fixed_point<FP, I, F> & rhs)
    {
        std::swap(fixed_, rhs.fixed_);
    }
/// Multiplication.
fp::fixed_point<FP, I, F> & operator *=(
        /// Factor for mutliplication.
        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 /=(
        /// Divisor for division.
        fp::fixed_point<FP, I, F> const& divisor)
    {

        fixed_ = ( (fixed_)<< F  / divisor.fixed_  )+
                 ( ( fixed_ / (divisor.fixed_ & (power2<F>::value-1) ) )<< F   ); 
        return *this;   //return A reference to this object.
    }
private:
    /// The value in fixed point format.
    FP fixed_;
 };

} // namespace fp
#endif // __fixed_point_header__

for multiplication i promoted the type of the resulting fixed point, for example (When two 8 bit numbers are multiplied, a 16 bit result is produced). 例如,对于乘法运算,我提升了所得固定点的类型(将两个8位数字相乘时,将生成16位结果)。 it is working fine. 它工作正常。

but when i do division with the following test file Test.cpp 但是当我用以下测试文件Test.cpp进行除法时

#include <stdio.h>
#include <fixed_point_header.h>
int main()
{
 fp::fixed_point<int, 15> fp1 = 3.0; // 011 0000 0000 0000 0000
 fp::fixed_point<int, 15> fp2 = 3.0; // 100 0000 0000 0000 0000
 fp::fixed_point<int, 15> fp3;

    fp3 = fp1/fp2;
    printf("divided value ==%f\n", (float)fp3 );
     }

i am always geeting float point exception error, can some please check it and see whats happening. 我一直在geeting浮点异常错误,可以一些请检查一下,看看发生了什么。

Surely this is potentially leading to divide by zero: 当然,这有可能导致被零除:

   fixed_ = ( (fixed_)<< F  / divisor.fixed_  )+
            ( ( fixed_ / (divisor.fixed_ & (power2<F>::value-1) ) )<< F   ); 
   return *this;   //return A reference to this object.

If divisor.fixed_ & (power2<F>::value-1) is zero, then you get divide by zero. 如果divisor.fixed_ & (power2<F>::value-1)为零,则除以零。

I'm also not convinced that it is correct, but I'll leave it to you to figure that out, as I haven't actually considered how you do a "fixed point" divide. 我也不相信它是正确的,但是我会留给您解决,因为我实际上还没有考虑过如何进行“定点”划分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM