00001 00026 #ifndef FIXED_HPP_ 00027 #define FIXED_HPP_ 00028 00029 #include <istream> 00030 #include <ostream> 00031 #include <string> 00032 00037 class fixed 00038 { 00039 public: 00040 typedef int value_type; 00041 00042 static int const places = 4; 00043 static value_type const places10 = 10000; 00044 00046 fixed() : value_(0) {} 00047 00051 fixed(value_type integer, value_type fraction); 00052 00054 fixed(value_type integer); 00055 00057 fixed(double value) 00058 : value_(static_cast<value_type>(value * places10 + (value < 0 ? -0.5 : 0.5))) 00059 {} 00060 00063 std::string as_string() const; 00068 bool read(std::istream& strm); 00070 double as_long_double() const { return static_cast<long double>(value()) / places10; } 00072 double as_double() const { return static_cast<double>(value()) / places10; } 00074 float as_float() const { return static_cast<float>(value()) / places10; } 00078 value_type round() const; 00079 00081 value_type integer() const { return value() / places10; } 00083 value_type fraction() const; 00084 00086 fixed& operator+=(fixed f); 00088 fixed& operator-=(fixed f); 00090 fixed& operator*=(fixed f); 00092 fixed& operator/=(fixed f); 00093 00095 void negate(); 00096 00098 fixed& operator++(); 00100 fixed operator++(int); 00102 fixed& operator--(); 00104 fixed operator--(int); 00105 00107 value_type value() const { return value_; } 00108 private: 00110 value_type reduce(value_type frac); 00111 value_type value_; 00112 }; 00113 00115 std::istream& operator>>(std::istream& strm, fixed& f); 00117 std::ostream& operator<<(std::ostream& strm, fixed f); 00118 00120 fixed operator+(fixed a, fixed b); 00122 fixed operator-(fixed a, fixed b); 00124 fixed operator*(fixed a, fixed b); 00126 fixed operator/(fixed a, fixed b); 00128 fixed operator-(fixed a); 00129 00131 bool operator==(fixed a, fixed b); 00133 bool operator!=(fixed a, fixed b); 00135 bool operator<(fixed a, fixed b); 00137 bool operator>(fixed a, fixed b); 00139 bool operator<=(fixed a, fixed b); 00141 bool operator>=(fixed a, fixed b); 00142 00143 #endif