Arcane  4.2.3.0
User documentation
Loading...
Searching...
No Matches
Arcane::HPReal Class Reference

Class implementing a High-Precision real number. More...

#include <arccore/base/HPReal.h>

Public Member Functions

constexpr HPReal ()
 Default constructor to 0.0.
constexpr HPReal (NoInitTag)
 Constructor WITHOUT initialization.
constexpr HPReal (double avalue)
 Creates an HP real with the value value and the correction correction.
constexpr HPReal (double avalue, double acorrection)
 Creates an HP real with the value value and the correction correction.
constexpr Real value () const
 Internal value. Generally, you must use toReal().
constexpr Real correction () const
 Internal correction.
constexpr void operator+= (Real v)
 Adds a Real while preserving the error.
constexpr void operator+= (HPReal v)
 Adds an HPReal v while preserving the error (reduction).
constexpr Real toReal () const
 Converts the instance to a Real.
constexpr HPReal reduce (HPReal b) const
 Adds an HPReal v while preserving the error (reduction).
constexpr void operator*= (Real v)
 Multiplies a Real while preserving the error.
constexpr void operator*= (HPReal v)
 Multiplies an HPReal v while preserving the error (reduction).
constexpr void operator/= (Real v)
 Multiplies a Real while preserving the error.
constexpr void operator/= (HPReal v)
 Multiplies an HPReal v while preserving the error (reduction).
std::istream & assign (std::istream &i)
 Reads an HPReal from the stream i. The pair is read in the form of two Real type values.
std::ostream & print (std::ostream &o) const
 Writes the instance to the stream o readable by an assign().
std::ostream & printPretty (std::ostream &o) const
 Writes the instance to the stream o in the form (x,y).

Static Public Member Functions

static constexpr HPReal zero ()
 Zero value.
static constexpr HPReal accumulate (Real a, HPReal b)
static constexpr HPReal reduce (HPReal a, HPReal b)
static constexpr HPReal product (HPReal a, HPReal b)
static constexpr HPReal product (Real a, HPReal b)
static constexpr HPReal div2 (HPReal a, HPReal b)
static constexpr HPReal div2 (Real b, HPReal a)

Friends

constexpr bool operator< (const HPReal &a, const HPReal &b)
constexpr bool operator> (const HPReal &a, const HPReal &b)
constexpr bool operator== (const HPReal &a, const HPReal &b)
constexpr bool operator!= (const HPReal &a, const HPReal &b)
constexpr HPReal operator+ (const HPReal &a, const HPReal &b)
std::ostream & operator<< (std::ostream &o, HPReal t)
std::istream & operator>> (std::istream &i, HPReal &t)

Detailed Description

Class implementing a High-Precision real number.

This class is based on the article:

Obtaining identical results with double precision global accuracy on different numbers of processors in parallel particle Monte Carlo simulations (Mathew A. Cleveland, Thomas A. Brunner, Nicholas A. Gentile, jeffrey A. Keasler) in Journal Of Computational Physics 251 (2013) 223-236.

Possible operations are accumulation (operator+=()), reduction (reduce()) and conversion to a Real (toReal()). Other operations such as division or multiplication are not possible.

To conform to the classic 'Real' type, the default constructor of this class performs no initialization.

The toReal() method allows converting the HPReal into a classic Real. The typical usage is as follows:

HPReal r(0.0);
for( Integer i=0, n=x.size(); i<n; ++i ){
// Accumulates the value
r += x[i];
}
Real final_r = r.toReal();
Modifiable view of an array of type T.
constexpr Integer size() const noexcept
Returns the size of the array.
constexpr HPReal()
Default constructor to 0.0.
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.

Evolution 12/18/18:

Correction of the base algorithm error for static HPReal _doTwoSum(Real a, Real b)

Real sum_error = (a - (value-approx_b) + (b-approx_b));

became (in accordance with Ref 1)

Real sum_error = (a - (value-approx_b)) + (b-approx_b);

Correction of the error in static HPReal _doQuickTwoSum(Real a,Real b)

it is necessary to test if a>b and invert a and b if this is not the case (see Ref 2, 3, 4)

Modification of Accumulate and Reduce, Accumulate and Reduce were based on Ref 1, which is based on Ref 4

Addition of products as proposed in Ref 2 (in accordance with Ref 5) of HPReal*Real or HPReal*HPReal (we take the same value of p for the SPLIT() function as in Ref 2, 4, 5)

Note that the Real*Real product in Ref 4 (p.4) is not written in the same way as those in Ref 2 (p.2) or 5 (p.3)

+++++++++++++++++

Overall, reduction algorithms or errors accumulated only with addition are less good

Algorithm 6 of DD_TWOSUM [Ref 2] is also less good:

