Celeritas 0.7.0-dev.242+develop.62c3034b
Loading...
Searching...
No Matches
Public Member Functions | List of all members
celeritas::SoftEqual< RealType > Class Template Reference

Functor for noninfinite floating point equality. More...

#include <SoftEqual.hh>

Public Types

Type aliases
using value_type = RealType
 

Public Member Functions

constexpr SoftEqual ()
 Construct with default relative/absolute precision.
 
 SoftEqual (value_type rel)
 Construct with scaled absolute precision.
 
 SoftEqual (value_type rel, value_type abs)
 Construct with both relative and absolute precision.
 
bool operator() (value_type a, value_type b) const
 Compare two values, implicitly casting arguments.
 
constexpr value_type rel () const
 Relative allowable error.
 
constexpr 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) using celeritas::EqualOr.

Constructor & Destructor Documentation

◆ SoftEqual()

template<class RealType >
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: