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.)
Based on two scalar inputs of type double, namely A and B, you must return a scalar logical output set to true if the difference in magnitude between the single-precision representations of A and B is 'small', or otherwise set to false.
For this problem "small" shall mean no more than Δ, which is defined as the larger of δ and ten times ε. In turn, δ has a fixed value of 1×10⁻¹⁰ and ε is the larger of ε₁ & ε₂, where ε₁ is the floating-point precision with which A is represented, and ε₂ is the precision with which B is represented.
EXAMPLE:
% Input A = 0 B = 1E-20
% Output practicallyEqual = true
Explanation: When represented as single values, A is represented with a precision of ε₁ = 2⁻¹⁴⁹, whereas B is represented with a precision of ε₂ = 2⁻⁹⁰. Thus ε = 2⁻⁹⁰; however the threshold Δ is 1×10⁻¹⁰ for this situation, because 1×10⁻¹⁰ > 10×2⁻⁹⁰. The difference between A and B is 1×10⁻²⁰, and this difference does not exceed the threshold. Thus A and B are practically equal in this example.
RELATED PROBLEMS:
Oops. I just noticed that I forgot to rename the function from "compareDoubles" (used in Problem 44690) to "compareSingles". Unfortunately I cannot make the fix now without breaking the two successful submissions. Sorry about any confusion. —DIV
7879 Solvers
1563 Solvers
What is the distance from point P(x,y) to the line Ax + By + C = 0?
216 Solvers
68 Solvers
How many days does the cat take to climb out of the hole?
56 Solvers