|
AndroMeta
2.0.0
|
00001 /* 00002 * Copyright 2009-2012 AndroMeta LLC. All rights reserved. 00003 * 00004 * AndroMeta LLC retains all intellectual property and proprietary 00005 * rights to this software and related documentation and any modifications 00006 * thereto. Any use, reproduction, disclosure, or distribution of this 00007 * software and related documentation without an express license agreement 00008 * from AndroMeta LLC is strictly prohibited. 00009 * 00010 */ 00011 00018 #ifndef ANDROMETA_M_RATIONAL_H 00019 #define ANDROMETA_M_RATIONAL_H 00020 00021 #include <cstddef> 00022 #include <ostream> 00023 00024 #include <AndroMeta/MStr.h> 00025 00026 namespace Meta{ 00027 00032 class MRational{ 00033 public: 00034 MRational(); 00035 00036 MRational(const MRational& r); 00037 00038 MRational(int numerator); 00039 00040 MRational(int64_t numerator); 00041 00042 MRational(int64_t numerator, int64_t denominator); 00043 00044 ~MRational(); 00045 00046 int64_t numerator() const; 00047 00048 int64_t denominator() const; 00049 00050 MRational& operator=(const MRational& r); 00051 00052 MRational& operator+=(const MRational& r); 00053 00054 template<typename T> 00055 MRational operator+(const T& t) const{ 00056 MRational ret(*this); 00057 return ret += MRational(t); 00058 } 00059 00060 double operator+(double d) const{ 00061 return this->toDouble() + d; 00062 } 00063 00064 MRational operator+(const MRational& r) const{ 00065 MRational ret(*this); 00066 return ret += r; 00067 } 00068 00069 MRational operator++(int){ 00070 MRational ret(*this); 00071 *this += 1; 00072 return ret; 00073 } 00074 00075 MRational& operator++(){ 00076 return *this += 1; 00077 } 00078 00079 MRational& operator-=(const MRational& r); 00080 00081 template<typename T> 00082 MRational operator-(const T& t) const{ 00083 MRational ret(*this); 00084 return ret -= MRational(t); 00085 } 00086 00087 double operator-(double d) const{ 00088 return this->toDouble() - d; 00089 } 00090 00091 MRational operator-(const MRational& r) const{ 00092 MRational ret(*this); 00093 return ret -= r; 00094 } 00095 00096 MRational operator-() const; 00097 00098 MRational operator--(int){ 00099 MRational ret(*this); 00100 *this -= 1; 00101 return ret; 00102 } 00103 00104 MRational& operator--(){ 00105 return *this -= 1; 00106 } 00107 00108 MRational& operator*=(const MRational& r); 00109 00110 template<typename T> 00111 MRational operator*(const T& t) const{ 00112 MRational ret(*this); 00113 return ret *= MRational(t); 00114 } 00115 00116 double operator*(double d) const{ 00117 return this->toDouble() * d; 00118 } 00119 00120 MRational operator*(const MRational& r) const{ 00121 MRational ret(*this); 00122 return ret *= r; 00123 } 00124 00125 MRational& operator/=(const MRational& r); 00126 00127 template<typename T> 00128 MRational operator/(const T& t) const{ 00129 MRational ret(*this); 00130 return ret /= MRational(t); 00131 } 00132 00133 double operator/(double d) const{ 00134 return this->toDouble() / d; 00135 } 00136 00137 MRational operator/(const MRational& r) const{ 00138 MRational ret(*this); 00139 return ret /= r; 00140 } 00141 00142 bool operator<(const MRational& r) const; 00143 00144 template<typename T> 00145 bool operator<(const T& t) const{ 00146 return *this < MRational(t); 00147 } 00148 00149 bool operator<(double d) const{ 00150 return this->toDouble() < d; 00151 } 00152 00153 bool operator>(const MRational& r) const; 00154 00155 template<typename T> 00156 bool operator>(const T& t) const{ 00157 return *this > MRational(t); 00158 } 00159 00160 bool operator>(double d) const{ 00161 return this->toDouble() > d; 00162 } 00163 00164 bool operator==(const MRational& r) const; 00165 00166 template<typename T> 00167 bool operator==(const T& t) const{ 00168 return *this == MRational(t); 00169 } 00170 00171 bool operator==(double d) const{ 00172 return this->toDouble() == d; 00173 } 00174 00175 bool operator<=(const MRational& r) const; 00176 00177 template<typename T> 00178 bool operator<=(const T& t) const{ 00179 return *this <= MRational(t); 00180 } 00181 00182 bool operator<=(double d) const{ 00183 return this->toDouble() <= d; 00184 } 00185 00186 bool operator>=(const MRational& r) const; 00187 00188 template<typename T> 00189 bool operator>=(const T& t) const{ 00190 return *this >= MRational(t); 00191 } 00192 00193 bool operator>=(double d) const{ 00194 return this->toDouble() >= d; 00195 } 00196 00197 bool operator!=(const MRational& r) const; 00198 00199 template<typename T> 00200 bool operator!=(const T& t) const{ 00201 return *this != MRational(t); 00202 } 00203 00204 bool operator!=(double d) const{ 00205 return this->toDouble() != d; 00206 } 00207 00212 int64_t toLong() const; 00213 00218 double toDouble() const; 00219 00220 bool toBool() const; 00221 00225 mstr toStr(int precision) const; 00226 00227 private: 00228 class MRational_* x_; 00229 }; 00230 00231 std::ostream& operator<<(std::ostream& ostr, const MRational& r); 00232 00233 inline bool operator<(double d, const MRational& r){ 00234 return d < r.toDouble(); 00235 } 00236 00237 template<typename T> 00238 inline bool operator<(const T& t, const MRational& r){ 00239 return MRational(t) < r; 00240 } 00241 00242 inline bool operator>(double d, const MRational& r){ 00243 return d > r.toDouble(); 00244 } 00245 00246 template<typename T> 00247 inline bool operator>(const T& t, const MRational& r){ 00248 return MRational(t) > r; 00249 } 00250 00251 inline bool operator<=(double d, const MRational& r){ 00252 return d <= r.toDouble(); 00253 } 00254 00255 template<typename T> 00256 inline bool operator<=(const T& t, const MRational& r){ 00257 return MRational(t) <= r; 00258 } 00259 00260 inline bool operator>=(double d, const MRational& r){ 00261 return d >= r.toDouble(); 00262 } 00263 00264 template<typename T> 00265 inline bool operator>=(const T& t, const MRational& r){ 00266 return MRational(t) >= r; 00267 } 00268 00269 inline bool operator==(double d, const MRational& r){ 00270 return d == r.toDouble(); 00271 } 00272 00273 template<typename T> 00274 inline bool operator==(const T& t, const MRational& r){ 00275 return MRational(t) == r; 00276 } 00277 00278 inline bool operator!=(double d, const MRational& r){ 00279 return d == r.toDouble(); 00280 } 00281 00282 template<typename T> 00283 inline bool operator!=(const T& t, const MRational& r){ 00284 return MRational(t) != r; 00285 } 00286 00287 inline double operator+(double d, const MRational& r){ 00288 return d + r.toDouble(); 00289 } 00290 00291 template<typename T> 00292 inline MRational operator+(const T& t, const MRational& r){ 00293 return MRational(t) + r; 00294 } 00295 00296 inline double operator-(double d, const MRational& r){ 00297 return d - r.toDouble(); 00298 } 00299 00300 template<typename T> 00301 inline MRational operator-(const T& t, const MRational& r){ 00302 return MRational(t) - r; 00303 } 00304 00305 inline double operator*(double d, const MRational& r){ 00306 return d * r.toDouble(); 00307 } 00308 00309 template<typename T> 00310 inline MRational operator*(const T& t, const MRational& r){ 00311 return MRational(t) * r; 00312 } 00313 00314 inline double operator/(double d, const MRational& r){ 00315 return d / r.toDouble(); 00316 } 00317 00318 template<typename T> 00319 inline MRational operator/(const T& t, const MRational& r){ 00320 return MRational(t) / r; 00321 } 00322 00323 typedef MRational mrat; 00324 00325 } // end namespace Meta 00326 00327 #endif // ANDROMETA_M_RATIONAL_H
1.7.6.1