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

Floating-point approximations. More...

Functions

double GetAbsoluteDifference (double a, double b)
 Returns the absolute value of the difference between a and b. More...
 
double GetLogDifference (double a, double b)
 Returns the difference of the logarithm; input values are truncated at the minimum positive value. More...
 
double GetRelativeDifference (double a, double b)
 Returns the safe relative difference, which is 2(|a-b|)/(|a|+|b|) except in special cases. More...
 

Detailed Description

Floating-point approximations.

Function Documentation

◆ GetAbsoluteDifference()

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

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

Definition at line 23 of file Numeric.cpp.

24 {
25  return std::abs(a - b);
26 }

Referenced by SimDataPair::absoluteDifference().

◆ 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 41 of file Numeric.cpp.

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

◆ 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 29 of file Numeric.cpp.

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

Referenced by DataUtils::createRelativeDifferenceData(), DataUtils::relativeDataDifference(), SimDataPair::relativeDifference(), HistoUtils::RelativeDifference(), and IHistogram::relativeDifferenceHistogram().