static HPReal accumulate(Real a,MYHPReal b)
{
HPReal x(_doTwoSum(a,b.value()));
Real d= x.correction() + b.correction();
HPReal u(_doQuickTwoSum(x.value(),d));
return _doQuickTwoSum(u.value(),u.correction());
}

or

{
HPReal x(_doTwoSum(a.value(),b.value()));
HPReal y(_doTwoSum(a.correction(),b.correction()));
Real d= x.correction() + y.value();
HPReal u(_doQuickTwoSum(x.value(),d));
Real w=y.correction()+u.correction();
return _doQuickTwoSum(u.value(),w);
}
constexpr Real value() const
Internal value. Generally, you must use toReal().
constexpr Real correction() const
Internal correction.
constexpr HPReal reduce(HPReal b) const
Adds an HPReal v while preserving the error (reduction).

Tests on millions of positive as well as negative values have been performed for addition, multiplication, and division in different orders.

I have not managed to show a difference in results in a simple case between the proposed algorithm for the sum and the one initially coded.

References

Ref 1 Obtaining identical results with double precision global accuracy on different numbers of processors in parallel particle Monte Carlo simulations (Mathew A. Cleveland, Thomas A. Brunner, Nicholas A. Gentile, jeffrey A. Keasler) in Journal Of Computational Physics 251 (2013) 223-236.

Ref 2 Automatic Source-to-Source error compensation of floating-point programs L.Thevenoux, Ph langlois, Mathieu Martel HAL Id: hal-01158399

Ref 3 Numerical validation of compensated summation algorithms withs stochastic arithmetic S.Gaillat, F.Jézéquel , R.Picot Published in Electronic Notes in Theoritical Computer Science

or

Numerical validation of compensated algorithms withs stochastic arithmetic S.Gaillat, F.Jézéquel , R.Picot Published in Applied Mathematics and Computation 329 (2018)339-363

Ref 4 Library for Double-Double and Quad-Double Arithmetic December 29, 2007 Yozo Hida Xiaoye Li D.H.Bailey

Ref 5 Accurate floating point Product and exponentation S.Graillat HAL ID: hal-00164607

Ref 6 A floating point technique for extending the avaible precision T.J.Dekker Numeri.Math 18, 224-242 (1971)

Definition at line 158 of file arccore/src/base/arccore/base/HPReal.h.

Constructor & Destructor Documentation

◆ HPReal() [1/4]

Arcane::HPReal::HPReal ( )
inlineconstexpr

Default constructor to 0.0.

Definition at line 163 of file arccore/src/base/arccore/base/HPReal.h.

Referenced by operator*=(), operator+=(), operator/=(), reduce(), and zero().

◆ HPReal() [2/4]

Arcane::HPReal::HPReal ( NoInitTag )
inlineconstexpr

Constructor WITHOUT initialization.

Definition at line 169 of file arccore/src/base/arccore/base/HPReal.h.

◆ HPReal() [3/4]

Arcane::HPReal::HPReal ( double avalue)
inlineexplicitconstexpr

Creates an HP real with the value value and the correction correction.

Definition at line 172 of file arccore/src/base/arccore/base/HPReal.h.

◆ HPReal() [4/4]

Arcane::HPReal::HPReal ( double avalue,
double acorrection )
inlineconstexpr

Creates an HP real with the value value and the correction correction.

Definition at line 178 of file arccore/src/base/arccore/base/HPReal.h.

Member Function Documentation

◆ accumulate()

constexpr HPReal Arcane::HPReal::accumulate ( Real a,
HPReal b )
inlinestaticconstexpr

Definition at line 254 of file arccore/src/base/arccore/base/HPReal.h.

◆ assign()

std::istream & Arcane::HPReal::assign ( std::istream & i)

Reads an HPReal from the stream i. The pair is read in the form of two Real type values.

Definition at line 27 of file HPReal.cc.

◆ correction()

Real Arcane::HPReal::correction ( ) const
inlineconstexpr

Internal correction.

Definition at line 187 of file arccore/src/base/arccore/base/HPReal.h.

Referenced by toReal().

◆ div2() [1/2]

constexpr HPReal Arcane::HPReal::div2 ( HPReal a,
HPReal b )
inlinestaticconstexpr

Definition at line 288 of file arccore/src/base/arccore/base/HPReal.h.

◆ div2() [2/2]

constexpr HPReal Arcane::HPReal::div2 ( Real b,
HPReal a )
inlinestaticconstexpr

Definition at line 296 of file arccore/src/base/arccore/base/HPReal.h.

◆ operator*=() [1/2]

void Arcane::HPReal::operator*= ( HPReal v)
inlineconstexpr

Multiplies an HPReal v while preserving the error (reduction).

