C-XSC - A C++ Class Library for Extended Scientific Computing  2.5.4
real.hpp
1 /*
2 ** CXSC is a C++ library for eXtended Scientific Computing (V 2.5.4)
3 **
4 ** Copyright (C) 1990-2000 Institut fuer Angewandte Mathematik,
5 ** Universitaet Karlsruhe, Germany
6 ** (C) 2000-2014 Wiss. Rechnen/Softwaretechnologie
7 ** Universitaet Wuppertal, Germany
8 **
9 ** This library is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU Library General Public
11 ** License as published by the Free Software Foundation; either
12 ** version 2 of the License, or (at your option) any later version.
13 **
14 ** This library is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** Library General Public License for more details.
18 **
19 ** You should have received a copy of the GNU Library General Public
20 ** License along with this library; if not, write to the Free
21 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 
24 /* CVS $Id: real.hpp,v 1.42 2014/01/30 17:23:48 cxsc Exp $ */
25 
26 #ifndef _CXSC_REAL_HPP_INCLUDED
27 #define _CXSC_REAL_HPP_INCLUDED
28 
29 #include <iostream>
30 #include <string>
31 
32 // namespace cxsc {
33 
34 #include "compiler.h"
35 #include "RtsTyp.h"
36 #include "ioflags.hpp"
37 
38 #include "except.hpp"
40 
44 namespace cxsc {
45 
46 class rvector;
47 class rmatrix;
48 class rvector_slice;
49 class rmatrix_slice;
50 
51 #define addu addup
52 #define addd adddown
53 #define subu subup
54 #define subd subdown
55 #define mulu multup
56 #define muld multdown
57 #define divu divup
58 #define divd divdown
59 
60 // ---------------------------------------------------------------------------
61 // ---- ----
62 // ---- class real (declaration) ----
63 // ---- ----
64 // ---------------------------------------------------------------------------
65 
67 
113 class real
114 {
115  private:
116  // ---- private data ----------------------------------------
117  double w;
118 
119  public:
120  // ---- implicit constructors -------------------------------
122  real(void) throw () { }
124  real(const float &a) throw () : w(a) { }
126  real(const double &a) throw () : w(a) { }
128  real(const int a) throw () : w(a) { }
130  real(const long a) throw () : w(a) { }
131 
132  // ---- explicit constructors -------------------------------
134  explicit real(const l_real &) throw();
135 
136  // The following are defined in the specific vector, matrix-files
137 #if(CXSC_INDEX_CHECK)
138  explicit INLINE real(const rvector &) throw (ERROR_RVECTOR_TYPE_CAST_OF_THICK_OBJ,ERROR_RVECTOR_USE_OF_UNINITIALIZED_OBJ);
141  explicit INLINE real(const rvector_slice &) throw (ERROR_RVECTOR_TYPE_CAST_OF_THICK_OBJ,ERROR_RVECTOR_USE_OF_UNINITIALIZED_OBJ);
143  explicit INLINE real(const rmatrix &) throw (ERROR_RMATRIX_TYPE_CAST_OF_THICK_OBJ,ERROR_RMATRIX_USE_OF_UNINITIALIZED_OBJ);
145  explicit INLINE real(const rmatrix_slice &) throw (ERROR_RMATRIX_TYPE_CAST_OF_THICK_OBJ,ERROR_RMATRIX_USE_OF_UNINITIALIZED_OBJ);
146 #else
147  explicit INLINE real(const rvector &) throw ();
150  explicit INLINE real(const rvector_slice &) throw ();
152  explicit INLINE real(const rmatrix &) throw ();
154  explicit INLINE real(const rmatrix_slice &) throw ();
155 #endif
156 
157 
158  // ---- assignments -----------------------------------------
159 
160  // ---- compatibility typecasts -----------------------------
161 
163  friend inline double _double(const real &a) throw();
165  friend inline real _real(const double &a) throw();
166 
167 #if(CXSC_INDEX_CHECK)
168 
174  friend INLINE real _real(const rvector &) throw (ERROR_RVECTOR_TYPE_CAST_OF_THICK_OBJ,ERROR_RVECTOR_USE_OF_UNINITIALIZED_OBJ);
176 
181  friend INLINE real _real(const rvector_slice &) throw (ERROR_RVECTOR_TYPE_CAST_OF_THICK_OBJ,ERROR_RVECTOR_USE_OF_UNINITIALIZED_OBJ);
183 
188  friend INLINE real _real(const rmatrix &) throw (ERROR_RMATRIX_TYPE_CAST_OF_THICK_OBJ,ERROR_RMATRIX_USE_OF_UNINITIALIZED_OBJ);
190 
195  friend INLINE real _real(const rmatrix_slice &) throw (ERROR_RMATRIX_TYPE_CAST_OF_THICK_OBJ,ERROR_RMATRIX_USE_OF_UNINITIALIZED_OBJ);
196 #else
197 
203  friend INLINE real _real(const rvector &) throw ();
205 
210  friend INLINE real _real(const rvector_slice &) throw ();
212 
217  friend INLINE real _real(const rmatrix &) throw ();
219 
224  friend INLINE real _real(const rmatrix_slice &) throw ();
225 #endif
226 
227  // ---- Input/Output ---------------------------------------
228 
230  friend std::ostream & operator <<(std::ostream &,const real &) throw();
232  friend std::istream & operator >>(std::istream &,real &) throw();
234  friend std::string & operator <<(std::string &,const real &) throw();
236  friend std::string & operator >>(std::string &,real &) throw();
238  friend void operator >>(const char *,real &) throw();
240  friend void operator >>(const std::string &,real &) throw();
241 
242  // ---- Std.Operators ---------------------------------------
243  // As the real-arithmetic should be as fast as double all
244  // operators are inlined.
245 
247  friend inline real operator -(const real &) throw ();
249  friend inline real operator +(const real &) throw ();
250 
252  friend inline real operator +(const real &,const real &) throw();
254  friend inline real operator -(const real &,const real &) throw();
256  friend inline real operator *(const real &,const real &) throw();
258  friend inline real operator /(const real &,const real &) throw();
259 
261  friend inline real& operator +=(real &, const real &) throw();
263  friend inline real& operator -=(real &, const real &) throw();
265  friend inline real& operator *=(real &, const real &) throw();
267  friend inline real& operator /=(real &, const real &) throw();
268 
269  // ---- Comp.Operat. ---------------------------------------
270 
272  friend inline bool operator! (const real& a) throw();
274  friend inline bool operator== (const real& a, const real& b) throw();
276  friend inline bool operator!= (const real& a, const real& b) throw();
278  friend inline bool operator< (const real& a, const real& b) throw();
280  friend inline bool operator<= (const real& a, const real& b) throw();
282  friend inline bool operator>= (const real& a, const real& b) throw();
284  friend inline bool operator> (const real& a, const real& b) throw();
285 
287  friend inline bool operator== (const real& a, const int & b) throw();
289  friend inline bool operator!= (const real& a, const int & b) throw();
291  friend inline bool operator== (const int & a, const real& b) throw();
293  friend inline bool operator!= (const int & a, const real& b) throw();
295  friend inline bool operator== (const real& a, const long & b) throw();
297  friend inline bool operator!= (const real& a, const long & b) throw();
299  friend inline bool operator== (const long & a, const real& b) throw();
301  friend inline bool operator!= (const long & a, const real& b) throw();
303  friend inline bool operator== (const real& a, const float & b) throw();
305  friend inline bool operator!= (const real& a, const float & b) throw();
307  friend inline bool operator== (const float & a, const real& b) throw();
309  friend inline bool operator!= (const float & a, const real& b) throw();
311  friend inline bool operator== (const real& a, const double & b) throw();
313  friend inline bool operator!= (const real& a, const double & b) throw();
315  friend inline bool operator== (const double & a, const real& b) throw();
317  friend inline bool operator!= (const double & a, const real& b) throw();
318 
319  // ---- Rounding Operators ---------------------------------
320 
322  friend inline real addup(const real &, const real &);
324  friend inline real adddown(const real &, const real &);
326  friend inline real subup(const real &, const real &);
328  friend inline real subdown(const real &, const real &);
330  friend inline real multup(const real &, const real &);
332  friend inline real multdown(const real &, const real &);
334  friend inline real divup(const real &, const real &);
336  friend inline real divdown(const real &, const real &);
337 
338  // ---- Others -------------------------------------------
339 
341  friend inline real abs(const real &a) throw();
343  friend inline int sign(const real &) throw();
344 
346  friend inline real pred(const real &) throw();
348  friend inline real succ(const real &) throw();
350  friend inline a_intg expo(const real &) throw();
352  friend inline real comp(const real &,a_intg) throw();
354  friend inline real mant(const real &) throw();
355 
356  real & operator = (const lx_real&) throw(); // Blomquist, 12.11.2008;
357  real & operator = (const l_real&) throw(); // Blomquist, 12.11.2008;
358 }; // end of class real
359 
360 inline real comp(const real &,a_intg) throw();
361 
362 // ---------------------------------------------------------------------------
363 // ---- ----
364 // ---- friend functions of class real (not inline) ----
365 // ---- ----
366 // ---------------------------------------------------------------------------
367 
368 std::ostream & operator <<(std::ostream &,const real &) throw();
369 std::istream & operator >>(std::istream &,real &) throw();
370 std::string & operator <<(std::string &,const real &) throw();
371 std::string & operator >>(std::string &,real &) throw();
372 void operator >>(const char *,real &) throw();
373 void operator >>(const std::string &,real &) throw();
374 
375 // ---------------------------------------------------------------------------
376 // ---- ----
377 // ---- global functions associated with class real ----
378 // ---- ----
379 // ---------------------------------------------------------------------------
380 
382 inline real max(const real & a, const real & b);
384 inline real min(const real & a, const real & b);
386 inline real Max(const real & a, const real & b);
387 
388 //----------------------------------------------------------------------
389 // MakeHexReal - erstellt aus den binaer angegebenen Einzelteilen einer
390 // IEEE 64-bit Gleitkommazahl eine solche
391 
393 const real& MakeHexReal (
394  int sign, unsigned int expo, a_btyp manthigh, a_btyp mantlow);
395 
397 inline bool IsInfinity(const real &a);
399 inline bool IsQuietNaN(const real &a);
401 inline bool IsSignalingNaN(const real &a);
402 
403 //-------------------------------------------------------------------------
404 // times2pown - Fast multiplication of reference parameter r with 2^n -----
406 inline void times2pown(real& r,const int n); // Blomquist 1.10.02. {real.inl}
407 
409 inline real pow2n(const int n) throw(); // returns 2^n;
410 
412 string realToHex(const real& a);
413 
414 // ---------------------------------------------------------------------------
415 // ---- ----
416 // ---- special constants associated with class real ----
417 // ---- ----
418 // ---------------------------------------------------------------------------
419 
420 // ---- special constants -----------------------------------------
422 extern const real MinReal;
424 extern const real minreal;
426 extern const real MaxReal;
428 extern const real Infinity;
430 extern const real SignalingNaN;
432 extern const real QuietNaN;
434 extern const real Epsilon;
435 extern const real Factor;
436 
438 extern const real Pi_real; // Pi
440 extern const real Pi2_real; // 2*Pi
442 extern const real Pi3_real; // 3*Pi
444 extern const real Pid2_real; // Pi/2
446 extern const real Pid3_real; // Pi/3
448 extern const real Pid4_real; // Pi/4
450 extern const real Pir_real; // 1/Pi
452 extern const real Pi2r_real; // 1/(2*Pi)
454 extern const real Pip2_real; // Pi^2
456 extern const real SqrtPi_real; // sqrt(Pi)
458 extern const real Sqrt2Pi_real; // sqrt(2Pi)
460 extern const real SqrtPir_real; // 1/sqrt(Pi)
462 extern const real Sqrt2Pir_real; // 1/sqrt(2Pi)
464 extern const real Sqrt2_real; // sqrt(2)
466 extern const real Sqrt5_real; // sqrt(5)
468 extern const real Sqrt7_real; // sqrt(7)
470 extern const real Sqrt2r_real; // 1/sqrt(2)
472 extern const real Sqrt3_real; // sqrt(3)
474 extern const real Sqrt3d2_real; // sqrt(3)/2
476 extern const real Sqrt3r_real; // 1/sqrt(3)
478 extern const real Ln2_real; // ln(2)
480 extern const real Ln2r_real; // 1/ln(2)
482 extern const real Ln10_real; // ln(10)
484 extern const real Ln10r_real; // 1/ln(10)
486 extern const real LnPi_real; // ln(Pi)
488 extern const real Ln2Pi_real; // ln(2Pi)
490 extern const real E_real; // e
492 extern const real Er_real; // 1/e
494 extern const real Ep2_real; // e^2
496 extern const real Ep2r_real; // 1/e^2
498 extern const real EpPi_real; // e^(Pi)
500 extern const real Ep2Pi_real; // e^(2Pi)
502 extern const real EpPid2_real; // e^(Pi/2)
504 extern const real EpPid4_real; // e^(Pi/4)
505 
506 } // namespace cxsc
507 
508 #include "real.inl"
509 #include "rmath.hpp"
510 // }
511 #endif // _CXSC_REAL_HPP_INCLUDED
512 
friend real mant(const real &)
The mantissa of a real value.
friend real adddown(const real &, const real &)
Addition of two real values and rounding the result downwards.
The Data Type rmatrix_slice.
Definition: rmatrix.hpp:1442
const real SqrtPir_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:86
const real Ln2Pi_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:100
friend bool operator==(const real &a, const real &b)
Implementation of standard equality operation.
The Multiple-Precision Data Type l_real.
Definition: l_real.hpp:77
friend bool operator>(const real &a, const real &b)
Implementation of standard greater-than operation.
friend real operator*(const real &, const real &)
Implementation of standard algebraic multiplication operation.
friend real pred(const real &)
The predecessor of a real value.
bool IsInfinity(const real &a)
Returns if the given real value represents the value infinity.
const real Sqrt5_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:89
const real EpPi_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:105
bool IsQuietNaN(const real &a)
Returns if the given real value represents the value of a quiet NaN.
bool IsSignalingNaN(const real &a)
Returns if the given real value represents the value of a signaling NaN.
The namespace cxsc, providing all functionality of the class library C-XSC.
Definition: cdot.cpp:29
friend bool operator<(const real &a, const real &b)
Implementation of standard less-than operation.
friend real operator-(const real &)
Implementation of standard algebraic negative sign operation.
friend real operator/(const real &, const real &)
Implementation of standard algebraic division operation.
friend real abs(const real &a)
The absolute value of a real value.
const real Epsilon
Machine epsilon.
Definition: real.cpp:69
real(const long a)
Constructor of class real.
Definition: real.hpp:130
friend bool operator!=(const real &a, const real &b)
Implementation of standard negated equality operation.
friend real subdown(const real &, const real &)
Subtraction of two real values and rounding the result downwards.
const real Pid3_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:79
const real Ln10_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:97
const real Er_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:102
const real Sqrt3r_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:94
const real Ln2r_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:96
friend real operator+(const real &)
Implementation of standard algebraic positive sign operation.
const real Sqrt2Pir_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:87
real(const double &a)
Constructor of class real.
Definition: real.hpp:126
friend real & operator/=(real &, const real &)
Implementation of standard algebraic division and allocation operation.
friend std::istream & operator>>(std::istream &, real &)
Implementation of standard input method.
Definition: realio.cpp:263
const real Pi2_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:76
friend real divdown(const real &, const real &)
Division of two real values and rounding the result downwards.
The Data Type rvector_slice.
Definition: rvector.hpp:1063
const real Sqrt7_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:90
friend bool operator>=(const real &a, const real &b)
Implementation of standard greater-or-equal-than operation.
friend real comp(const real &, a_intg)
Composes an IEEE floating point value out of an given mantissa and exponent.
The Data Type rvector.
Definition: rvector.hpp:57
const real EpPid4_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:108
const real MaxReal
Greatest representable floating-point number.
Definition: real.cpp:65
const real Ep2_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:103
const real Ln2_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:95
real(const int a)
Constructor of class real.
Definition: real.hpp:128
const real Pi2r_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:82
const real Sqrt3d2_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:93
friend a_intg expo(const real &)
The exponent of a real value.
const real Infinity
Representation of positive infinity in floating-point format.
Definition: real.cpp:66
const real EpPid2_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:107
const real Sqrt2r_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:91
friend real multdown(const real &, const real &)
Multiplication of two real values and rounding the result downwards.
friend real divup(const real &, const real &)
Division of two real values and rounding the result upwards.
The Data Type rmatrix.
Definition: rmatrix.hpp:470
const real Pip2_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:83
const real Pid2_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:78
friend real multup(const real &, const real &)
Multiplication of two real values and rounding the result upwards.
real Max(const real &a, const real &b)
Returns the greater value of two real values (for Compatibility with former r_util....
real pow2n(const int n)
Returns the value of .
friend bool operator!(const real &a)
Implementation of standard negation operation.
const real SqrtPi_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:84
friend double _double(const real &a)
Typecast to convert a real value into a double value.
const real & MakeHexReal(int sign, unsigned int expo, a_btyp manthigh, a_btyp mantlow)
Produces an IEEE 64-bit floating-point number from given binary coded parts of an IEEE 64-bit floatin...
Definition: real.cpp:52
const real Sqrt2Pi_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:85
const real Ln10r_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:98
const real Ep2r_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:104
const real Pi_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:75
real(void)
Constructor of class real.
Definition: real.hpp:122
void times2pown(cinterval &x, int n)
Fast multiplication of reference parameter [z] with .
Definition: cimath.cpp:2059
const real MinReal
Smallest normalized representable floating-point number.
Definition: real.cpp:62
const real Pi3_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:77
const real minreal
Smallest positive denormalized representable floating-point number.
Definition: real.cpp:63
friend real & operator+=(real &, const real &)
Implementation of standard algebraic addition and allocation operation.
friend real & operator-=(real &, const real &)
Implementation of standard algebraic subtraction and allocation operation.
const real QuietNaN
Representation of Not-a-Number in floating-point format.
Definition: real.cpp:68
const real Sqrt3_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:92
const real LnPi_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:99
string realToHex(const real &a)
Returns a real number in hexadecimal format as string.
Definition: realio.cpp:58
friend std::ostream & operator<<(std::ostream &, const real &)
Implementation of standard output method.
Definition: realio.cpp:185
const real Ep2Pi_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:106
const real Pir_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:81
friend int sign(const real &)
The sign of a real value.
friend real & operator*=(real &, const real &)
Implementation of standard algebraic multiplication and allocation operation.
The Scalar Type real.
Definition: real.hpp:113
friend real subup(const real &, const real &)
Subtraction of two real values and rounding the result upwards.
const real E_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:101
const real SignalingNaN
Not defined result in floating-point format.
Definition: real.cpp:67
real(const float &a)
Constructor of class real.
Definition: real.hpp:124
friend real succ(const real &)
The successor of a real value.
const real Pid4_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:80
friend real _real(const double &a)
Deprecated typecast, which only exist for the reason of compatibility with older versions of C-XSC.
friend bool operator<=(const real &a, const real &b)
Implementation of standard less-or-equal-than operation.
const real Sqrt2_real
Constant for rounded to the nearest machine number.
Definition: real.cpp:88
friend real addup(const real &, const real &)
Addition of two real values and rounding the result upwards.