00001 #ifndef __FIXED_H__
00002 #define __FIXED_H__
00003
00004 #include <stdlib.h>
00005 #include <iostream>
00006
00007 #define _GCC_TEMPLATE_BUG_ 1
00008
00009 template <unsigned int SH> class fixed {
00010 public:
00011 inline fixed ( int i = 0 ) : m_f ( i2f( i ) ) { }
00012 inline fixed ( double d ) : m_f ( d2f( d )) { }
00013 inline fixed ( const fixed &f ) : m_f ( f. m_f ) { }
00014
00015 inline operator bool ( ) const { return m_f != 0; }
00016 inline operator double ( ) const { return f2d( m_f ); }
00017 inline operator float ( ) const { return (float) f2d( m_f ); }
00018 inline operator int ( ) const { return (int) f2i( m_f ); }
00019
00020 inline fixed &operator = ( const fixed &f ) { m_f = f. m_f; return *this; }
00021 inline fixed &operator = ( double d ) { m_f = d2f( d ); return *this; }
00022 inline fixed &operator = ( float f ) { m_f = d2f( f ); return *this; }
00023 inline fixed &operator = ( int i ) { m_f = i2f( i ); return *this; }
00024
00025 inline fixed &operator += ( const fixed &f ) { m_f += f. m_f; return *this; }
00026 inline fixed &operator -= ( const fixed &f ) { m_f -= f. m_f; return *this; }
00027 inline fixed &operator *= ( const fixed &f ) { m_f = mul ( m_f, f. m_f ); return *this; }
00028 inline fixed &operator /= ( const fixed &f ) { m_f = div ( m_f, f. m_f ); return *this; }
00029
00030 inline fixed &operator += ( int i ) { m_f += i2f( i ); return *this; }
00031 inline fixed &operator -= ( int i ) { m_f -= i2f( i ); return *this; }
00032 inline fixed &operator *= ( int i ) { m_f *= i; return *this; }
00033 inline fixed &operator /= ( int i ) { m_f /= i; return *this; }
00034
00035 inline fixed &operator += ( double d ) { m_f += d2f( d ); return *this; }
00036 inline fixed &operator -= ( double d ) { m_f -= d2f( d ); return *this; }
00037 inline fixed &operator *= ( double d ) { m_f = mul ( m_f, d2f( d )); return *this; }
00038 inline fixed &operator /= ( double d ) { m_f = div ( m_f, d2f( d )); return *this; }
00039
00040 inline fixed operator - ( ) const { return fixed ( -m_f, true ); }
00041
00042 inline fixed operator + ( const fixed &f ) const { return fixed ( m_f + f. m_f, true ); }
00043 inline fixed operator - ( const fixed &f ) const { return fixed ( m_f - f. m_f, true ); }
00044 inline fixed operator * ( const fixed &f ) const { return fixed ( mul ( m_f, f. m_f ), true ); }
00045 inline fixed operator / ( const fixed &f ) const { return fixed ( div ( m_f, f. m_f ), true ); }
00046
00047 inline fixed operator + ( double d ) const { return fixed ( m_f + d2f( d ), true ); }
00048 inline fixed operator - ( double d ) const { return fixed ( m_f - d2f( d ), true ); }
00049 inline fixed operator * ( double d ) const { return fixed ( mul ( m_f, d2f( d )), true ); }
00050 inline fixed operator / ( double d ) const { return fixed ( div ( m_f, d2f( d )), true ); }
00051
00052 inline fixed operator + ( int i ) const { return fixed ( m_f + i2f( i ), true ); }
00053 inline fixed operator - ( int i ) const { return fixed ( m_f - i2f( i ), true ); }
00054 inline fixed operator * ( int i ) const { return fixed ( m_f * i, true ); }
00055 inline fixed operator / ( int i ) const { return fixed ( m_f / i, true ); }
00056
00057 inline bool operator < ( const fixed &f ) const { return m_f < f. m_f; }
00058 inline bool operator > ( const fixed &f ) const { return m_f > f. m_f; }
00059 inline bool operator <= ( const fixed &f ) const { return m_f <= f. m_f; }
00060 inline bool operator >= ( const fixed &f ) const { return m_f >= f. m_f; }
00061 inline bool operator == ( const fixed &f ) const { return m_f == f. m_f; }
00062 inline bool operator != ( const fixed &f ) const { return m_f != f. m_f; }
00063
00064 inline bool operator < ( double d ) const { return m_f < d2f( d ); }
00065 inline bool operator > ( double d ) const { return m_f > d2f( d ); }
00066 inline bool operator <= ( double d ) const { return m_f <= d2f( d ); }
00067 inline bool operator >= ( double d ) const { return m_f >= d2f( d ); }
00068 inline bool operator == ( double d ) const { return m_f == d2f( d ); }
00069 inline bool operator != ( double d ) const { return m_f != d2f( d ); }
00070
00071 inline bool operator < ( int i ) const { return m_f < i2f( i ); }
00072 inline bool operator > ( int i ) const { return m_f > i2f( i ); }
00073 inline bool operator <= ( int i ) const { return m_f <= i2f( i ); }
00074 inline bool operator >= ( int i ) const { return m_f >= i2f( i ); }
00075 inline bool operator == ( int i ) const { return m_f == i2f( i ); }
00076 inline bool operator != ( int i ) const { return m_f != i2f( i ); }
00077
00078 #if _GCC_TEMPLATE_BUG_
00079 public:
00080 #else
00081 private:
00082 #endif
00083 typedef int fix_t;
00084
00085 inline static double f2d ( fix_t f ) { return ((double) f ) / ((double) ( 1 << SH )); }
00086 inline static fix_t d2f ( double d ) { return (fix_t) ( d * ((double) ( 1 << SH ))); }
00087
00088 inline static int f2i ( fix_t f ) { return (int) ( f >> SH ); }
00089 inline static fix_t i2f ( int i ) { return (fix_t) ( i << SH ); }
00090
00091 inline static fix_t mul ( fix_t m1, fix_t m2 ) { return (fix_t) ((((long long int) m1 ) * m2 ) >> SH ); }
00092 inline static fix_t div ( fix_t d1, fix_t d2 ) { return (fix_t) ((((long long int) d1 ) << SH ) / d2 ); }
00093
00094 fixed ( fix_t f, bool ) : m_f ( f ) { }
00095
00096
00097 fix_t m_f;
00098
00099
00100 #if !_GCC_TEMPLATE_BUG_
00101 friend fixed operator + <> ( int i, const fixed &f );
00102 friend fixed operator - <> ( int i, const fixed &f );
00103 friend fixed operator * <> ( int i, const fixed &f );
00104 friend fixed operator / <> ( int i, const fixed &f );
00105
00106 friend fixed operator + <> ( double d, const fixed &f );
00107 friend fixed operator - <> ( double d, const fixed &f );
00108 friend fixed operator * <> ( double d, const fixed &f );
00109 friend fixed &operator / <> ( double d, const fixed<SH> &f );
00110
00111 friend bool operator < <> ( double d, const fixed &f );
00112 friend bool operator > <> ( double d, const fixed &f );
00113 friend bool operator <= <> ( double d, const fixed &f );
00114 friend bool operator >= <> ( double d, const fixed &f );
00115 friend bool operator == <> ( double d, const fixed &f );
00116 friend bool operator != <> ( double d, const fixed &f );
00117
00118 friend bool operator < <> ( int i, const fixed &f );
00119 friend bool operator > <> ( int i, const fixed &f );
00120 friend bool operator <= <> ( int i, const fixed &f );
00121 friend bool operator >= <> ( int i, const fixed &f );
00122 friend bool operator == <> ( int i, const fixed &f );
00123 friend bool operator != <> ( int i, const fixed &f );
00124
00125 friend long int lrint ( const fixed &f );
00126 friend fixed sqrt ( const fixed &f );
00127 friend fixed fabs ( const fixed &f );
00128 #endif
00129 };
00130
00131
00132 template <unsigned int SH> inline fixed<SH> operator + ( int i, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::i2f( i ) + f. m_f, true ); }
00133 template <unsigned int SH> inline fixed<SH> operator - ( int i, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::i2f( i ) - f. m_f, true ); }
00134 template <unsigned int SH> inline fixed<SH> operator * ( int i, const fixed<SH> &f ) { return fixed<SH> ( i * f. m_f, true ); }
00135 template <unsigned int SH> inline fixed<SH> operator / ( int i, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::div ( fixed<SH>::i2f( i ), f. m_f ), true ); }
00136
00137
00138 template <unsigned int SH> inline fixed<SH> operator + ( double d, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::d2f( d ) + f. m_f, true ); }
00139 template <unsigned int SH> inline fixed<SH> operator - ( double d, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::d2f( d ) - f. m_f, true ); }
00140 template <unsigned int SH> inline fixed<SH> operator * ( double d, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::mul ( fixed<SH>::d2f( d ), f. m_f ), true ); }
00141 template <unsigned int SH> inline fixed<SH> operator / ( double d, const fixed<SH> &f ) { return fixed<SH> ( fixed<SH>::mul ( fixed<SH>::d2f( d ), f. m_f ), true ); }
00142
00143 template <unsigned int SH> inline bool operator < ( double d, const fixed<SH> &f ) { return fixed<SH>::d2f( d ) < f. m_f; }
00144 template <unsigned int SH> inline bool operator > ( double d, const fixed<SH> &f ) { return fixed<SH>::d2f( d ) > f. m_f; }
00145 template <unsigned int SH> inline bool operator <= ( double d, const fixed<SH> &f ) { return fixed<SH>::d2f( d ) <= f. m_f; }
00146 template <unsigned int SH> inline bool operator >= ( double d, const fixed<SH> &f ) { return fixed<SH>::d2f( d ) >= f. m_f; }
00147 template <unsigned int SH> inline bool operator == ( double d, const fixed<SH> &f ) { return fixed<SH>::d2f( d ) == f. m_f; }
00148 template <unsigned int SH> inline bool operator != ( double d, const fixed<SH> &f ) { return fixed<SH>::d2f( d ) != f. m_f; }
00149
00150 template <unsigned int SH> inline bool operator < ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) < f. m_f; }
00151 template <unsigned int SH> inline bool operator > ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) > f. m_f; }
00152 template <unsigned int SH> inline bool operator <= ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) <= f. m_f; }
00153 template <unsigned int SH> inline bool operator >= ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) >= f. m_f; }
00154 template <unsigned int SH> inline bool operator == ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) == f. m_f; }
00155 template <unsigned int SH> inline bool operator != ( int i, const fixed<SH> &f ) { return fixed<SH>::i2f( i ) != f. m_f; }
00156
00157 template <unsigned int SH> inline long int lrint ( const fixed<SH> &f )
00158 {
00159 return fixed<SH>::f2i (( f. m_f < 0 ) ? f. m_f - ( 1 << ( SH - 1 )) : f. m_f + ( 1 << ( SH - 1 )));
00160 }
00161
00162 template <unsigned int SH> inline fixed<SH> fabs ( const fixed<SH> &f )
00163 {
00164 return ( f. m_f < 0 ) ? fixed<SH> ( -f. m_f, true ) : f;
00165 }
00166
00167
00168 template <unsigned int SH> inline fixed<SH> sqrt ( const fixed<SH> &f )
00169 {
00170 if ( f. m_f <= 0 )
00171 return fixed<SH> ( 0, true );
00172
00173 typename fixed<SH>::fix_t a0 = 0;
00174 typename fixed<SH>::fix_t a1 = f. m_f;
00175
00176 do {
00177 a0 = a1;
00178 a1 = ( a0 + fixed<SH>::div ( f. m_f, a0 )) >> 1;
00179 } while ( abs ( fixed<SH>::div ( a1 - a0, a1 )) > 1 );
00180
00181 return fixed<SH> ( a1, true );
00182 }
00183
00184 #if 0 // no std::ostream needed in OPIE
00185 template <unsigned int SH> inline std::ostream &operator << ( std::ostream &o, const fixed<SH> &f )
00186 {
00187 o << double( f );
00188 return o;
00189 }
00190 #endif
00191
00192 #endif