C-XSC - A C++ Class Library for Extended Scientific Computing  2.5.4
real.hpp
00001 /*
00002 **  CXSC is a C++ library for eXtended Scientific Computing (V 2.5.4)
00003 **
00004 **  Copyright (C) 1990-2000 Institut fuer Angewandte Mathematik,
00005 **                          Universitaet Karlsruhe, Germany
00006 **            (C) 2000-2014 Wiss. Rechnen/Softwaretechnologie
00007 **                          Universitaet Wuppertal, Germany   
00008 **
00009 **  This library is free software; you can redistribute it and/or
00010 **  modify it under the terms of the GNU Library General Public
00011 **  License as published by the Free Software Foundation; either
00012 **  version 2 of the License, or (at your option) any later version.
00013 **
00014 **  This library is distributed in the hope that it will be useful,
00015 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 **  Library General Public License for more details.
00018 **
00019 **  You should have received a copy of the GNU Library General Public
00020 **  License along with this library; if not, write to the Free
00021 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 /* CVS $Id: real.hpp,v 1.42 2014/01/30 17:23:48 cxsc Exp $ */
00025 
00026 #ifndef _CXSC_REAL_HPP_INCLUDED
00027 #define _CXSC_REAL_HPP_INCLUDED
00028 
00029 #include <iostream>
00030 #include <string>
00031 
00032 // namespace cxsc {
00033 
00034 #include "compiler.h"
00035 #include "RtsTyp.h"
00036 #include "ioflags.hpp"
00037 
00038 #include "except.hpp"
00040 
00044 namespace cxsc {
00045 
00046 class rvector;
00047 class rmatrix;
00048 class rvector_slice;
00049 class rmatrix_slice;
00050 
00051 #define  addu  addup
00052 #define  addd  adddown
00053 #define  subu  subup
00054 #define  subd  subdown
00055 #define  mulu  multup
00056 #define  muld  multdown
00057 #define  divu  divup
00058 #define  divd  divdown
00059 
00060 // ---------------------------------------------------------------------------
00061 // ----                                                                   ----
00062 // ---- class real (declaration)                                          ----
00063 // ----                                                                   ----
00064 // ---------------------------------------------------------------------------
00065 
00067 
00113 class real
00114 {
00115    private:
00116       // ---- private data ----------------------------------------
00117       double w;
00118 
00119    public:
00120       // ---- implicit constructors -------------------------------
00122       real(void)  throw ()                  { }
00124       real(const float  &a) throw () : w(a) { }
00126       real(const double &a) throw () : w(a) { }
00128       real(const int     a) throw () : w(a) { }
00130       real(const long    a) throw () : w(a) { }
00131 
00132       // ---- explicit constructors -------------------------------
00134       explicit real(const l_real &) throw();
00135 
00136       // The following are defined in the specific vector, matrix-files
00137 #if(CXSC_INDEX_CHECK) 
00138 
00139       explicit INLINE real(const rvector &)       throw (ERROR_RVECTOR_TYPE_CAST_OF_THICK_OBJ,ERROR_RVECTOR_USE_OF_UNINITIALIZED_OBJ);
00141       explicit INLINE real(const rvector_slice &) throw (ERROR_RVECTOR_TYPE_CAST_OF_THICK_OBJ,ERROR_RVECTOR_USE_OF_UNINITIALIZED_OBJ);
00143       explicit INLINE real(const rmatrix &)       throw (ERROR_RMATRIX_TYPE_CAST_OF_THICK_OBJ,ERROR_RMATRIX_USE_OF_UNINITIALIZED_OBJ);
00145       explicit INLINE real(const rmatrix_slice &) throw (ERROR_RMATRIX_TYPE_CAST_OF_THICK_OBJ,ERROR_RMATRIX_USE_OF_UNINITIALIZED_OBJ);
00146 #else
00147 
00148       explicit INLINE real(const rvector &)       throw ();
00150       explicit INLINE real(const rvector_slice &) throw ();
00152       explicit INLINE real(const rmatrix &)       throw ();
00154       explicit INLINE real(const rmatrix_slice &) throw ();
00155 #endif
00156       
00157 
00158       // ---- assignments -----------------------------------------
00159 
00160       // ---- compatibility typecasts -----------------------------
00161 
00163       friend inline double _double(const real &a) throw();
00165       friend inline real   _real(const double &a) throw();
00166 
00167 #if(CXSC_INDEX_CHECK)
00168 
00169 
00174       friend INLINE real _real(const rvector &)       throw (ERROR_RVECTOR_TYPE_CAST_OF_THICK_OBJ,ERROR_RVECTOR_USE_OF_UNINITIALIZED_OBJ);
00176 
00181       friend INLINE real _real(const rvector_slice &) throw (ERROR_RVECTOR_TYPE_CAST_OF_THICK_OBJ,ERROR_RVECTOR_USE_OF_UNINITIALIZED_OBJ);
00183 
00188       friend INLINE real _real(const rmatrix &)       throw (ERROR_RMATRIX_TYPE_CAST_OF_THICK_OBJ,ERROR_RMATRIX_USE_OF_UNINITIALIZED_OBJ);
00190 
00195       friend INLINE real _real(const rmatrix_slice &) throw (ERROR_RMATRIX_TYPE_CAST_OF_THICK_OBJ,ERROR_RMATRIX_USE_OF_UNINITIALIZED_OBJ);
00196 #else
00197 
00198 
00203       friend INLINE real _real(const rvector &)       throw ();
00205 
00210       friend INLINE real _real(const rvector_slice &) throw ();
00212 
00217       friend INLINE real _real(const rmatrix &)       throw ();
00219 
00224       friend INLINE real _real(const rmatrix_slice &) throw ();
00225 #endif
00226       
00227       // ---- Input/Output  ---------------------------------------
00228 
00230       friend std::ostream & operator <<(std::ostream &,const real &) throw();
00232       friend std::istream & operator >>(std::istream &,real &)       throw();
00234       friend std::string &  operator <<(std::string &,const real &)  throw();
00236       friend std::string &  operator >>(std::string &,real &)        throw();
00238       friend void           operator >>(const char *,real &)         throw();
00240       friend void           operator >>(const std::string &,real &)  throw();
00241 
00242       // ---- Std.Operators ---------------------------------------
00243       // As the real-arithmetic should be as fast as double all
00244       // operators are inlined.                
00245 
00247       friend inline real operator -(const real &) throw ();
00249       friend inline real operator +(const real &) throw ();
00250 
00252       friend inline real operator +(const real &,const real &) throw();
00254       friend inline real operator -(const real &,const real &) throw();
00256       friend inline real operator *(const real &,const real &) throw();
00258       friend inline real operator /(const real &,const real &) throw();
00259 
00261       friend inline real& operator +=(real &, const real &) throw();
00263       friend inline real& operator -=(real &, const real &) throw();
00265       friend inline real& operator *=(real &, const real &) throw();
00267       friend inline real& operator /=(real &, const real &) throw();
00268 
00269       // ---- Comp.Operat.  ---------------------------------------
00270 
00272       friend inline bool operator!  (const real& a)                throw();
00274       friend inline bool operator== (const real& a, const real& b) throw();
00276       friend inline bool operator!= (const real& a, const real& b) throw();
00278       friend inline bool operator<  (const real& a, const real& b) throw();
00280       friend inline bool operator<= (const real& a, const real& b) throw();
00282       friend inline bool operator>= (const real& a, const real& b) throw();
00284       friend inline bool operator>  (const real& a, const real& b) throw();
00285 
00287       friend inline bool operator== (const real& a, const int & b) throw();
00289       friend inline bool operator!= (const real& a, const int & b) throw();
00291       friend inline bool operator== (const int & a, const real& b) throw();
00293       friend inline bool operator!= (const int & a, const real& b) throw();
00295       friend inline bool operator== (const real& a, const long & b) throw();
00297       friend inline bool operator!= (const real& a, const long & b) throw();
00299       friend inline bool operator== (const long & a, const real& b) throw();
00301       friend inline bool operator!= (const long & a, const real& b) throw();
00303       friend inline bool operator== (const real& a, const float & b) throw();
00305       friend inline bool operator!= (const real& a, const float & b) throw();
00307       friend inline bool operator== (const float & a, const real& b) throw();
00309       friend inline bool operator!= (const float & a, const real& b) throw();
00311       friend inline bool operator== (const real& a, const double & b) throw();
00313       friend inline bool operator!= (const real& a, const double & b) throw();
00315       friend inline bool operator== (const double & a, const real& b) throw();
00317       friend inline bool operator!= (const double & a, const real& b) throw();
00318    
00319       // ---- Rounding Operators ---------------------------------
00320 
00322       friend inline real addup(const real &, const real &);
00324       friend inline real adddown(const real &, const real &);
00326       friend inline real subup(const real &, const real &);
00328       friend inline real subdown(const real &, const real &);
00330       friend inline real multup(const real &, const real &);
00332       friend inline real multdown(const real &, const real &);
00334       friend inline real divup(const real &, const real &);
00336       friend inline real divdown(const real &, const real &);
00337 
00338       // ---- Others   -------------------------------------------
00339 
00341       friend inline real abs(const real &a) throw();
00343       friend inline int  sign(const real &) throw();
00344       
00346       friend inline real pred(const real &) throw();
00348       friend inline real succ(const real &) throw();
00350       friend inline a_intg expo(const real &) throw();
00352       friend inline real comp(const real &,a_intg) throw();
00354       friend inline real mant(const real &) throw();
00355                 
00356       real & operator = (const lx_real&) throw();  // Blomquist, 12.11.2008;
00357       real & operator = (const l_real&)  throw();  // Blomquist, 12.11.2008;
00358 }; // end of class real
00359 
00360 inline real comp(const real &,a_intg) throw();
00361 
00362 // ---------------------------------------------------------------------------
00363 // ----                                                                   ----
00364 // ---- friend functions of class real (not inline)                       ----
00365 // ----                                                                   ----
00366 // ---------------------------------------------------------------------------
00367 
00368 std::ostream & operator <<(std::ostream &,const real &) throw();
00369 std::istream & operator >>(std::istream &,real &)       throw();
00370 std::string &  operator <<(std::string &,const real &)  throw();
00371 std::string &  operator >>(std::string &,real &)        throw();
00372 void           operator >>(const char *,real &)         throw();
00373 void           operator >>(const std::string &,real &)  throw();
00374 
00375 // ---------------------------------------------------------------------------
00376 // ----                                                                   ----
00377 // ---- global functions associated with class real                       ----
00378 // ----                                                                   ----
00379 // ---------------------------------------------------------------------------
00380 
00382 inline real max(const real & a, const real & b);
00384 inline real min(const real & a, const real & b);
00386 inline real Max(const real & a, const real & b); 
00387 
00388 //----------------------------------------------------------------------
00389 // MakeHexReal - erstellt aus den binaer angegebenen Einzelteilen einer
00390 //               IEEE 64-bit Gleitkommazahl eine solche
00391 
00393 const real& MakeHexReal ( 
00394    int sign, unsigned int expo, a_btyp manthigh, a_btyp mantlow);
00395 
00397 inline bool IsInfinity(const real &a);
00399 inline bool IsQuietNaN(const real &a);
00401 inline bool IsSignalingNaN(const real &a);
00402 
00403 //-------------------------------------------------------------------------
00404 // times2pown - Fast multiplication of reference parameter r with 2^n -----
00406 inline void times2pown(real& r,const int n); // Blomquist 1.10.02. {real.inl}
00407 
00409 inline real pow2n(const int n) throw(); // returns 2^n; 
00410 
00412 string realToHex(const real& a);
00413 
00414 // ---------------------------------------------------------------------------
00415 // ----                                                                   ----
00416 // ---- special constants associated with class real                      ----
00417 // ----                                                                   ----
00418 // ---------------------------------------------------------------------------
00419 
00420 // ----  special constants -----------------------------------------
00422 extern const real MinReal;
00424 extern const real minreal;
00426 extern const real MaxReal;
00428 extern const real Infinity;
00430 extern const real SignalingNaN;
00432 extern const real QuietNaN; 
00434 extern const real Epsilon;
00435 extern const real Factor;
00436 
00438 extern const real Pi_real;        // Pi
00440 extern const real Pi2_real;       // 2*Pi
00442 extern const real Pi3_real;       // 3*Pi
00444 extern const real Pid2_real;      // Pi/2
00446 extern const real Pid3_real;      // Pi/3
00448 extern const real Pid4_real;      // Pi/4
00450 extern const real Pir_real;       // 1/Pi
00452 extern const real Pi2r_real;      // 1/(2*Pi)
00454 extern const real Pip2_real;      // Pi^2
00456 extern const real SqrtPi_real;    // sqrt(Pi)
00458 extern const real Sqrt2Pi_real;   // sqrt(2Pi)
00460 extern const real SqrtPir_real;   // 1/sqrt(Pi)
00462 extern const real Sqrt2Pir_real;  // 1/sqrt(2Pi)
00464 extern const real Sqrt2_real;     // sqrt(2)
00466 extern const real Sqrt5_real;     // sqrt(5)
00468 extern const real Sqrt7_real;     // sqrt(7)
00470 extern const real Sqrt2r_real;    // 1/sqrt(2)
00472 extern const real Sqrt3_real;     // sqrt(3)
00474 extern const real Sqrt3d2_real;   // sqrt(3)/2
00476 extern const real Sqrt3r_real;    // 1/sqrt(3)
00478 extern const real Ln2_real;       // ln(2)
00480 extern const real Ln2r_real;      // 1/ln(2)
00482 extern const real Ln10_real;      // ln(10)
00484 extern const real Ln10r_real;     // 1/ln(10)
00486 extern const real LnPi_real;      // ln(Pi)
00488 extern const real Ln2Pi_real;     // ln(2Pi)
00490 extern const real E_real;         // e
00492 extern const real Er_real;        // 1/e
00494 extern const real Ep2_real;       // e^2
00496 extern const real Ep2r_real;      // 1/e^2
00498 extern const real EpPi_real;      // e^(Pi)
00500 extern const real Ep2Pi_real;     // e^(2Pi)
00502 extern const real EpPid2_real;    // e^(Pi/2)
00504 extern const real EpPid4_real;    // e^(Pi/4)
00505 
00506 } // namespace cxsc 
00507 
00508 #include "real.inl"
00509 #include "rmath.hpp"
00510 // }
00511 #endif // _CXSC_REAL_HPP_INCLUDED
00512