C++ 定点实现中的浮点异常错误
helo all i实现了一个头文件,用于执行定点算术运算,如下所示,它执行乘法和除法运算C++ 定点实现中的浮点异常错误,c++,types,floating-point,fixed-point,C++,Types,Floating Point,Fixed Point,helo all i实现了一个头文件,用于执行定点算术运算,如下所示,它执行乘法和除法运算 #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 typ
#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__
\ifndef\uu固定点\uu标题\uh__
#定义固定点标题__
#包括
#包括
命名空间fp{
//FP固定点(基)类型,应为整数类型
//I整数部分位计数
//F小数部分位计数
模板
类固定点:boost::有序字段\u运算符
{
//由于定点类在浮点转换的几个部分中需要2到P的幂,因此使用模板元编程的模板递归在编译时本身计算2到F的幂。
模板
结构power2
{
静态常量long long value=2*power2::value;
};
模板
结构power2
{
静态常量长值=1;
};
//正在初始化构造函数。
//!此构造函数接受FP类型的值,并用它初始化固定点的//内部表示形式。
固定点(FP值,bool):固定点(值){}//初始值设定项列表
公众:
typedef FP base_type;///此定点cass的定点基本类型。
static const unsigned char integer\u bit\u count=I;///整数部分位计数。
静态常量无符号字符分数位计数=F;///分数部分位计数。
修复了_point(){}///默认构造函数。
//构造函数的转换。
//整数到不动点
模板固定点(T值):固定点(FP值>F))+
((fixed.*(factor.fixed.&(power2::value-1))>>F);
return*this;//返回对此对象的引用。
}
///分部。
fp::固定点和运算符/=(
///除法的除数。
fp::定点常数和除数)
{
fixed_u=((fixed_)p>这肯定会导致被零除:
fixed_ = ( (fixed_)<< F / divisor.fixed_ )+
( ( fixed_ / (divisor.fixed_ & (power2<F>::value-1) ) )<< F );
return *this; //return A reference to this object.
fixed_=((fixed_uu)你能不能在调试器中一步一步地检查相关的代码,看看问题出在哪里?我可以看到除法过程中有些东西被零除了,所以我得到了浮点异常error@PaulR我是否在正确的位置进行了除法转换?在您的体系结构中,有可能是“除以零”实际上被视为与“浮点异常”相同。不要被零除…@MatsPetersson但在测试文件中,您可以看到,我刚刚进行了3.0/3.0除法,但仍然得到浮点异常错误
fixed_ = ( (fixed_)<< F / divisor.fixed_ )+
( ( fixed_ / (divisor.fixed_ & (power2<F>::value-1) ) )<< F );
return *this; //return A reference to this object.