- sherpa.utils.Knuth_close(x, y, tol, myop=<built-in function or_>)
Check whether two floating-point numbers are close together.
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) <= edoes 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) ).