BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Numeric Namespace Reference

Functions

double GetAbsoluteDifference (double a, double b)
 
double GetRelativeDifference (double a, double b)
 
double GetLogDifference (double a, double b)
 

Detailed Description

Floating-point epsilon, tolerances, almost-equal.

Function Documentation

◆ GetAbsoluteDifference()

double Numeric::GetAbsoluteDifference ( double  a,
double  b 
)

Returns the absolute value of the difference between a and b.

Definition at line 26 of file Numeric.cpp.

27 {
28  return std::abs(a - b);
29 }

Referenced by SimDataPair::absoluteDifference().

◆ GetRelativeDifference()

double Numeric::GetRelativeDifference ( double  a,
double  b 
)

Returns the safe relative difference, which is 2(|a-b|)/(|a|+|b|) except in special cases.

Definition at line 32 of file Numeric.cpp.

33 {
34  constexpr double eps = std::numeric_limits<double>::epsilon();
35  const double avg_abs = (std::abs(a) + std::abs(b)) / 2.0;
36  // return 0.0 if relative error smaller than epsilon
37  if (std::abs(a - b) <= eps * avg_abs)
38  return 0.0;
39  return std::abs(a - b) / avg_abs;
40 }

References anonymous_namespace{PolyhedralComponents.cpp}::eps.

Referenced by IntensityDataFunctions::createRelativeDifferenceData(), IntensityDataFunctions::getRelativeDifference(), SimDataPair::relativeDifference(), IntensityDataFunctions::RelativeDifference(), IHistogram::relativeDifferenceHistogram(), and MinimizerTestPlan::valuesAsExpected().

◆ GetLogDifference()

double Numeric::GetLogDifference ( double  a,
double  b 
)

Returns the difference of the logarithm; input values are truncated at the minimum positive value.

Definition at line 44 of file Numeric.cpp.

45 {
46  double a_t = std::max(a, std::numeric_limits<double>::min());
47  double b_t = std::max(b, std::numeric_limits<double>::min());
48  return std::abs(std::log(a_t) - std::log(b_t));
49 }