World Ocean Simulation System (WOSS) library
custom-precision-double.h
Go to the documentation of this file.
1/* WOSS - World Ocean Simulation System -
2 *
3 * Copyright (C) 2009 Federico Guerra
4 * and regents of the SIGNET lab, University of Padova
5 *
6 * Author: Federico Guerra - federico@guerra-tlc.com
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation;
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22/*
23 * This software has been developed by Federico Guerra and SIGNET lab,
24 * University of Padova, in collaboration with the NATO Centre for
25 * Maritime Research and Experimentation (http://www.cmre.nato.int ;
26 * E-mail: pao@cmre.nato.int), whose support is gratefully acknowledged.
27 */
28
29
45#ifndef CUSTOM_PRECISION_DOUBLE_H
46#define CUSTOM_PRECISION_DOUBLE_H
47
48
49#include <iostream>
50#include <cmath>
51
52
53
54namespace woss {
55
56
60 #define PDOUBLE_DEFAULT_PRECISION (1.0e-17)
61
67 class PDouble {
68
69
70 public:
71
77 PDouble( const long double input = 0.0, const long double precision = PDOUBLE_DEFAULT_PRECISION );
78
83 PDouble( const PDouble& copy );
84
89 ~PDouble();
90
91
96 static void setDebug( bool flag ) { debug = flag; }
97
102 void setPrecision( double value ) { precision = value; }
103
104
109 long double getPrecision() const { return precision; }
110
115 long double getValue() const { return value; }
116
117
123 PDouble& operator=( const PDouble& copy );
124
125
130 operator int() const;
131
136 operator float() const;
137
142 operator double() const;
143
148 operator long double() const;
149
150
157 friend const PDouble operator+( const PDouble& left, const PDouble& right );
158
165 friend const PDouble operator-( const PDouble& left, const PDouble& right );
166
173 friend const PDouble operator/( const PDouble& left, const PDouble& right );
174
181 friend const PDouble operator*( const PDouble& left, const PDouble& right );
182
189 friend const PDouble operator%( const PDouble& left, const PDouble& right );
190
191
198 friend PDouble& operator+=( PDouble& left, const PDouble& right );
199
206 friend PDouble& operator-=( PDouble& left, const PDouble& right );
207
214 friend PDouble& operator/=( PDouble& left, const PDouble& right );
215
222 friend PDouble& operator*=( PDouble& left, const PDouble& right );
223
230 friend PDouble& operator%=( PDouble& left, const PDouble& right );
231
232
239 friend bool operator==( const PDouble& left, const PDouble& right );
240
247 friend bool operator!=( const PDouble& left, const PDouble& right );
248
249
256 friend bool operator>( const PDouble& left, const PDouble& right );
257
264 friend bool operator<( const PDouble& left, const PDouble& right );
265
272 friend bool operator>=( const PDouble& left, const PDouble& right );
273
280 friend bool operator<=( const PDouble& left, const PDouble& right );
281
282
289 friend ::std::ostream& operator<<( ::std::ostream& os, const PDouble& instance );
290
297 friend ::std::istream& operator>>( ::std::istream& is, PDouble& instance );
298
299
300
301 protected:
302
306 long double value;
307
311 long double precision;
312
313
317 static bool debug;
318
319 };
320
321
323 // inline functions
324
325 inline PDouble::operator double() const {
326 return( (double) value) ;
327 }
328
329
330 inline PDouble::operator int() const {
331 return( (int) value );
332 }
333
334
335 inline PDouble::operator float() const {
336 return( (float) value );
337 }
338
339
340 inline PDouble::operator long double() const {
341 return( (long double) value );
342 }
343
344
345 inline const PDouble operator+( const PDouble& left, const PDouble& right ) {
346 return( PDouble(left.value + right.value, ::std::max(left.precision, right.precision)) );
347 }
348
349
350 inline const PDouble operator-( const PDouble& left, const PDouble& right ) {
351 return( PDouble(left.value - right.value, ::std::max(left.precision, right.precision)) );
352 }
353
354
355 inline const PDouble operator/( const PDouble& left, const PDouble& right ) {
356 return( PDouble(left.value / right.value, ::std::max(left.precision, right.precision)) );
357 }
358
359
360 inline const PDouble operator*( const PDouble& left, const PDouble& right ) {
361 return( PDouble(left.value * right.value, ::std::max(left.precision, right.precision)) );
362 }
363
364
365 inline const PDouble operator%( const PDouble& left, const PDouble& right ) {
366 return( PDouble( ::std::fmod(left.value, right.value), ::std::max(left.precision, right.precision) ) );
367 }
368
369
370 inline PDouble& operator+=( PDouble& left, const PDouble& right ) {
371 // if ( &left == &right ) { /* self assignment code here */ }
372 left.value += right.value;
373 return left;
374 }
375
376
377 inline PDouble& operator-=( PDouble& left, const PDouble& right ) {
378 // if ( &left == &right ) { /* self assignment code here */ }
379 left.value -= right.value;
380 return left;
381 }
382
383
384 inline PDouble& operator/=( PDouble& left, const PDouble& right ) {
385 // if ( &left == &right ) { /* self assignment code here */ }
386 left.value /= right.value;
387 return left;
388 }
389
390
391 inline PDouble& operator*=( PDouble& left, const PDouble& right ) {
392 // if ( &left == &right ) { /* self assignment code here */ }
393 left.value *= right.value;
394 return left;
395 }
396
397
398 inline PDouble& operator%=( PDouble& left, const PDouble& right ) {
399 // if ( &left == &right ) { /* self assignment code here */ }
400 left.value = ::std::fmod(left.value, right.value);
401 return left;
402 }
403
404
405 inline bool operator==( const PDouble& left, const PDouble& right ) {
406 if ( &left == &right ) { return true; }
407 return( ::std::abs(left.value - right.value) <= left.precision );
408 }
409
410
411 inline bool operator!=( const PDouble& left, const PDouble& right ) {
412 if ( &left == &right ) { return false; }
413 return( ::std::abs(left.value - right.value) > left.precision );
414 }
415
416
417 inline bool operator>( const PDouble& left, const PDouble& right ) {
418 if ( left == right ) return false;
419 return( left.value > right.value );
420 }
421
422
423 inline bool operator<( const PDouble& left, const PDouble& right ) {
424 if ( left == right ) return false;
425 return( left.value < right.value );
426 }
427
428
429 inline bool operator>=( const PDouble& left, const PDouble& right ) {
430 if ( left == right ) return true;
431 return( left.value > right.value );
432 }
433
434
435 inline bool operator<=( const PDouble& left, const PDouble& right ) {
436 if ( left == right ) return true;
437 return( left.value < right.value );
438 }
439
440
441 inline ::std::ostream& operator<<( ::std::ostream& os, const PDouble& instance ) {
442 os << instance.value;
443 return os;
444 }
445
446
447 inline ::std::istream& operator>>( ::std::istream& is, PDouble& instance ) {
448 is >> instance.value;
449 return is;
450 }
451
452}
453
454#endif /* CUSTOM_PRECISION_DOUBLE_H */
455
bool operator!=(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.h:846
std::ostream & operator<<(std::ostream &os, const Altimetry &instance)
Definition altimetry-definitions.h:826
Altimetry & operator*=(Altimetry &left, double right)
Definition altimetry-definitions.cpp:367
const Altimetry operator/(const Altimetry &left, const double right)
Definition altimetry-definitions.cpp:285
bool operator==(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.h:840
Altimetry & operator+=(Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:327
const Altimetry operator+(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:257
const Altimetry operator*(const Altimetry &left, const double right)
Definition altimetry-definitions.cpp:292
Altimetry & operator-=(Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:335
const Altimetry operator-(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:264
Altimetry & operator/=(Altimetry &left, double right)
Definition altimetry-definitions.cpp:359
Custom precision long double class.
Definition custom-precision-double.h:67
long double getPrecision() const
Definition custom-precision-double.h:109
friend bool operator<=(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:435
long double precision
Definition custom-precision-double.h:311
void setPrecision(double value)
Definition custom-precision-double.h:102
friend bool operator!=(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:411
friend PDouble & operator-=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:377
friend::std::istream & operator>>(::std::istream &is, PDouble &instance)
friend const PDouble operator*(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:360
friend const PDouble operator-(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:350
friend const PDouble operator+(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:345
~PDouble()
Definition custom-precision-double.cpp:62
friend bool operator==(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:405
friend bool operator>=(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:429
friend PDouble & operator*=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:391
friend bool operator<(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:423
long double value
Definition custom-precision-double.h:306
friend::std::ostream & operator<<(::std::ostream &os, const PDouble &instance)
friend PDouble & operator%=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:398
PDouble & operator=(const PDouble &copy)
Definition custom-precision-double.cpp:68
friend const PDouble operator/(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:355
friend PDouble & operator/=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:384
friend PDouble & operator+=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:370
static void setDebug(bool flag)
Definition custom-precision-double.h:96
long double getValue() const
Definition custom-precision-double.h:115
static bool debug
Definition custom-precision-double.h:317
friend bool operator>(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:417
friend const PDouble operator%(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:365
bool operator<=(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:928
bool operator>=(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:922
bool operator<(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:916
bool operator>(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:910
const PDouble operator%(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:365
PDouble & operator%=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:398