Cody

Problem 44691. Comparison of floating-point numbers (singles)

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:

Solution Stats

25.0% Correct | 75.0% Incorrect
Last solution submitted on Aug 14, 2018

Problem Comments