C-XSC - A C++ Class Library for Extended Scientific Computing  2.5.4
lx_real.inl
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: lx_real.inl,v 1.8 2014/01/30 17:23:47 cxsc Exp $ */
25 
26 /*
27 ** F. Blomquist, University of Wuppertal, 19.09.2007;
28 */
29 
30 namespace cxsc {
31 
32 // --------------------------------------------------------------------------
33 // --------------------------------------------------------------------------
34 // --------- Inline functions and operators related to type lx_real ---------
35 // --------------------------------------------------------------------------
36 // --------------------------------------------------------------------------
37 
38  inline void times2pown(lx_real &a, const real &n) throw()
39 {
40  a = lx_real(add_real(n,a.ex),a.lr);
41 }
42 
43 inline std::string & operator << (std::string &s, const lx_real& a)
44  throw()
45 // The value of a variable a of type lx_real is copied to a string s.
46 // s has the form: {2**(ex)*lr}
47 {
48  std::stringstream ss;
49  string str;
50 
51  str = "{2**(";
52  s += str;
53  ss << SaveOpt << SetPrecision(0,0) << Fixed << a.ex << RestoreOpt;
54  ss >> str;
55  s += str;
56  s+=")*";
57  s << a.lr;
58  s+='}';
59  return s;
60 }
61 
62 
63  inline bool Is_Integer(const real& x)
64  // returns 1 if x is an integer value and
65  // if |x| <= 2^53 - 1 = 9007199254740991.0;
66  // otherwise 0 is returned
67 {
68  double dbl1,dbl2;
69  dbl1 = _double(x);
70  dbl2 = floor(dbl1);
71  if (dbl1 == dbl2 && fabs(dbl1) <= Max_Int_R) return 1;
72  else return 0;
73 }
74 
75  inline real add_real(const real &a, const real &b) throw()
76 {
77  real res(a+b);
78  if (abs(res)>Max_Int_R)
79  cxscthrow(REAL_INT_OUT_OF_RANGE(
80  "add_real(const real&, const real&)"));
81  return res;
82 }
83 
84  inline real sub_real(const real &a, const real &b) throw()
85 {
86  real res(a-b);
87  if (abs(res)>Max_Int_R)
88  cxscthrow(REAL_INT_OUT_OF_RANGE("sub_real(const real&, const real&)"));
89  return res;
90 }
91 
92  inline int StagPrec(const lx_real &a) throw()
93 { return StagPrec(a.lr); }
94 
95  inline real expo(const lx_real &a) throw()
96  { return (a.ex); }
97 
98  inline int sign(const lx_real &a) throw()
99  { return sign(a.lr); }
100 
101  inline l_real lr_part(const lx_real &a) throw()
102  { return (a.lr); }
103 
104  inline lx_real abs(const lx_real& a) throw()
105  { return lx_real(a.ex,abs(a.lr)); }
106 
107  inline lx_real adjust(const lx_real &a) throw()
108  { return lx_real(a.ex,adjust(a.lr)); }
109 
110  inline void times2pown_neg(lx_real& a, const real& n) throw()
111 // Calculating an approximation of a*2^n, n = 0,-1,-2,...,-9007199254740991.0;
112 // n MUST be an integer and n MUST not be positive!
113 // These conditions are not tested in this function!
114 // Blomquist, 09.11.2008;
115  {
116  int exal(expo_gr(a.lr));
117  real exa,d,n_d;
118  l_real lia(a.lr);
119 // int k;
120 
121  if (exal>-100000) // a != [0,0]
122  {
123  exa = a.ex;
124  if (exa < -Max_Int_R - n) // exa+n < -9007199254740991;
125  { // It holds: -Max_Int_R - n in {-Max_Int_R, ...,-1,0},
126  // Thus, -Max_Int_R - n is always in integer value.
127  // Furthermore it holds: exa in {-Max_Int_R,...,-2,-1}.
128  d = -Max_Int_R - exa; // d in {-Max_Int_R+1,..., -1,0}
129  n_d = n-d;
130  // With exa+n < -Max_Int_R and with exa+d = -Max_Int_R
131  // it follows: n-d < 0, and:
132  // n-d in {-Max_Int_R,-Max_Int_R+1, ... , -1};
133  // Thus, n-d is a negative and integer value.
134  if (n_d < -2100)
135  lia = 0.0;
136  else // -2100 <= n_d <0:
137  Times2pown(lia,n_d);
138  a = lx_real(-Max_Int_R,lia);
139  }
140  else // n+a.ex >= -9007199254740991, so an integer overflow
141  // is not possible here!
142  a = lx_real(n+a.ex,lia);
143  }
144  } // times2pown_neg(...)
145 
146 
147  inline lx_real & lx_real::operator = (const lx_real &a) throw()
148  {
149  ex = a.ex;
150  lr = a.lr;
151  return *this;
152  }
153 
154  inline lx_real & lx_real::operator = (const l_real &a) throw()
155  {
156  ex = 0;
157  lr = a;
158  return *this;
159  }
160 
161  inline lx_real & lx_real::operator = (const real &a) throw()
162  {
163  ex = 0;
164  lr = a;
165  return *this;
166  }
167 
168 // -----------------------------------------------------
169 
170  inline bool eq_zero(const lx_real &a) throw()
171  { return (a.lr == 0 ); }
172 
173  inline bool gr_zero(const lx_real &a) throw()
174  { return (a.lr > 0 ); }
175 
176  inline bool ge_zero(const lx_real &a) throw()
177  { return (a.lr >= 0); }
178 
179  inline bool sm_zero(const lx_real &a) throw()
180  { return (a.lr < 0 ); }
181 
182  inline bool se_zero(const lx_real &a) throw()
183  { return (a.lr <= 0); }
184 
185 // -----------------------------------------------------
186 
187  inline lx_real operator -(const lx_real &a) throw()
188  { return lx_real(a.ex,-a.lr); }
189 
190  inline lx_real operator +(const lx_real &a) throw()
191  { return a; }
192 
193  inline lx_real operator + (const lx_real& a, const l_real& b) throw()
194  { return a + lx_real(b); }
195  inline lx_real operator + (const l_real& a, const lx_real& b) throw()
196  { return lx_real(a) + b; }
197  inline lx_real operator + (const lx_real& a, const real& b) throw()
198  { return a + lx_real(l_real(b)); }
199  inline lx_real operator + (const real& a, const lx_real& b) throw()
200  { return lx_real(l_real(a)) + b; }
201 
202  inline lx_real & operator +=(lx_real& a, const lx_real& b) throw()
203  { return a = a+b; }
204  inline lx_real & operator +=(lx_real& a, const l_real& b) throw()
205  { return a = a+b; }
206  inline lx_real & operator +=(lx_real& a, const real& b) throw()
207  { return a = a+b; }
208 
209  inline lx_real operator - (const lx_real& a, const lx_real& b) throw()
210  { return a + lx_real(-b); }
211  inline lx_real operator - (const lx_real& a, const l_real& b) throw()
212  { return a + lx_real(-b); }
213  inline lx_real operator - (const l_real& a, const lx_real& b) throw()
214  { return lx_real(a) + lx_real(-b); }
215  inline lx_real operator - (const lx_real& a, const real& b) throw()
216  { return a + lx_real(-b); }
217  inline lx_real operator - (const real& a, const lx_real& b) throw()
218  { return lx_real(a) + lx_real(-b); }
219 
220  inline lx_real & operator -=(lx_real& a, const lx_real& b) throw()
221  { return a = a-b; }
222  inline lx_real & operator -=(lx_real& a, const l_real& b) throw()
223  { return a = a-b; }
224  inline lx_real & operator -=(lx_real& a, const real& b) throw()
225  { return a = a-b; }
226 
227  inline lx_real operator * (const lx_real& a, const l_real& b) throw()
228  { return a * lx_real(b); }
229  inline lx_real operator * (const l_real& a, const lx_real& b) throw()
230  { return lx_real(a) * b; }
231  inline lx_real operator * (const lx_real& a, const real& b) throw()
232  { return a * lx_real(b); }
233  inline lx_real operator * (const real& a, const lx_real& b) throw()
234  { return lx_real(a) * b; }
235 
236  inline lx_real & operator *=(lx_real& a, const lx_real& b) throw()
237  { return a = a*b; }
238  inline lx_real & operator *=(lx_real& a, const l_real& b) throw()
239  { return a = a*b; }
240  inline lx_real & operator *=(lx_real& a, const real& b) throw()
241  { return a = a*b; }
242 
243  inline lx_real operator / (const lx_real& a, const l_real& b) throw()
244  { return a / lx_real(b); }
245  inline lx_real operator / (const l_real& a, const lx_real& b) throw()
246  { return lx_real(a) / b; }
247  inline lx_real operator / (const lx_real& a, const real& b) throw()
248  { return a / lx_real(b); }
249  inline lx_real operator / (const real& a, const lx_real& b) throw()
250  { return lx_real(a) / b; }
251 
252  inline lx_real & operator /=(lx_real& a, const lx_real& b) throw()
253  { return a = a/b; }
254  inline lx_real & operator /=(lx_real& a, const l_real& b) throw()
255  { return a = a/b; }
256  inline lx_real & operator /=(lx_real& a, const real& b) throw()
257  { return a = a/b; }
258 
259  inline bool operator ! (const lx_real& a) throw()
260  { return !a.lr; }
261 
262  inline bool operator == (const lx_real &a, const l_real &b) throw()
263  { return (a==lx_real(b)); }
264 
265  inline bool operator == (const l_real &a, const lx_real &b) throw()
266  { return (lx_real(a)==b); }
267 
268  inline bool operator == (const lx_real &a, const real &b) throw()
269  { return (a==lx_real(b)); }
270 
271  inline bool operator == (const real &a, const lx_real &b) throw()
272  { return (lx_real(a)==b); }
273 
274 
275  inline bool operator != (const lx_real &a, const lx_real &b) throw()
276  { return !(a==b); }
277 
278  inline bool operator != (const lx_real &a, const l_real &b) throw()
279  { return !(a==lx_real(b)); }
280 
281  inline bool operator != (const l_real &a, const lx_real &b) throw()
282  { return !(lx_real(a)==b); }
283 
284  inline bool operator != (const lx_real &a, const real &b) throw()
285  { return !(a==lx_real(b)); }
286 
287  inline bool operator != (const real &a, const lx_real &b) throw()
288  { return !(lx_real(a)==b); }
289 
290  inline bool operator <= (const lx_real &a, const lx_real &b) throw()
291  { return !(a>b); }
292 
293  inline bool operator < (const lx_real &a, const lx_real &b) throw()
294  { return (b>a); }
295 
296  inline bool operator >= (const lx_real &a, const lx_real &b) throw()
297  { return !(a<b); }
298 
299 // ---------------------------------------------------------
300 
301  inline bool operator > (const real &a, const lx_real &b) throw()
302  { return lx_real(a)>b; }
303 
304  inline bool operator <= (const real &a, const lx_real &b) throw()
305  { return !(a>b); }
306 
307  inline bool operator < (const real &a, const lx_real &b) throw()
308  { return b>lx_real(a); }
309 
310  inline bool operator >= (const real &a, const lx_real &b) throw()
311  { return !(a<b); }
312 
313 // ---------------------------------------------------------
314 
315  inline bool operator > (const lx_real &a, const real &b) throw()
316  { return a>lx_real(b); }
317 
318  inline bool operator <= (const lx_real &a, const real &b) throw()
319  { return !(a>b); }
320 
321  inline bool operator < (const lx_real &a, const real &b) throw()
322  { return b>a; }
323 
324  inline bool operator >= (const lx_real &a, const real &b) throw()
325  { return !(a<b); }
326 
327 // ---------------------------------------------------------
328 
329  inline bool operator > (const l_real &a, const lx_real &b) throw()
330  { return lx_real(a)>b; }
331 
332  inline bool operator <= (const l_real &a, const lx_real &b) throw()
333  { return !(a>b); }
334 
335  inline bool operator < (const l_real &a, const lx_real &b) throw()
336  { return b>lx_real(a); }
337 
338  inline bool operator >= (const l_real &a, const lx_real &b) throw()
339  { return !(a<b); }
340 
341 // ---------------------------------------------------------
342 
343  inline bool operator > (const lx_real &a, const l_real &b) throw()
344  { return a>lx_real(b); }
345 
346  inline bool operator <= (const lx_real &a, const l_real &b) throw()
347  { return !(a>b); }
348 
349  inline bool operator < (const lx_real &a, const l_real &b) throw()
350  { return b>a; }
351 
352  inline bool operator >= (const lx_real &a, const l_real &b) throw()
353  { return !(a<b); }
354 
355 // -----------------------------------------------------
356 
357 
358  inline lx_real max(const lx_real& a, const lx_real& b)
359  { return (b>a)? b : a; }
360 
361  inline lx_real min(const lx_real& a, const lx_real& b)
362  { return (b>a)? a : b; }
363 
364  inline real cutint(const real& x) throw()
365 // y = cutint(x) delivers the truncated part y of x.
366 // If y is not an integer value then 9007199254740992.0
367 // is returned;
368 // For all integer values y it holds:
369 // |y| <= Max_Int_R := 9007199254740991.0
370 // Examples:
371 // y = cutint(-0.1); ---> y = 0;
372 // y = cutint(-123.5); ---> y = -123.0;
373 // y = cutint(+123.5); ---> y = +123.0;
374 // y = cutint(9007199254740991.8); ---> y = 9007199254740992.0;
375 // y = cutint(9007199254740992.0); ---> y = 9007199254740992.0;
376 // y = cutint(-1e20); ---> y = 9007199254740992.0;
377 // Blomquist, 26.05.2008;
378  {
379  real res(x);
380  double dbl;
381  bool neg;
382  neg = x<0;
383  if (neg) res = -res; // res = |x|
384  if (res>Max_Int_R) res = 9007199254740992.0;
385  else
386  {
387  dbl = _double(res);
388  dbl = floor(dbl);
389  res = real(dbl);
390  if (neg) res = -res;
391  }
392  return res;
393  }
394 
395 } // end namespace cxsc
cxsc::Is_Integer
bool Is_Integer(const real &x)
Returns 1 if x is an integer value and if .
Definition: lx_real.inl:63
cxsc::operator*=
cimatrix & operator*=(cimatrix &m, const cinterval &c)
Implementation of multiplication and allocation operation.
Definition: cimatrix.inl:1605
cxsc::expo_gr
int expo_gr(const l_interval &x)
Definition: l_interval.inl:522
cxsc::sub_real
real sub_real(const real &a, const real &b)
Returns a,b must be integers with .
Definition: lx_real.inl:84
cxsc::abs
ivector abs(const cimatrix_subv &mv)
Returns the absolute value of the matrix.
Definition: cimatrix.inl:737
cxsc::cutint
real cutint(const real &x)
Returns the truncated integer part of x.
Definition: lx_real.inl:364
cxsc::operator*
civector operator*(const cimatrix_subv &rv, const cinterval &s)
Implementation of multiplication operation.
Definition: cimatrix.inl:731
cxsc::operator/=
cimatrix & operator/=(cimatrix &m, const cinterval &c)
Implementation of division and allocation operation.
Definition: cimatrix.inl:1623
cxsc::times2pown
void times2pown(cinterval &x, int n)
Fast multiplication of reference parameter [z] with .
Definition: cimath.cpp:2059
cxsc
The namespace cxsc, providing all functionality of the class library C-XSC.
Definition: cdot.cpp:29
cxsc::operator+=
cdotprecision & operator+=(cdotprecision &cd, const l_complex &lc)
Implementation of standard algebraic addition and allocation operation.
Definition: cdot.inl:251
cxsc::l_real
The Multiple-Precision Data Type l_real.
Definition: l_real.hpp:77
cxsc::add_real
real add_real(const real &a, const real &b)
Returns a,b must be integers with .
Definition: lx_real.inl:75
cxsc::operator/
civector operator/(const cimatrix_subv &rv, const cinterval &s)
Implementation of division operation.
Definition: cimatrix.inl:730
cxsc::real
The Scalar Type real.
Definition: real.hpp:113