
please wait
JLBorges wrote: |
---|
Note: The relative distance/error between a and b is defined as fabs( (a-b) / min(a,b) ) |
This method passes tests for many important special cases, but as you can see, it uses some quite non-obvious logic. In particular, it has to use a completely different definition of error margin when a or b is zero, because the classical definition of relative error becomes meaningless in those cases. There are some cases where the method above still produces unexpected results (in particular, it’s much stricter when one value is nearly zero than when it is exactly zero), and some of the tests it was developed to pass probably specify behaviour that is not appropriate for some applications. Before using it, make sure it’s appropriate for your application! |
It is asymmetric, but it is only used to check if the two values are 'close enough' to each other. |
a=10, b=1 would give 9.0 a=-10, b=-1 would give 0.9 |
The following special cases are handled as follows: . If either of a or b is a NaN, then returns the largest representable value for T: for example for type double, this is std::numeric_limits<double>::max() which is the same as DBL_MAX or 1.7976931348623157e+308. . If a and b differ in sign then returns the largest representable value for T. . If both a and b are both infinities (of the same sign), then returns zero. . If just one of a and b is an infinity, then returns the largest representable value for T. . If both a and b are zero then returns zero. . If just one of a or b is a zero or a denormalized value, then it is treated as if it were the smallest (non-denormalized) value representable in T for the purposes of the above calculation. These rules were primarily designed to assist with our own test suite, they are designed to be robust enough that the function can in most cases be used blindly, including in cases where the expected result is actually too small to represent in type T and underflows to zero. |
#include <iostream> #include <boost/math/special_functions/relative_difference.hpp> using boost::math::relative_difference; int main() { std::cout << "relative_difference for (1,10) = " << relative_difference(1.0f, 10.0f) << '\n'; std::cout << "relative_difference for (-1,-10) = " << relative_difference(-1.0f, -10.0f) << '\n'; } |