00001
00007
00008 #include<config.h>
00009 #include <gmp.h>
00010
00011 #ifdef USE_MPFR
00012 #include <mpfr.h>
00013 #include <mpf2mpfr.h>
00014 #endif
00015
00016 CPS_START_NAMESPACE
00017
00018 #ifndef INCLUDED_BIGFLOAT_H
00019 #define INCLUDED_BIGFLOAT_H
00020
00021
00022
00023
00024
00025
00026
00028
00037 class bigfloat {
00038
00039 private:
00040
00041 mpf_t x;
00042
00043 public:
00044
00045 bigfloat() { mpf_init(x); }
00046 bigfloat(const bigfloat& y) { mpf_init_set(x, y.x); }
00047 bigfloat(const unsigned long u) { mpf_init_set_ui(x, u); }
00048 bigfloat(const long i) { mpf_init_set_si(x, i); }
00049 bigfloat(const int i) {mpf_init_set_si(x,(long)i);}
00050 bigfloat(const float d) { mpf_init_set_d(x, (double)d); }
00051 bigfloat(const double d) { mpf_init_set_d(x, d); }
00052 ~bigfloat(void) { mpf_clear(x); }
00053 operator const Float (void) const { return (Float)mpf_get_d(x); }
00054 static void setDefaultPrecision(unsigned long dprec) {
00055 unsigned long bprec = (unsigned long)(3.321928094 * (double)dprec);
00056 mpf_set_default_prec(bprec);
00057 }
00058
00059 void setPrecision(unsigned long dprec) {
00060 unsigned long bprec = (unsigned long)(3.321928094 * (double)dprec);
00061 mpf_set_prec(x,bprec);
00062 }
00063
00064 unsigned long getPrecision(void) const { return mpf_get_prec(x); }
00065
00066 unsigned long getDefaultPrecision(void) const { return mpf_get_default_prec(); }
00067
00068 bigfloat& operator=(const bigfloat& y) {
00069 mpf_set(x, y.x);
00070 return *this;
00071 }
00072
00073 bigfloat& operator=(const unsigned long y) {
00074 mpf_set_ui(x, y);
00075 return *this;
00076 }
00077
00078 bigfloat& operator=(const signed long y) {
00079 mpf_set_si(x, y);
00080 return *this;
00081 }
00082
00083 bigfloat& operator=(const float y) {
00084 mpf_set_d(x, (double)y);
00085 return *this;
00086 }
00087
00088 bigfloat& operator=(const double y) {
00089 mpf_set_d(x, y);
00090 return *this;
00091 }
00092
00093 size_t write(void);
00094 size_t read(void);
00095
00096
00097
00098 bigfloat& operator+=(const bigfloat& y) { return *this = *this + y; }
00099 bigfloat& operator-=(const bigfloat& y) { return *this = *this - y; }
00100 bigfloat& operator*=(const bigfloat& y) { return *this = *this * y; }
00101 bigfloat& operator/=(const bigfloat& y) { return *this = *this / y; }
00102
00103 friend bigfloat operator+(const bigfloat& x, const bigfloat& y) {
00104 bigfloat a;
00105 mpf_add(a.x,x.x,y.x);
00106 return a;
00107 }
00108
00109 friend bigfloat operator+(const bigfloat& x, const unsigned long y) {
00110 bigfloat a;
00111 mpf_add_ui(a.x,x.x,y);
00112 return a;
00113 }
00114
00115 friend bigfloat operator-(const bigfloat& x, const bigfloat& y) {
00116 bigfloat a;
00117 mpf_sub(a.x,x.x,y.x);
00118 return a;
00119 }
00120
00121 friend bigfloat operator-(const unsigned long x, const bigfloat& y) {
00122 bigfloat a;
00123 mpf_ui_sub(a.x,x,y.x);
00124 return a;
00125 }
00126
00127 friend bigfloat operator-(const bigfloat& x, const unsigned long y) {
00128 bigfloat a;
00129 mpf_sub_ui(a.x,x.x,y);
00130 return a;
00131 }
00132
00133 friend bigfloat operator-(const bigfloat& x) {
00134 bigfloat a;
00135 mpf_neg(a.x,x.x);
00136 return a;
00137 }
00138
00139 friend bigfloat operator*(const bigfloat& x, const bigfloat& y) {
00140 bigfloat a;
00141 mpf_mul(a.x,x.x,y.x);
00142 return a;
00143 }
00144
00145 friend bigfloat operator*(const bigfloat& x, const unsigned long y) {
00146 bigfloat a;
00147 mpf_mul_ui(a.x,x.x,y);
00148 return a;
00149 }
00150
00151 friend bigfloat operator/(const bigfloat& x, const bigfloat& y){
00152 bigfloat a;
00153 mpf_div(a.x,x.x,y.x);
00154 return a;
00155 }
00156
00157 friend bigfloat operator/(const unsigned long x, const bigfloat& y){
00158 bigfloat a;
00159 mpf_ui_div(a.x,x,y.x);
00160 return a;
00161 }
00162
00163 friend bigfloat operator/(const bigfloat& x, const unsigned long y){
00164 bigfloat a;
00165 mpf_div_ui(a.x,x.x,y);
00166 return a;
00167 }
00168
00169 friend bigfloat sqrt_bf(const bigfloat& x){
00170 bigfloat a;
00171 mpf_sqrt(a.x,x.x);
00172 return a;
00173 }
00174
00175 friend bigfloat sqrt_bf(const unsigned long x){
00176 bigfloat a;
00177 mpf_sqrt_ui(a.x,x);
00178 return a;
00179 }
00180
00181 friend bigfloat abs_bf(const bigfloat& x){
00182 bigfloat a;
00183 mpf_abs(a.x,x.x);
00184 return a;
00185 }
00186
00187 friend bigfloat pow_bf(const bigfloat& a, long power) {
00188 bigfloat b;
00189 mpf_pow_ui(b.x,a.x,power);
00190 return b;
00191 }
00192
00193 #ifdef USE_MPFR
00194 friend bigfloat pow_bf(const bigfloat& a, const bigfloat &power) {
00195 bigfloat b;
00196 mpfr_pow(b.x,a.x,power.x,GMP_RNDN);
00197 return b;
00198 }
00199 #endif
00200
00201
00202
00203 friend int operator>(const bigfloat& x, const bigfloat& y) {
00204 int test;
00205 test = mpf_cmp(x.x,y.x);
00206 if (test > 0) return 1;
00207 else return 0;
00208 }
00209
00210 friend int operator<(const bigfloat& x, const bigfloat& y) {
00211 int test;
00212 test = mpf_cmp(x.x,y.x);
00213 if (test < 0) return 1;
00214 else return 0;
00215 }
00216
00217 friend int sgn(const bigfloat&);
00218
00219
00220
00221
00222 };
00223
00224 #endif
00225
00226 CPS_END_NAMESPACE