2013-04-17 3 views
0

helo 모두 아래에 표시된 고정 소수점 arthimethic 연산을 수행하기위한 헤더 파일을 구현했지만 곱셈 및 나누기 연산을 수행합니다.고정 소수점 구현시 부동 소수점 예외 오류

#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__ 

는 곱셈 난 (예를 들어, 2 개 개의 8 비트 값을 곱하면, 16 비트 결과를 생성한다), 생성 된 고정 소수점의 유형을 촉진. 잘 작동하고 있습니다.

하지만 난 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); 
    } 

내가 항상 부동 소수점 예외 오류를 geeting하고 다음 테스트 파일로 분할을 수행 할 때, 몇 가지를 확인하시기 바랍니다 수 있으며, 일이 뭐죠 참조하십시오. 확실히이 잠재적으로 0으로 나눌 수

+0

문제가 무엇인지 확인하기 위해 디버거의 관련 코드를 단계별로 실행할 수 없습니까? –

+0

나눗셈 중에 무언가가 0으로 나뉘어서 부동 소수점 예외 오류가 발생하는 것을 볼 수 있습니다 – Rd7

+0

@PaulR 나눗셈을위한 올바른 위치에서 이동을 했습니까? – Rd7

답변

0

선도 : 제로

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

divisor.fixed_ & (power2<F>::value-1) 경우에, 당신은 0으로 나누기를 얻을.

나는 그것이 옳다는 것을 확신하지는 못했지만 실제로는 고정 소수점 나누기를 고려하지 않았으므로 그 점을 이해하도록 남겨 두겠습니다.