AndroMeta  2.0.0
AndroMeta/include/AndroMeta/MReal.h
Go to the documentation of this file.
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_REAL_H
00019 #define ANDROMETA_M_REAL_H
00020 
00021 #include <iostream>
00022 #include <ostream>
00023 
00024 #include <AndroMeta/MStr.h>
00025 #include <AndroMeta/MRational.h>
00026 
00027 namespace Meta{
00028   
00037   class MReal{
00038   public:
00039     
00040     MReal();
00041     
00047     explicit MReal(const char* s);
00048     
00049     MReal(double x);
00050     
00051     MReal(int64_t x);
00052     
00053     MReal(int x);
00054     
00055     MReal(const MReal& r);
00056     
00057     MReal(const mrat& r);
00058     
00059     ~MReal();
00060     
00061     MReal& operator=(const MReal& r);
00062     
00063     MReal& operator+=(const MReal& r);
00064     
00065     template<typename T>
00066     MReal operator+(const T& t) const{
00067       MReal ret(*this);
00068       return ret += MReal(t);
00069     }
00070     
00071     MReal operator+(const MReal& x) const{
00072       MReal ret(*this);
00073       return ret += x;
00074     }
00075     
00076     MReal operator++(int){
00077       MReal ret(*this);
00078       *this += 1;
00079       return ret;
00080     }
00081     
00082     MReal& operator++(){
00083       return *this += 1;
00084     }
00085     
00086     MReal& operator-=(const MReal& r);
00087     
00088     template<typename T>
00089     MReal operator-(const T& t) const{
00090       MReal ret(*this);
00091       return ret -= MReal(t);
00092     }
00093     
00094     MReal operator-(const MReal& x) const{
00095       MReal ret(*this);
00096       return ret -= x;
00097     }
00098     
00099     MReal operator--(int){
00100       MReal ret(*this);
00101       *this -= 1;
00102       return ret;
00103     }
00104     
00105     MReal& operator--(){
00106       return *this -= 1;
00107     }
00108     
00109     MReal operator-() const;
00110     
00111     MReal& operator*=(const MReal& r);
00112     
00113     template<typename T>
00114     MReal operator*(const T& t) const{
00115       MReal ret(*this);
00116       return ret *= MReal(t);
00117     }
00118     
00119     MReal operator*(const MReal& x) const{
00120       MReal ret(*this);
00121       return ret *= x;
00122     }
00123     
00124     MReal& operator/=(const MReal& r);
00125     
00126     template<typename T>
00127     MReal operator/(const T& t) const{
00128       MReal ret(*this);
00129       return ret /= MReal(t);
00130     }
00131     
00132     MReal operator/(const MReal& x) const{
00133       MReal ret(*this);
00134       return ret /= x;
00135     }
00136     
00137     MReal& operator%=(const MReal& x);
00138     
00139     template<typename T>
00140     MReal operator%(const T& t) const{
00141       MReal ret(*this);
00142       return ret %= MReal(t);
00143     }
00144     
00145     MReal operator%(const MReal& x) const{
00146       MReal ret(*this);
00147       return ret %= x;
00148     }
00149     
00150     bool operator<(const MReal& x) const;
00151     
00152     template<typename T>
00153     bool operator<(const T& t) const{
00154       return *this < MReal(t);
00155     }
00156     
00157     bool operator>(const MReal& x) const;
00158     
00159     template<typename T>
00160     bool operator>(const T& t) const{
00161       return *this > MReal(t);
00162     }
00163     
00164     bool operator<=(const MReal& x) const;
00165     
00166     template<typename T>
00167     bool operator<=(const T& t) const{
00168       return *this <= MReal(t);
00169     }
00170     
00171     bool operator>=(const MReal& x) const;
00172     
00173     template<typename T>
00174     bool operator>=(const T& t) const{
00175       return *this >= MReal(t);
00176     }
00177     
00178     bool operator==(const MReal& x) const;
00179     
00180     template<typename T>
00181     bool operator==(const T& t) const{
00182       return *this == MReal(t);
00183     }
00184     
00185     bool operator!=(const MReal& x) const;
00186     
00187     template<typename T>
00188     bool operator!=(const T& t) const{
00189       return *this != MReal(t);
00190     }
00191     
00195     void setPrecision(size_t bits);
00196     
00202     static void setDefaultPrecision(size_t bits);
00203     
00204     static size_t defaultPrecision();
00205     
00206     double toDouble() const;
00207     
00208     int64_t toLong() const;
00209     
00210     mrat toRat() const;
00211     
00212     static MReal fromStr(const MStr& str);
00213     
00223     MStr toStr(bool exp=true, int precision=-1) const;
00224     
00228     static MReal cos(const MReal& x);
00229     
00233     static MReal sin(const MReal& x);
00234     
00238     static MReal tan(const MReal& x);
00239     
00243     static MReal acos(const MReal& x);
00244     
00248     static MReal asin(const MReal& x);
00249     
00253     static MReal atan(const MReal& x);
00254     
00258     static MReal atan2(const MReal& y, const MReal& x);
00259     
00263     static MReal cosh(const MReal& x);
00264     
00268     static MReal sinh(const MReal& x);
00269     
00273     static MReal tanh(const MReal& x);
00274     
00278     static MReal exp(const MReal& x);
00279     
00283     static MReal log(const MReal& x);
00284     
00288     static MReal log10(const MReal& x);
00289     
00293     static MReal pow(const MReal& base, const MReal& exponent);
00294     
00298     static MReal sqrt(const MReal& x);
00299     
00303     static MReal ceil(const MReal& x);
00304     
00308     static MReal floor(const MReal& x);
00309     
00313     static MReal abs(const MReal& x);
00314     
00318     static MReal pi();
00319     
00323     static MReal euler();
00324     
00328     static MReal catalan();
00329 
00330   private:
00331     class MReal_* x_;
00332   };
00333   
00334   typedef MReal mreal;
00335   
00336   template<typename T>
00337   inline bool operator<(const T& t, const MReal& x){
00338     return MReal(t) < x;
00339   }
00340   
00341   template<typename T>
00342   inline bool operator>(const T& t, const MReal& x){
00343     return MReal(t) > x;
00344   }
00345   
00346   template<typename T>
00347   inline bool operator<=(const T& t, const MReal& x){
00348     return MReal(t) <= x;
00349   }
00350   
00351   template<typename T>
00352   inline bool operator>=(const T& t, const MReal& x){
00353     return MReal(t) >= x;
00354   }
00355   
00356   template<typename T>
00357   inline bool operator==(const T& t, const MReal& x){
00358     return MReal(t) == x;
00359   }
00360   
00361   template<typename T>
00362   inline bool operator!=(const T& t, const MReal& x){
00363     return MReal(t) != x;
00364   }
00365   
00366   template<typename T>
00367   inline MReal operator+(const T& t, const MReal& x){
00368     return MReal(t) + x;
00369   }
00370   
00371   template<typename T>
00372   inline MReal operator-(const T& t, const MReal& x){
00373     return MReal(t) - x;
00374   }
00375   
00376   template<typename T>
00377   inline MReal operator*(const T& t, const MReal& x){
00378     return MReal(t) * x;
00379   }
00380   
00381   template<typename T>
00382   inline MReal operator/(const T& t, const MReal& x){
00383     return MReal(t) / x;
00384   }
00385   
00386   template<typename T>
00387   inline MReal operator%(const T& t, const MReal& x){
00388     return MReal(t) % x;
00389   }
00390   
00391   inline std::ostream& operator<<(std::ostream& ostr, const MReal& r){
00392     return ostr << r.toStr();
00393   }
00394   
00395 } // end namespace Meta
00396 
00397 #endif // ANDROMETA_M_REAL_H