Celeritas  0.5.0-86+4a8eea4
Public Member Functions | List of all members
celeritas::SoftEqual< RealType > Class Template Reference

Functor for noninfinite floating point equality. More...

#include <SoftEqual.hh>

Inheritance diagram for celeritas::SoftEqual< RealType >:
Inheritance graph
[legend]

Public Types

Type aliases
using value_type = RealType
 

Public Member Functions

CELER_CONSTEXPR_FUNCTION SoftEqual ()
 Construct with default relative/absolute precision.
 
CELER_FUNCTION SoftEqual (value_type rel)
 Construct with scaled absolute precision.
 
CELER_FUNCTION SoftEqual (value_type rel, value_type abs)
 Construct with both relative and absolute precision. More...
 
bool CELER_FUNCTION operator() (value_type a, value_type b) const
 Compare two values, implicitly casting arguments.
 
CELER_CONSTEXPR_FUNCTION value_type rel () const
 Relative allowable error.
 
CELER_CONSTEXPR_FUNCTION value_type abs () const
 Absolute tolerance.
 

Detailed Description

template<class RealType = ::celeritas::real_type>
class celeritas::SoftEqual< RealType >

Functor for noninfinite floating point equality.

This function-like class considers an absolute tolerance for values near zero, and a relative tolerance for values far from zero. It correctly returns "false" if either value being compared is NaN. The call operator is commutative: eq(a,b) should always give the same as eq(b,a).

The actual comparison is:

\[ |a - b| < \max(\epsilon_r \max(|a|, |b|), \epsilon_a) \]

Note
The edge case where both values are infinite (with the same sign) returns false for equality, which could be considered reasonable because relative error is meaningless. To explicitly allow infinities to compare equal, you must test separately, e.g., a == b || soft_eq(a, b).

Constructor & Destructor Documentation

◆ SoftEqual()

template<class RealType >
CELER_FUNCTION celeritas::SoftEqual< RealType >::SoftEqual ( value_type  rel,
value_type  abs 
)

Construct with both relative and absolute precision.

Parameters
reltolerance of relative error (default 1.0e-12 for doubles)
absthreshold for absolute error when comparing small quantities (default 1.0e-14 for doubles)

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