big_float.hpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002     
00003     big_float.hpp -- Big Float Numbers implementation.
00004 
00005     This file is a part of the Arageli library.
00006 
00007     Copyright (C) Alexander Pshenichnikov, 2005--2006
00008     Copyright (C) Nikolay Santalov, 2005--2006
00009     
00010     University of Nizhni Novgorod, Russia
00011 
00012 *****************************************************************************/
00013 
00035 #include "config.hpp"
00036 
00037 #include <iostream>
00038 #include "std_import.hpp"
00039 #include "big_int.hpp"
00040 
00041 #ifndef _ARAGELI_big_float_h_
00042 #define _ARAGELI_big_float_h_
00043 
00044 #undef NAN
00045 
00046 #pragma warning ( disable : 4018 )
00047 
00048 namespace Arageli
00049 {
00050  
00053 
00054 // WARNING! Names are too simple and general.
00055 // TODO: Rename or incapsulate them!
00056 // WARNING! How does it combine with ISO C++ 18.2.1.3?
00057 
00058 const int EXACT = 1; 
00059 const int ROUND = 2; 
00060 const int TO_NEAREST = 3; 
00061 const int TO_ZERO = 4; 
00062 const int TO_P_INF = 5; 
00063 const int TO_M_INF = 6; 
00064 const int TO_INF = 7;   
00065 
00066 
00069 
00070 const int NAN = 1;
00071 const int PINF = 2;
00072 const int M_INF= 3;
00073 const int P_ZERO = 4;
00074 const int M_ZERO = 5;
00075 
00077 
00078 const int PREC_MIN = 2;
00079 const unsigned long PREC_MAX = _Internal::max_digit;
00080 const unsigned long D_PREC_MAX = ( long ) (_Internal::max_digit * log ( 2.0l ) / log ( 10.0l )); 
00081  
00082 class big_float
00083 {
00084 
00085  public:
00086 
00087   big_float();                    
00088   big_float(const char *str);           
00089   big_float(const big_float & b); 
00090   big_float( const big_int &s, const big_int &e);
00091   big_float(double b );        
00092   big_float(int i);               
00093   ~big_float();                   
00094 
00095   big_float & operator = (const big_float & b);     
00096   big_float & operator = ( const double d );          
00097   big_float & operator = ( const char *str );         
00098   big_float & operator = ( const int i );                
00099 
00100   big_int get_exponent() const;
00101 
00103   big_int get_significant() const;
00104 
00106   friend std::ostream & operator << (std::ostream & s, const big_float & x);
00108   friend std::istream & operator >> (std::istream & s, big_float & x);
00110   void out ( std::ostream & os, char c = 'd' ) const ;
00112   void in ( std::istream &ins, char c = 'd' );
00113 
00115   static void set_global_precision(long p); 
00117   static void set_round_mode (int m = TO_NEAREST);
00118 
00120 
00126   friend int cmp(const big_float & a, const big_float & b);
00128   friend int operator ==(const big_float & a, const big_float & b);
00130   friend int operator !=(const big_float & a, const big_float & b);
00132   friend int operator > (const big_float & a, const big_float & b);
00134   friend int operator >=(const big_float & a, const big_float & b);
00136   friend int operator < (const big_float & a, const big_float & b);
00138   friend int operator <=(const big_float & a, const big_float & b);
00139 
00140   friend big_float operator + (const big_float & a);       
00141   friend big_float operator - (const big_float & a);       
00142 
00146   friend big_float add(const big_float & b, const big_float & c, long prec, int mode);
00147   friend big_float add(const big_float & b, const big_float & c, long prec);
00148   friend big_float add(const big_float & b, const big_float & c);
00149 
00153   friend big_float sub(const big_float & b, const big_float & c, long prec, int mode);
00154   friend big_float sub(const big_float & b, const big_float & c, long prec);
00155   friend big_float sub(const big_float & b, const big_float & c);
00156 
00160   friend big_float mul(const big_float & b, const big_float & c, long prec, int mode);
00161   friend big_float mul(const big_float & b, const big_float & c, long prec);
00162   friend big_float mul(const big_float & b, const big_float & c);
00163 
00167   friend big_float div(const big_float & b, const big_float & c, long prec, int mode);
00168   friend big_float div(const big_float & b, const big_float & c, long prec);
00169   friend big_float div(const big_float & b, const big_float & c);
00170   friend big_float divnu(const big_float & b, const big_float & c, long prec, int mode);
00171   friend big_float div_i ( const big_int & c );
00175   friend big_float fsqrt(const big_float & b,/* const big_float & c,*/ long prec, int mode);
00176   friend big_float fsqrt(const big_float & b,/* const big_float & c,*/ long prec);
00177   friend big_float fsqrt(const big_float & b/*, const big_float & c*/);
00178   friend big_float nfsqrt(const big_float & b,/* const big_float & c,*/ long prec, int mode);
00179 
00181   friend big_float operator + (const big_float & b, const big_float & c);
00183   friend big_float operator - (const big_float & b, const big_float & c);
00185   friend big_float operator * (const big_float & b, const big_float & c);
00187   friend big_float operator / (const big_float & b, const big_float & c);
00189   friend big_float & operator += (big_float & b, const big_float & c);
00191   friend big_float & operator -= (big_float & b, const big_float & c);
00193   friend big_float & operator *= (big_float & b, const big_float & c);
00195   friend big_float & operator /= (big_float & b, const big_float & c);
00196 
00198   friend big_float ceil  (const big_float & a);
00200   friend big_float floor (const big_float & a);
00202   friend big_float frac  (const big_float & a);
00204   friend big_int iceil (const big_float & a);
00206   friend big_int ifloor(const big_float & a);
00207     
00209   friend big_float frandom  (long bits);
00211   friend big_float frandom  ( );
00213   friend big_float frandom1 (long bits, const big_int & exp = 0 );
00214 
00215   int is_NaN();     
00216   int is_pInf();    
00217   int is_mInf();    
00218   int is_Inf();     
00219   int is_pZero();   
00220   int is_mZero();   
00221   int is_Zero();    
00222   int is_Special(); 
00223     
00224  private:
00225   void normalize_1 ( long prec = big_float::prec, long mode = big_float::mode );
00226   
00227   // the following type is used inside the bigfloat unit and implements
00228   // the storage for a Big Float Number
00229 
00230   big_int e; 
00231   big_int s; //< significant
00232 
00233   int special;  
00234   
00235   static long prec; 
00236   static long mode; 
00237 };
00238 
00239 
00240 inline big_float abs (const big_float& x)
00241 { return is_negative(x) ? -x : x; }
00242 
00243 
00244 template <typename Outiter>
00245 inline void generate_range_helper (big_float& t1, const big_float& t2, Outiter outiter)
00246 { generate_range_helper_wo_inc(t1, t2, outiter); }
00247 
00248 
00249 inline big_float & operator+= (big_float & b, const big_float & c)
00250 { return b = b + c; }
00251 
00252 inline big_float & operator-= (big_float & b, const big_float & c)
00253 { return b = b - c; }
00254 
00255 inline big_float & operator*= (big_float & b, const big_float & c)
00256 { return b = b * c; }
00257 
00258 inline big_float & operator/= (big_float & b, const big_float & c)
00259 { return b = b / c; }
00260 
00261 
00262 
00263 }
00264 
00265 #endif

Generated on Thu Aug 31 17:38:04 2006 for Arageli by  doxygen 1.4.7