Skip to content

Commit

Permalink
update explanation of approx_check
Browse files Browse the repository at this point in the history
  • Loading branch information
LorrensP-2158466 committed Feb 8, 2025
1 parent 9a397ea commit 25ecf26
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions tests/pass/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ use std::fmt::{Debug, Display, LowerHex};
use std::hint::black_box;
use std::{f32, f64};

/// Another form of checking if 2 floating point numbers are almost equal to eachother
/// Another way of checking if 2 floating point numbers are almost equal to eachother.
/// Using `a` and `b` as floating point numbers:
///
/// Instead of doing a simple EPSILON check (which we did at first):
/// Absolute difference of `a` and `b` can't be greater than some E (10^-6, ...).
/// `(a - b).asb() <= E`
///
/// We look at ULP: `Units in the Last Place` or `Units of Least Precision`
/// We will now use ULP: `Units in the Last Place` or `Units of Least Precision`
/// It is the absolute difference of the *unsigned integer* representation of `a` and `b`.
/// For example for f32, which in IEEE format looks like this:
/// For example checking 2 f32's, which in IEEE format looks like this:
///
/// s: sign bit
/// e: exponent bits
Expand All @@ -33,11 +34,11 @@ use std::{f32, f64};
/// Same with exponents but no zero checking.
///
/// So when Sign and Exponent are the same, we can get a reasonable ULP value
/// by doing the operation explained above. And if this is less than or equal to our chosen upper bound
/// by doing the absolute difference, and if this is less than or equal to our chosen upper bound
/// we can say that `a` and `b` are approximately equal.
///
/// Basically ULP can be seen as a distance metric of floating point numbers, but having
/// the same amount of "spacing" between consecutive numbers. So eventhough 2 very large floating point numbers
/// the same amount of "spacing" between all consecutive representable values. So eventhough 2 very large floating point numbers
/// have a large value difference, their ULP can still be 1, so they are still "approximatly equal",
/// but the EPSILON check would have failed.
macro_rules! assert_approx_eq {
Expand Down

0 comments on commit 25ecf26

Please sign in to comment.