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 A and B is 'small', or otherwise set to false.
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.
EXAMPLE:
% 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.
RELATED PROBLEMS:
"The hard way to go about it, but it worked like a charm!" :-)
If you think this one is ugly (and I agree 100% it is!) you should have seen my first few attempts, which included using "typecast" to get the numbers as uint64s and trying to compare them that way. At least this one worked...eventually.
James, actually, it's interesting on Cody to get to see things from another point of view. Kudos for original thinking :-) —DIV
5171 Solvers
1313 Solvers
Increment a number, given its digits
506 Solvers
Without the French accent please!
76 Solvers
31 Solvers