Definition at line 220 of file arccore/src/base/arccore/base/HPReal.h.

References HPReal().

◆ operator*=() [2/2]

void Arcane::HPReal::operator*= ( Real v)
inlineconstexpr

Multiplies a Real while preserving the error.

Definition at line 214 of file arccore/src/base/arccore/base/HPReal.h.

◆ operator+=() [1/2]

void Arcane::HPReal::operator+= ( HPReal v)
inlineconstexpr

Adds an HPReal v while preserving the error (reduction).

Definition at line 196 of file arccore/src/base/arccore/base/HPReal.h.

References HPReal(), and reduce().

◆ operator+=() [2/2]

void Arcane::HPReal::operator+= ( Real v)
inlineconstexpr

Adds a Real while preserving the error.

Definition at line 190 of file arccore/src/base/arccore/base/HPReal.h.

◆ operator/=() [1/2]

void Arcane::HPReal::operator/= ( HPReal v)
inlineconstexpr

Multiplies an HPReal v while preserving the error (reduction).

Definition at line 232 of file arccore/src/base/arccore/base/HPReal.h.

References HPReal().

◆ operator/=() [2/2]

void Arcane::HPReal::operator/= ( Real v)
inlineconstexpr

Multiplies a Real while preserving the error.

Definition at line 226 of file arccore/src/base/arccore/base/HPReal.h.

◆ print()

std::ostream & Arcane::HPReal::print ( std::ostream & o) const

Writes the instance to the stream o readable by an assign().

Definition at line 37 of file HPReal.cc.

◆ printPretty()

std::ostream & Arcane::HPReal::printPretty ( std::ostream & o) const

Writes the instance to the stream o in the form (x,y).

Definition at line 47 of file HPReal.cc.

◆ product() [1/2]

constexpr HPReal Arcane::HPReal::product ( HPReal a,
HPReal b )
inlinestaticconstexpr

Definition at line 270 of file arccore/src/base/arccore/base/HPReal.h.

◆ product() [2/2]

constexpr HPReal Arcane::HPReal::product ( Real a,
HPReal b )
inlinestaticconstexpr

Definition at line 277 of file arccore/src/base/arccore/base/HPReal.h.

◆ reduce() [1/2]

constexpr HPReal Arcane::HPReal::reduce ( HPReal a,
HPReal b )
inlinestaticconstexpr

Definition at line 262 of file arccore/src/base/arccore/base/HPReal.h.

◆ reduce() [2/2]

HPReal Arcane::HPReal::reduce ( HPReal b) const
inlineconstexpr

Adds an HPReal v while preserving the error (reduction).

Definition at line 208 of file arccore/src/base/arccore/base/HPReal.h.

References HPReal(), and reduce().

Referenced by operator+=(), and reduce().

◆ toReal()

Real Arcane::HPReal::toReal ( ) const
inlineconstexpr

Converts the instance to a Real.

Definition at line 202 of file arccore/src/base/arccore/base/HPReal.h.

References correction(), and value().

◆ value()

Real Arcane::HPReal::value ( ) const
inlineconstexpr

Internal value. Generally, you must use toReal().

Definition at line 184 of file arccore/src/base/arccore/base/HPReal.h.

Referenced by toReal().

◆ zero()

constexpr HPReal Arcane::HPReal::zero ( )
inlinestaticconstexpr

Zero value.

Definition at line 250 of file arccore/src/base/arccore/base/HPReal.h.

References HPReal().

◆ operator!=

bool operator!= ( const HPReal & a,
const HPReal & b )
friend

Definition at line 318 of file arccore/src/base/arccore/base/HPReal.h.

◆ operator+

HPReal operator+ ( const HPReal & a,
const HPReal & b )
friend

Definition at line 322 of file arccore/src/base/arccore/base/HPReal.h.

◆ operator<

bool operator< ( const HPReal & a,
const HPReal & b )
friend

Definition at line 306 of file arccore/src/base/arccore/base/HPReal.h.

◆ operator<<

std::ostream & operator<< ( std::ostream & o,
HPReal t )
friend

Definition at line 327 of file arccore/src/base/arccore/base/HPReal.h.

◆ operator==

bool operator== ( const HPReal & a,
const HPReal & b )
friend

Definition at line 314 of file arccore/src/base/arccore/base/HPReal.h.

◆ operator>

bool operator> ( const HPReal & a,
const HPReal & b )
friend

Definition at line 310 of file arccore/src/base/arccore/base/HPReal.h.

◆ operator>>

std::istream & operator>> ( std::istream & i,
HPReal & t )
friend

Definition at line 332 of file arccore/src/base/arccore/base/HPReal.h.


The documentation for this class was generated from the following files: