Skip to content

Commit 10ece47

Browse files
update explanation since a lot of it is not needed any more
1 parent 94d5ba5 commit 10ece47

File tree

1 file changed

+6
-25
lines changed

1 file changed

+6
-25
lines changed

tests/pass/float.rs

+6-25
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,12 @@ use std::{f32, f64};
2323
/// First: The ULP of a float 'a' is the smallest possible change at 'a', so the ULP difference represents how
2424
/// many discrete floating-point steps are needed to reach 'b' from 'a'.
2525
///
26-
/// There are 2 techniques we can use to compute the ULP difference of 2 floating-point numbers:
26+
/// ULP(a) is the distance between the 2 closest floating-point numbers `x` and `y` around `a`, satisfying x < a < y, x != y.
27+
/// To use this to calculate the ULP difference we have to halve it (we need it at `a`, but we just went "up" and "down", halving it gives us this ULP).
28+
/// Then take the difference of `b` and `a` and divide it by that ULP and finally round it.
29+
/// We know now how many floating-point changes we have to apply to `a` to get to `b`.
2730
///
28-
/// ULP(a) is the difference between 'a' and the next larger representable floating-point number.
29-
/// So to get the ULP difference of `a` and `b`, we can take the absolute difference of
30-
/// the *unsigned integer* representation of `a` and `b`.
31-
/// For example checking 2 f32's, which in IEEE format looks like this:
32-
///
33-
/// s: sign bit
34-
/// e: exponent bits
35-
/// m: significand bits
36-
/// f32: s | eeee eeee | mmm mmmm mmmm mmmm mmmm mmmm
37-
/// u32: seee eeee emmm mmmm mmmm mmmm mmmm mmmm
38-
///
39-
///
40-
/// So when the sign and exponent are the same, we can compute a reasonable ULP difference.
41-
///
42-
/// But if you know some details of floating-point numbers, it is possible to get 2 values that sit on the border of the exponent,
43-
/// so `a` can have exponent but `b` can have a different exponent while still being approximately equal to `a`. For this we can use
44-
/// John Harrison's definition. Which states that ULP(a) is the distance between the 2 closest floating-point numbers
45-
/// `x` and `y` around `a`, satisfying x < a < y, x != y. This makes the ULP of `a` twice as large as in the previous defintion and so
46-
/// if we want to use this, we have to halve it.
47-
/// This ULP value of `a` can then be used to find the ULP difference of `a` and `b`:
48-
/// Take the difference of `b` and `a` and divide it by that ULP and finally round it. This is the same value as the first definition,
49-
/// but this way can go around that exponent border problem.
50-
///
51-
/// If this ULP difference is less than or equal to our chosen upper bound
31+
/// So if this ULP difference is less than or equal to our chosen upper bound
5232
/// we can say that `a` and `b` are approximately equal, because they lie "close" enough to each other to be considered equal.
5333
///
5434
/// Note: We can see that checking `a` and `b` with different signs has no meaning, but we should not forget
@@ -58,6 +38,7 @@ use std::{f32, f64};
5838
/// the same amount of "spacing" between all consecutive representable values. So even though 2 very large floating point numbers
5939
/// have a large value difference, their ULP can still be 1, so they are still "approximatly equal",
6040
/// but the EPSILON check would have failed.
41+
///
6142
fn approx_eq_check<F: Float>(
6243
actual: F,
6344
expected: F,

0 commit comments

Comments
 (0)