Knuth_close¶
-
sherpa.utils.
Knuth_close
(x, y, tol, myop=<built-in function or_>)[source]¶ Check whether two floating-point numbers are close together.
Notes
The following text was taken verbatim from [1]:
In most cases it is unreasonable to use an operator==(...) for a floating-point values equality check. The simple solution like
abs(f1-f2) <= e
does not work for very small or very big values. This floating-point comparison algorithm is based on the more confident solution presented by D. E. Knuth in ‘The art of computer programming (vol II)’. For a given floating point values u and v and a tolerance e:| u - v | <= e * |u| and | u - v | <= e * |v| (1)
defines a “very close with tolerance e” relationship between u and v:
| u - v | <= e * |u| or | u - v | <= e * |v| (2)
defines a “close enough with tolerance e” relationship between u and v. Both relationships are commutative but are not transitive. The relationship defined by inequations (1) is stronger that the relationship defined by inequations (2) (i.e. (1) => (2) ).
References
[1] http://www.boost.org/doc/libs/1_35_0/libs/test/doc/components/test_tools/floating_point_comparison.html#Introduction