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 2025 Federico Guerra
4 * and regents of the SIGNET lab, University of Padova
5 *
6 * Author: Federico Guerra - WOSS@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
38#ifndef CUSTOM_PRECISION_DOUBLE_H
39#define CUSTOM_PRECISION_DOUBLE_H
40
41
42#include <iostream>
43#include <cmath>
44
45
46namespace woss {
47
51 static constexpr inline double PDOUBLE_DEFAULT_PRECISION = 1.0e-17;
52
58 class PDouble {
59
60
61 public:
62
68 constexpr PDouble( const long double input = 0.0, const long double precision = PDOUBLE_DEFAULT_PRECISION ) noexcept;
69
74 PDouble( const PDouble& copy ) noexcept = default;
75
80 PDouble( PDouble&& tmp ) noexcept = default;
81
86 ~PDouble() noexcept = default;
87
92 constexpr void setPrecision( double value ) noexcept { precision = value; }
93
98 constexpr long double getPrecision() const noexcept { return precision; }
99
104 constexpr long double getValue() const noexcept { return value; }
105
106
107 constexpr PDouble& operator=( const PDouble& copy ) noexcept = default;
108
109 constexpr PDouble& operator=( PDouble&& tmp ) noexcept = default;
110
115 constexpr operator int() const;
116
121 constexpr operator float() const;
122
127 constexpr operator double() const;
128
133 constexpr operator long double() const;
134
141 friend constexpr PDouble operator+( const PDouble& left, const PDouble& right );
142
149 friend constexpr PDouble operator-( const PDouble& left, const PDouble& right );
150
157 friend constexpr PDouble operator/( const PDouble& left, const PDouble& right );
158
165 friend constexpr PDouble operator*( const PDouble& left, const PDouble& right );
166
173 friend constexpr PDouble operator%( const PDouble& left, const PDouble& right );
174
181 friend constexpr PDouble& operator+=( PDouble& left, const PDouble& right );
182
189 friend constexpr PDouble& operator-=( PDouble& left, const PDouble& right );
190
197 friend constexpr PDouble& operator/=( PDouble& left, const PDouble& right );
198
205 friend constexpr PDouble& operator*=( PDouble& left, const PDouble& right );
206
213 friend constexpr PDouble& operator%=( PDouble& left, const PDouble& right );
214
221 friend constexpr bool operator==( const PDouble& left, const PDouble& right );
222
229 friend constexpr bool operator!=( const PDouble& left, const PDouble& right );
230
237 friend constexpr bool operator>( const PDouble& left, const PDouble& right );
238
245 friend constexpr bool operator<( const PDouble& left, const PDouble& right );
246
253 friend constexpr bool operator>=( const PDouble& left, const PDouble& right );
254
261 friend constexpr bool operator<=( const PDouble& left, const PDouble& right );
262
269 friend std::ostream& operator<<( std::ostream& os, const PDouble& instance );
270
277 friend std::istream& operator>>( std::istream& is, PDouble& instance );
278
279 protected:
280
284 long double value;
285
289 long double precision;
290
291 };
292
293
295 // inline functions
296 inline constexpr PDouble::PDouble( const long double input, const long double prec ) noexcept
297 : value(input),
298 precision(prec)
299 {
300
301 }
302
303 inline constexpr PDouble::operator double() const {
304 return(static_cast<double>(value)) ;
305 }
306
307
308 inline constexpr PDouble::operator int() const {
309 return(static_cast<int>(value));
310 }
311
312
313 inline constexpr PDouble::operator float() const {
314 return(static_cast<float>(value));
315 }
316
317
318 inline constexpr PDouble::operator long double() const {
319 return(static_cast<long double>(value));
320 }
321
322
323 inline constexpr PDouble operator+( const PDouble& left, const PDouble& right ) {
324 return( PDouble(left.value + right.value, std::max(left.precision, right.precision)) );
325 }
326
327
328 inline constexpr PDouble operator-( const PDouble& left, const PDouble& right ) {
329 return( PDouble(left.value - right.value, std::max(left.precision, right.precision)) );
330 }
331
332
333 inline constexpr PDouble operator/( const PDouble& left, const PDouble& right ) {
334 return( PDouble(left.value / right.value, std::max(left.precision, right.precision)) );
335 }
336
337
338 inline constexpr PDouble operator*( const PDouble& left, const PDouble& right ) {
339 return( PDouble(left.value * right.value, std::max(left.precision, right.precision)) );
340 }
341
342
343 inline constexpr PDouble operator%( const PDouble& left, const PDouble& right ) {
344 return( PDouble( std::fmod(left.value, right.value), std::max(left.precision, right.precision) ) );
345 }
346
347
348 inline constexpr PDouble& operator+=( PDouble& left, const PDouble& right ) {
349 left.value += right.value;
350 return left;
351 }
352
353
354 inline constexpr PDouble& operator-=( PDouble& left, const PDouble& right ) {
355 left.value -= right.value;
356 return left;
357 }
358
359
360 inline constexpr PDouble& operator/=( PDouble& left, const PDouble& right ) {
361 left.value /= right.value;
362 return left;
363 }
364
365
366 inline constexpr PDouble& operator*=( PDouble& left, const PDouble& right ) {
367 left.value *= right.value;
368 return left;
369 }
370
371
372 inline constexpr PDouble& operator%=( PDouble& left, const PDouble& right ) {
373 left.value = std::fmod(left.value, right.value);
374 return left;
375 }
376
377
378 inline constexpr bool operator==( const PDouble& left, const PDouble& right ) {
379 if ( &left == &right )
380 return true;
381 return( std::abs(left.value - right.value) <= left.precision );
382 }
383
384
385 inline constexpr bool operator!=( const PDouble& left, const PDouble& right ) {
386 if ( &left == &right )
387 return false;
388 return( std::abs(left.value - right.value) > left.precision );
389 }
390
391
392 inline constexpr bool operator>( const PDouble& left, const PDouble& right ) {
393 if ( left == right )
394 return false;
395 return( left.value > right.value );
396 }
397
398
399 inline constexpr bool operator<( const PDouble& left, const PDouble& right ) {
400 if ( left == right )
401 return false;
402 return( left.value < right.value );
403 }
404
405
406 inline constexpr bool operator>=( const PDouble& left, const PDouble& right ) {
407 if ( left == right )
408 return true;
409 return( left.value > right.value );
410 }
411
412
413 inline constexpr bool operator<=( const PDouble& left, const PDouble& right ) {
414 if ( left == right )
415 return true;
416 return( left.value < right.value );
417 }
418
419
420 inline std::ostream& operator<<( std::ostream& os, const PDouble& instance ) {
421 os << instance.value;
422 return os;
423 }
424
425
426 inline std::istream& operator>>( std::istream& is, PDouble& instance ) {
427 is >> instance.value;
428 return is;
429 }
430
431}
432
433#endif /* CUSTOM_PRECISION_DOUBLE_H */
434
Custom precision long double class.
Definition custom-precision-double.h:58
PDouble(const PDouble &copy) noexcept=default
friend constexpr bool operator<(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:399
long double precision
Definition custom-precision-double.h:289
friend constexpr PDouble & operator%=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:372
friend constexpr PDouble operator%(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:343
constexpr long double getPrecision() const noexcept
Definition custom-precision-double.h:98
friend std::istream & operator>>(std::istream &is, PDouble &instance)
Definition custom-precision-double.h:426
friend constexpr PDouble operator/(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:333
friend constexpr bool operator>=(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:406
constexpr long double getValue() const noexcept
Definition custom-precision-double.h:104
constexpr void setPrecision(double value) noexcept
Definition custom-precision-double.h:92
friend constexpr PDouble & operator/=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:360
friend constexpr PDouble operator-(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:328
friend constexpr bool operator!=(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:385
constexpr PDouble & operator=(const PDouble &copy) noexcept=default
constexpr PDouble & operator=(PDouble &&tmp) noexcept=default
long double value
Definition custom-precision-double.h:284
friend constexpr bool operator>(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:392
friend constexpr PDouble operator+(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:323
friend constexpr PDouble & operator-=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:354
friend constexpr PDouble operator*(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:338
friend std::ostream & operator<<(std::ostream &os, const PDouble &instance)
Definition custom-precision-double.h:420
constexpr PDouble(const long double input=0.0, const long double precision=PDOUBLE_DEFAULT_PRECISION) noexcept
Definition custom-precision-double.h:296
friend constexpr PDouble & operator*=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:366
friend constexpr bool operator<=(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:413
friend constexpr PDouble & operator+=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:348
~PDouble() noexcept=default
friend constexpr bool operator==(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:378
PDouble(PDouble &&tmp) noexcept=default
Definition ac-toolbox-arr-asc-reader.h:44
constexpr PDouble operator%(const PDouble &left, const PDouble &right)
Definition custom-precision-double.h:343
constexpr bool operator>(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:1241
constexpr bool operator<(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:1248
bool operator!=(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.h:833
constexpr bool operator<=(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:1262
std::ostream & operator<<(std::ostream &os, const Altimetry &instance)
Definition altimetry-definitions.h:812
Altimetry & operator*=(Altimetry &left, double right)
Definition altimetry-definitions.cpp:346
const Altimetry operator/(const Altimetry &left, const double right)
Definition altimetry-definitions.cpp:264
bool operator==(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.h:826
Altimetry & operator+=(Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:306
constexpr bool operator>=(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:1255
const Altimetry operator+(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:236
const Altimetry operator*(const Altimetry &left, const double right)
Definition altimetry-definitions.cpp:271
constexpr PDouble & operator%=(PDouble &left, const PDouble &right)
Definition custom-precision-double.h:372
Altimetry & operator-=(Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:314
const Altimetry operator-(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:243
std::istream & operator>>(std::istream &is, PDouble &instance)
Definition custom-precision-double.h:426
Altimetry & operator/=(Altimetry &left, double right)
Definition altimetry-definitions.cpp:338