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
956 Solvers
142 Solvers
Top of the Hour : Return from your routine within 1 second of the hour
18 Solvers
Without the French accent please!
85 Solvers
Generate a vector like 1,2,2,3,3,3,4,4,4,4
1898 Solvers