numerix_doc 0.4
|
00001 00002 /****************************************************************************** 00003 * MODULE : floating.hpp 00004 * DESCRIPTION: Floating point numbers 00005 * COPYRIGHT : (C) 2003--2006 Joris van der Hoeven 00006 ******************************************************************************* 00007 * This software falls under the GNU general public license and comes WITHOUT 00008 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details. 00009 * If you don't have this file, write to the Free Software Foundation, Inc., 00010 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00011 ******************************************************************************/ 00012 00013 #ifndef __MMX_FLOATING_HPP 00014 #define __MMX_FLOATING_HPP 00015 #include <numerix/rational.hpp> 00016 #include <numerix/string_scnot.hpp> 00017 #include <numerix/mmx_mpfr.hpp> 00018 namespace mmx { 00019 #define TMPL_DEF template<typename V=std_floating> 00020 #define TMPL template<typename V> 00021 #define TMPL2 template<typename V,typename V2> 00022 #define Floating floating<V> 00023 #define Floating2 floating<V2> 00024 #define Floating_rep floating_rep<V> 00025 #define Rnd V::rnd() 00026 TMPL class floating; 00027 TMPL class floating_rep; 00028 TMPL Floating copy (const Floating& x); 00029 00030 /****************************************************************************** 00031 * Different variants of floating types 00032 ******************************************************************************/ 00033 00034 class std_floating { 00035 public: 00036 static inline mp_rnd_t rnd () { 00037 return mmx_rounding_mode; } 00038 static inline xnat prec () { 00039 return mmx_bit_precision; } 00040 static inline xnat prec (const mpfr_t& x) { 00041 //(void) x; return DEFAULT_LOW; } 00042 return min (mmx_bit_precision, (xnat) mpfr_get_prec (x)); } 00043 static inline xnat prec (const mpfr_t& x, const mpfr_t& y) { 00044 //(void) x; (void) y; return DEFAULT_LOW; } 00045 return min (mmx_bit_precision, 00046 (xnat) max (mpfr_get_prec (x), mpfr_get_prec (y))); } 00047 static inline xnat disc () { 00048 return mmx_discrepancy; } 00049 }; 00050 00051 class single_floating: public std_floating { 00052 public: 00053 static inline xnat prec () { 00054 return DEFAULT_LOW; } 00055 static inline xnat prec (const mpfr_t& x) { 00056 (void) x; return DEFAULT_LOW; } 00057 static inline xnat prec (const mpfr_t& x, const mpfr_t& y) { 00058 (void) x; (void) y; return DEFAULT_LOW; } 00059 static inline xnat disc () { 00060 return DEFAULT_LOW; } 00061 }; 00062 00063 template<typename V, mp_rnd_t R> 00064 class rnd_floating: public V { 00065 public: 00066 static inline mp_rnd_t rnd () { 00067 return R; } 00068 }; 00069 00070 template<typename V, xnat P, xnat D=P> 00071 class fixed_floating: public V { 00072 public: 00073 static inline xnat prec () { 00074 return P; } 00075 static inline xnat prec (const mpfr_t& x) { 00076 (void) x; return P; } 00077 static inline xnat prec (const mpfr_t& x, const mpfr_t& y) { 00078 (void) x; (void) y; return P; } 00079 static inline xnat disc () { 00080 return D; } 00081 }; 00082 00083 /****************************************************************************** 00084 * The floating type 00085 ******************************************************************************/ 00086 00087 TMPL_DEF 00088 class floating_rep REP_STRUCT { 00089 private: 00090 mpfr_t x; 00091 00092 public: 00093 inline floating_rep () { mpfr_init2 (x, V::prec()); } 00094 inline floating_rep (xnat prec) { mpfr_init2 (x, prec); } 00095 inline ~floating_rep () { mpfr_clear (x); } 00096 friend class Floating; 00097 }; 00098 00099 TMPL_DEF 00100 class floating { 00101 INDIRECT_PROTO_1 (floating, floating_rep, V) 00102 public: 00103 // this should really be private, but we do not want to declare 00104 // the whole set of inline functions below twice 00105 inline floating (const mpfr_t& x): 00106 rep (new Floating_rep (V::prec (x))) {} 00107 inline floating (const mpfr_t& x, const mpfr_t& y): 00108 rep (new Floating_rep (V::prec (x, y))) {} 00109 00110 inline const mpfr_t& operator * () const { return rep->x; } 00111 inline mpfr_t& operator * () { secure(); return rep->x; } 00112 00113 public: 00114 // constructors 00115 inline floating (): 00116 rep (new Floating_rep ()) {} 00117 inline floating (signed int i): 00118 rep (new Floating_rep ()) { mpfr_set_si (rep->x, i, Rnd); } 00119 inline floating (unsigned int i): 00120 rep (new Floating_rep ()) { mpfr_set_ui (rep->x, i, Rnd); } 00121 inline floating (signed short int i): 00122 rep (new Floating_rep ()) { mpfr_set_si (rep->x, i, Rnd); } 00123 inline floating (unsigned short int i): 00124 rep (new Floating_rep ()) { mpfr_set_ui (rep->x, i, Rnd); } 00125 inline floating (signed long int i): 00126 rep (new Floating_rep ()) { mpfr_set_si (rep->x, i, Rnd); } 00127 inline floating (unsigned long int i): 00128 rep (new Floating_rep ()) { mpfr_set_ui (rep->x, i, Rnd); } 00129 inline floating (float x2): 00130 rep (new Floating_rep ()) { mpfr_set_d (rep->x, x2, Rnd); } 00131 inline floating (double x2): 00132 rep (new Floating_rep ()) { mpfr_set_d (rep->x, x2, Rnd); } 00133 inline floating (const integer& i): 00134 rep (new Floating_rep ()) { mpfr_set_z (rep->x, *i, Rnd); } 00135 inline floating (const rational& x): 00136 rep (new Floating_rep ()) { mpfr_set_q (rep->x, *x, Rnd); } 00137 inline floating (const char* s, int base= 0): 00138 rep (new Floating_rep ()) { mpfr_set_str (rep->x, s, base, Rnd); } 00139 inline floating (const string& s, int base= 0): 00140 rep (new Floating_rep ()) { 00141 char* r= as_charp (s); 00142 mpfr_set_str (rep->x, r, base, Rnd); 00143 free_charp (r); } 00144 }; 00145 INDIRECT_IMPL_1 (floating, floating_rep, typename V, V) 00146 00147 template<> struct symbolic_type_information<floating<> > { 00148 static const nat id= SYMBOLIC_FLOATING; }; 00149 00150 TMPL 00151 struct fast_helper<Floating > { 00152 typedef double fast_type; 00153 static inline fast_type dd (const Floating& x) { return as_double (x); } 00154 static inline Floating uu (const fast_type& x) { return Floating (x); } 00155 }; 00156 00157 STYPE_TO_TYPE(TMPL,default_radius_type,Floating,floating<single_floating> ); 00158 00159 /****************************************************************************** 00160 * Standard functions 00161 ******************************************************************************/ 00162 00163 TMPL inline Floating copy (const Floating& x) { 00164 Floating r; mpfr_set (*r, *x, Rnd); return r; } 00165 TMPL inline Floating duplicate (const Floating& x) { 00166 return copy (x); } 00167 00168 TMPL nat hash (const Floating& x) { 00169 mpfr_t y; 00170 mpfr_init2 (y, 32); 00171 mpfr_set (y, *x, GMP_RNDN); 00172 if (!mpfr_number_p (y)) return 12321; 00173 if (mpfr_zero_p (y)) return 1234321; 00174 nat m= (nat) y->_mpfr_d[0]; 00175 nat e= (nat) y->_mpfr_exp; 00176 nat s= (nat) y->_mpfr_sign; 00177 mpfr_clear (y); 00178 return m ^ (m >> ((nat) 31)) ^ e ^ (e << ((nat) 3)) ^ s ^ (s << ((nat) 7)); 00179 } 00180 00181 /****************************************************************************** 00182 * Conversion functions 00183 ******************************************************************************/ 00184 00185 TMPL string 00186 as_string (const Floating& x) { 00187 return mpfr_to_string (*x); 00188 } 00189 00190 TMPL syntactic 00191 flatten (const Floating& g) { 00192 return flatten_number (as_string (g)); 00193 } 00194 00195 TMPL syntactic 00196 flatten (const Floating& g, xnat dd) { 00197 return flatten_number (as_string (g, dd)); 00198 } 00199 00200 TMPL 00201 struct binary_helper<Floating >: public void_binary_helper<Floating > { 00202 static inline string short_type_name () { return "R"; } 00203 static inline generic full_type_name () { return "Floating"; } 00204 static inline generic disassemble (const Floating& x) { 00205 xnat p= precision (x); 00206 string s= mpfr_to_string (*x, p + 9); 00207 return gen_vec (as<generic> (p), as<generic> (s)); } 00208 static inline Floating assemble (const generic& v) { 00209 xnat old= mmx_bit_precision; 00210 mmx_bit_precision= as<xnat> (vector_access (v, 0)); 00211 Floating r= Floating (as<string> (vector_access (v, 1))); 00212 mmx_bit_precision= old; 00213 return r; } 00214 static inline void write (const port& out, const Floating& x) { 00215 mpfr_binary_write (out, *x); } 00216 static inline Floating read (const port& in) { 00217 Floating x; mpfr_binary_read (in, *x); return x; } 00218 }; 00219 00220 template<typename W, typename V> floating<W> 00221 as_floating (const floating<V>& x) { 00222 floating<W> r; 00223 mpfr_set (*r, *x, W::rnd()); 00224 return r; 00225 } 00226 00227 template<typename W,typename V> 00228 struct as_helper<floating<W>,floating<V> > { 00229 static inline floating<W> cv (const floating<V>& x) { 00230 floating<W> r; 00231 mpfr_set (*r, *x, W::rnd()); 00232 return r; 00233 } 00234 }; 00235 00236 template<typename V> floating<V> 00237 as_floating (const rational& x) { 00238 floating<V> r; 00239 mpfr_set_q (*r, *x, V::rnd()); 00240 return r; 00241 } 00242 00243 template<typename V> 00244 struct as_helper<Floating,rational> { 00245 static inline Floating cv (const rational& x) { 00246 return as_floating<V> (x); 00247 } 00248 }; 00249 00250 template<typename V> 00251 struct as_helper<double,Floating > { 00252 static inline double cv (const Floating& x) { return as_double (x); } 00253 }; 00254 00255 template<typename F, typename V> inline Floating 00256 promote (const F& x, const Floating&) { return as<Floating > (x); } 00257 00258 TMPL inline int as_int (const Floating& x) { 00259 return mpfr_get_si (*x, Rnd); } 00260 TMPL inline nat as_nat (const Floating& x) { 00261 return mpfr_get_ui (*x, Rnd); } 00262 TMPL inline double as_double (const Floating& x) { 00263 return mpfr_get_d (*x, Rnd); } 00264 TMPL inline integer as_integer (const Floating& x) { 00265 integer r; mpfr_get_z (*r, *x, Rnd); return r; } 00266 00267 /****************************************************************************** 00268 * Constants 00269 ******************************************************************************/ 00270 00271 TMPL inline void set_nan (Floating& r) { 00272 mpfr_init (*r); } 00273 TMPL inline void set_infinity (Floating& r) { 00274 mpfr_set_inf (*r, 1); } 00275 TMPL inline void set_maximal (Floating& r) { 00276 mpfr_set_inf (*r, 1); } 00277 TMPL inline void set_minimal (Floating& r) { 00278 mpfr_set_inf (*r, -1); } 00279 TMPL inline void set_largest (Floating& r) { 00280 mpfr_set_inf (*r, 1); mpfr_nextbelow (*r); } 00281 TMPL inline void set_smallest (Floating& r) { 00282 mpfr_set_si (*r, 0, GMP_RNDU); mpfr_nextabove (*r); } 00283 TMPL inline void set_accuracy (Floating& r) { 00284 r= next_above (Floating (1)) - Floating (1); } 00285 TMPL inline void set_log2 (Floating& r) { 00286 mpfr_const_log2 (*r, Rnd); } 00287 TMPL inline void set_pi (Floating& r) { 00288 mpfr_const_pi (*r, Rnd); } 00289 TMPL inline void set_euler (Floating& r) { 00290 mpfr_const_euler (*r, Rnd); } 00291 TMPL inline Floating times_infinity (const Floating& x) { 00292 return x * infinity_cst<Floating > (); } 00293 00294 /****************************************************************************** 00295 * Basic arithmetic 00296 ******************************************************************************/ 00297 00298 TMPL inline Floating operator - (const Floating& x1) { 00299 Floating r (*x1); mpfr_neg (*r, *x1, Rnd); return r; } 00300 TMPL inline Floating operator + (const Floating& x1, const Floating& x2) { 00301 Floating r (*x1, *x2); mpfr_add (*r, *x1, *x2, Rnd); return r; } 00302 TMPL inline Floating operator - (const Floating& x1, const Floating& x2) { 00303 Floating r (*x1, *x2); mpfr_sub (*r, *x1, *x2, Rnd); return r; } 00304 TMPL inline Floating operator * (const Floating& x1, const Floating& x2) { 00305 Floating r (*x1, *x2); mpfr_mul (*r, *x1, *x2, Rnd); return r; } 00306 TMPL inline Floating operator / (const Floating& x1, const Floating& x2) { 00307 Floating r (*x1, *x2); mpfr_div (*r, *x1, *x2, Rnd); return r; } 00308 TMPL inline Floating square (const Floating& x) { 00309 Floating r (*x); mpfr_mul (*r, *x, *x, Rnd); return r; } 00310 TMPL inline Floating invert (const Floating& x) { 00311 Floating one (1); Floating r (*x); mpfr_div (*r, *one, *x, Rnd); return r; } 00312 00313 // in-place arithmetic 00314 TMPL inline Floating& operator += (Floating& x1, const Floating& x2) { 00315 x1.secure (); mpfr_add (*x1, *x1, *x2, Rnd); return x1; } 00316 TMPL inline Floating& operator -= (Floating& x1, const Floating& x2) { 00317 x1.secure (); mpfr_sub (*x1, *x1, *x2, Rnd); return x1; } 00318 TMPL inline Floating& operator *= (Floating& x1, const Floating& x2) { 00319 x1.secure (); mpfr_mul (*x1, *x1, *x2, Rnd); return x1; } 00320 TMPL inline Floating& operator /= (Floating& x1, const Floating& x2) { 00321 x1.secure (); mpfr_div (*x1, *x1, *x2, Rnd); return x1; } 00322 00323 TMPL inline void neg (Floating& r) { 00324 r.secure (); mpfr_neg (*r, *r, Rnd); } 00325 TMPL inline void neg (Floating& r, const Floating& x1) { 00326 r.secure (); mpfr_neg (*r, *x1, Rnd); } 00327 TMPL inline void add (Floating& r, const Floating& x1, const Floating& x2) { 00328 r.secure (); mpfr_add (*r, *x1, *x2, Rnd); } 00329 TMPL inline void sub (Floating& r, const Floating& x1, const Floating& x2) { 00330 r.secure (); mpfr_sub (*r, *x1, *x2, Rnd); } 00331 TMPL inline void mul (Floating& r, const Floating& x1, const Floating& x2) { 00332 r.secure (); mpfr_mul (*r, *x1, *x2, Rnd); } 00333 TMPL inline void div (Floating& r, const Floating& x1, const Floating& x2) { 00334 r.secure (); mpfr_div (*r, *x1, *x2, Rnd); } 00335 TMPL inline void mul_add (Floating& r, const Floating& x, const Floating& y) { 00336 r.secure (); mpfr_fma (*r, *x, *y, *r, Rnd); } 00337 00338 ARITH_INT_SUGAR(TMPL,Floating); 00339 00340 /****************************************************************************** 00341 * Order related functions 00342 ******************************************************************************/ 00343 00344 TMPL inline bool is_zero (const Floating& x) { 00345 return mpfr_zero_p (*x) != 0; } 00346 00347 TMPL2 inline bool operator == (const Floating& x1, const Floating2& x2) { 00348 return mpfr_equal_p (*x1, *x2) != 0; } 00349 TMPL2 inline bool operator != (const Floating& x1, const Floating2& x2) { 00350 return mpfr_equal_p (*x1, *x2) == 0; } 00351 TMPL2 inline bool operator < (const Floating& x1, const Floating2& x2) { 00352 return mpfr_less_p (*x1, *x2) != 0; } 00353 TMPL2 inline bool operator > (const Floating& x1, const Floating2& x2) { 00354 return mpfr_greater_p (*x1, *x2) != 0; } 00355 TMPL2 inline bool operator <= (const Floating& x1, const Floating2& x2) { 00356 return mpfr_lessequal_p (*x1, *x2) != 0; } 00357 TMPL2 inline bool operator >= (const Floating& x1, const Floating2& x2) { 00358 return mpfr_greaterequal_p (*x1, *x2) != 0; } 00359 00360 TMPL inline bool operator == (const Floating& x1, const Floating& x2) { 00361 return mpfr_equal_p (*x1, *x2) != 0; } 00362 TMPL inline bool operator != (const Floating& x1, const Floating& x2) { 00363 return mpfr_equal_p (*x1, *x2) == 0; } 00364 TMPL inline bool operator < (const Floating& x1, const Floating& x2) { 00365 return mpfr_less_p (*x1, *x2) != 0; } 00366 TMPL inline bool operator > (const Floating& x1, const Floating& x2) { 00367 return mpfr_greater_p (*x1, *x2) != 0; } 00368 TMPL inline bool operator <= (const Floating& x1, const Floating& x2) { 00369 return mpfr_lessequal_p (*x1, *x2) != 0; } 00370 TMPL inline bool operator >= (const Floating& x1, const Floating& x2) { 00371 return mpfr_greaterequal_p (*x1, *x2) != 0; } 00372 00373 TMPL inline int sign (const Floating& x1) { 00374 int r= mpfr_sgn (*x1); return (r<0? -1: (r==0? 0: 1)); } 00375 TMPL inline Floating abs (const Floating& x1) { 00376 Floating r (*x1); mpfr_abs (*r, *x1, Rnd); return r; } 00377 TMPL inline Floating min (const Floating& x1, const Floating& x2) { 00378 return x1 <= x2? x1: x2; } 00379 TMPL inline Floating max (const Floating& x1, const Floating& x2) { 00380 return x1 >= x2? x1: x2; } 00381 00382 TMPL inline Floating floor (const Floating& x1) { 00383 Floating r (*x1); mpfr_floor (*r, *x1); return r; } 00384 TMPL inline Floating trunc (const Floating& x1) { 00385 Floating r (*x1); mpfr_trunc (*r, *x1); return r; } 00386 TMPL inline Floating ceil (const Floating& x1) { 00387 Floating r (*x1); mpfr_ceil (*r, *x1); return r; } 00388 TMPL inline Floating round (const Floating& x1) { 00389 Floating r (*x1); mpfr_round (*r, *x1); return r; } 00390 00391 TRUE_TO_EXACT_IDENTITY_SUGAR(TMPL,Floating) 00392 EQUAL_INT_SUGAR(TMPL,Floating); 00393 COMPARE_INT_SUGAR(TMPL,Floating); 00394 00395 /****************************************************************************** 00396 * Elementary functions 00397 ******************************************************************************/ 00398 00399 TMPL inline Floating sqrt (const Floating& x1) { 00400 Floating r (*x1); mpfr_sqrt (*r, *x1, Rnd); return r; } 00401 TMPL inline Floating cbrt (const Floating& x1) { 00402 Floating r (*x1); mpfr_cbrt (*r, *x1, Rnd); return r; } 00403 TMPL inline Floating hypot (const Floating& x1, const Floating& x2) { 00404 Floating r (*x1, *x2); mpfr_hypot (*r, *x1, *x2, Rnd); return r; } 00405 TMPL inline Floating exp (const Floating& x1) { 00406 Floating r (*x1); mpfr_exp (*r, *x1, Rnd); return r; } 00407 TMPL inline Floating exp2 (const Floating& x1) { 00408 Floating r (*x1); mpfr_exp2 (*r, *x1, Rnd); return r; } 00409 TMPL inline Floating log (const Floating& x1) { 00410 Floating r (*x1); mpfr_log (*r, *x1, Rnd); return r; } 00411 TMPL inline Floating log2 (const Floating& x1) { 00412 Floating r (*x1); mpfr_log2 (*r, *x1, Rnd); return r; } 00413 TMPL inline Floating log10 (const Floating& x1) { 00414 Floating r (*x1); mpfr_log10 (*r, *x1, Rnd); return r; } 00415 TMPL inline Floating cos (const Floating& x1) { 00416 Floating r (*x1); mpfr_cos (*r, *x1, Rnd); return r; } 00417 TMPL inline Floating sin (const Floating& x1) { 00418 Floating r (*x1); mpfr_sin (*r, *x1, Rnd); return r; } 00419 TMPL inline Floating tan (const Floating& x1) { 00420 Floating r (*x1); mpfr_tan (*r, *x1, Rnd); return r; } 00421 TMPL inline Floating acos (const Floating& x1) { 00422 Floating r (*x1); mpfr_acos (*r, *x1, Rnd); return r; } 00423 TMPL inline Floating asin (const Floating& x1) { 00424 Floating r (*x1); mpfr_asin (*r, *x1, Rnd); return r; } 00425 TMPL inline Floating atan (const Floating& x1) { 00426 Floating r (*x1); mpfr_atan (*r, *x1, Rnd); return r; } 00427 //TMPL inline Floating atan2 (const Floating& x1, const Floating& x2) { 00428 //Floating r (*x1, *x2); mpfr_atan2 (*r, *x1, *x2, Rnd); return r; } 00429 TMPL inline Floating cosh (const Floating& x1) { 00430 Floating r (*x1); mpfr_cosh (*r, *x1, Rnd); return r; } 00431 TMPL inline Floating sinh (const Floating& x1) { 00432 Floating r (*x1); mpfr_sinh (*r, *x1, Rnd); return r; } 00433 TMPL inline Floating tanh (const Floating& x1) { 00434 Floating r (*x1); mpfr_tanh (*r, *x1, Rnd); return r; } 00435 TMPL inline Floating acosh (const Floating& x1) { 00436 Floating r (*x1); mpfr_acosh (*r, *x1, Rnd); return r; } 00437 TMPL inline Floating asinh (const Floating& x1) { 00438 Floating r (*x1); mpfr_asinh (*r, *x1, Rnd); return r; } 00439 TMPL inline Floating atanh (const Floating& x1) { 00440 Floating r (*x1); mpfr_atanh (*r, *x1, Rnd); return r; } 00441 TMPL inline Floating pow (const Floating& x1, const Floating& x2) { 00442 Floating r (*x1, *x2); mpfr_pow (*r, *x1, *x2, Rnd); return r; } 00443 TMPL inline Floating pow (const Floating& x1, const int& x2) { 00444 Floating r (*x1); mpfr_pow_ui (*r, *x1, x2, Rnd); return r; } 00445 TMPL inline Floating pow (const int& x1, const Floating& x2) { 00446 Floating r (*x2); mpfr_ui_pow (*r, x1, *x2, Rnd); return r; } 00447 00448 ATAN2_SUGAR(TMPL,Floating) 00449 INV_TRIGO_SUGAR(TMPL,Floating) 00450 INV_HYPER_SUGAR(TMPL,Floating) 00451 00452 /****************************************************************************** 00453 * Floating point related functions 00454 ******************************************************************************/ 00455 00456 TMPL inline bool is_finite (const Floating& x) { 00457 return mpfr_number_p (*x) != 0; } 00458 TMPL inline bool is_infinite (const Floating& x) { 00459 return mpfr_inf_p (*x) != 0; } 00460 TMPL inline bool is_fuzz (const Floating& x) { 00461 (void) x; return false; } 00462 TMPL inline bool is_nan (const Floating& x) { 00463 return mpfr_nan_p (*x) != 0; } 00464 TMPL inline bool is_reliable (const Floating& x) { 00465 (void) x; return false; } 00466 00467 TMPL inline Floating change_precision (const Floating& x, xnat prec) { 00468 Floating r (new Floating_rep (prec)); mpfr_set (*r, *x, Rnd); return r; } 00469 TMPL inline xnat precision (const Floating& x) { 00470 return (xnat) mpfr_get_prec (*x); } 00471 TMPL inline Floating next_above (const Floating& x1) { 00472 Floating r (*x1); mpfr_set (*r, *x1, Rnd); 00473 mpfr_nextabove (*r); return r; } 00474 TMPL inline Floating next_below (const Floating& x1) { 00475 Floating r (*x1); mpfr_set (*r, *x1, Rnd); 00476 mpfr_nextbelow (*r); return r; } 00477 TMPL inline Floating rounding_error (const Floating& x) { 00478 Floating r (*x); 00479 mpfr_abs (*r, *x, GMP_RNDN); 00480 mpfr_mul_2si (*r, *r, 1-mpfr_get_prec (*x), GMP_RNDN); 00481 mpfr_nextabove (*r); 00482 return r; } 00483 TMPL inline Floating additive_error (const Floating& x) { 00484 Floating r (*x); 00485 mpfr_abs (*r, *x, GMP_RNDN); 00486 mpfr_mul_2si (*r, *r, 1-mpfr_get_prec (*x), GMP_RNDN); 00487 mpfr_nextabove (*r); 00488 return r; } 00489 00490 TMPL inline xint exponent (const Floating& x) { 00491 return mpfr_safe_get_exp (*x); } 00492 TMPL inline double magnitude (const Floating& x) { 00493 return mpfr_get_magnitude (*x); } 00494 TMPL inline Floating operator << (const Floating& x1, const xint& shift) { 00495 Floating r (*x1); mpfr_mul_2si (*r, *x1, shift, Rnd); return r; } 00496 TMPL inline Floating operator >> (const Floating& x1, const xint& shift) { 00497 Floating r (*x1); mpfr_mul_2si (*r, *x1, -shift, Rnd); return r; } 00498 TMPL inline Floating& operator <<= (Floating& x1, const xint& shift) { 00499 return x1= x1 << shift; } 00500 TMPL inline Floating& operator >>= (Floating& x1, const xint& shift) { 00501 return x1= x1 >> shift; } 00502 00503 /****************************************************************************** 00504 * Special functions and other functions 00505 ******************************************************************************/ 00506 00507 TMPL inline Floating gamma (const Floating& x1) { 00508 Floating r (*x1); mpfr_gamma (*r, *x1, Rnd); return r; } 00509 TMPL inline Floating zeta (const Floating& x1) { 00510 Floating r (*x1); mpfr_zeta (*r, *x1, Rnd); return r; } 00511 TMPL inline Floating erf (const Floating& x1) { 00512 Floating r (*x1); mpfr_erf (*r, *x1, Rnd); return r; } 00513 00514 TMPL inline Floating uniform_deviate (const Floating& lo, const Floating& hi) { 00515 Floating r (*lo, *hi); mpfr_urandomb (*r, gmp_randstate); 00516 return lo + r * (hi - lo); } 00517 00518 /****************************************************************************** 00519 * Extras for glue 00520 ******************************************************************************/ 00521 00522 #define mmx_floating floating<> 00523 #define make_literal_floating make_literal<floating<> > 00524 00525 #undef TMPL_DEF 00526 #undef TMPL 00527 #undef TMPL2 00528 #undef Floating 00529 #undef Floating2 00530 #undef Floating_rep 00531 #undef Rnd 00532 } 00533 #endif // __MMX_FLOATING_HPP