Problem 44690. Comparison of floating-point numbers (doubles)
Floating-point numbers cannot generally be represented exactly, so it is usually inappropriate to test for 'equality' between two floating-point numbers. Rather, it is generally appropriate to check whether the difference between the two numbers is sufficiently small that they can be considered practically equal. (In other words, so close in value that any differences could be explained by inherent limitations of computations using floating-point numbers.)
For this problem "small" shall be defined as no more than ten times ε, in which ε is the larger of ε₁ & ε₂, and ε₁ is the floating-point precision with which A is represented, and ε₂ is the precision with which B is represented.
% Input A = 0 B = 1E-50
% Output practicallyEqual = false
Explanation: A is represented with a precision of ε₁ = 2⁻¹⁰⁷⁴, whereas B is represented with a precision of ε₂ = 2⁻²¹⁹. Thus ε = 2⁻²¹⁹, and the threshold is 10×2⁻²¹⁹. The difference between A and B is 1×10⁻⁵⁰, and this difference exceeds the threshold. Thus A and B are not practically equal in this example.
Solution CommentsShow comments
Problem Recent Solvers7