1
//! Utilities to compare floating-point numbers.
2

            
3
use float_cmp::ApproxEq;
4

            
5
// The following are copied from cairo/src/{cairo-fixed-private.h,
6
// cairo-fixed-type-private.h}
7

            
8
const CAIRO_FIXED_FRAC_BITS: u64 = 8;
9
const CAIRO_MAGIC_NUMBER_FIXED: f64 = (1u64 << (52 - CAIRO_FIXED_FRAC_BITS)) as f64 * 1.5;
10

            
11
6
fn cairo_magic_double(d: f64) -> f64 {
12
6
    d + CAIRO_MAGIC_NUMBER_FIXED
13
6
}
14

            
15
6
fn cairo_fixed_from_double(d: f64) -> i32 {
16
6
    let bits = cairo_magic_double(d).to_bits();
17
6
    let lower = bits & 0xffffffff;
18
6
    lower as i32
19
6
}
20

            
21
/// Implements a method to check whether two `f64` numbers would have
22
/// the same fixed-point representation in Cairo.
23
///
24
/// This generally means that the absolute difference between them,
25
/// when taken as floating-point numbers, is less than the smallest
26
/// representable fraction that Cairo can represent in fixed-point.
27
///
28
/// Implementation detail: Cairo fixed-point numbers use 24 bits for
29
/// the integral part, and 8 bits for the fractional part.  That is,
30
/// the smallest fraction they can represent is 1/256.
31
pub trait FixedEqCairo {
32
    #[allow(dead_code)] // https://github.com/rust-lang/rust/issues/120770
33
    fn fixed_eq_cairo(&self, other: &Self) -> bool;
34
}
35

            
36
impl FixedEqCairo for f64 {
37
3
    fn fixed_eq_cairo(&self, other: &f64) -> bool {
38
        // FIXME: Here we have the same problem as Cairo itself: we
39
        // don't check for overflow in the conversion of double to
40
        // fixed-point.
41
3
        cairo_fixed_from_double(*self) == cairo_fixed_from_double(*other)
42
3
    }
43
}
44

            
45
/// Checks whether two floating-point numbers are approximately equal,
46
/// considering Cairo's limitations on numeric representation.
47
///
48
/// Cairo uses fixed-point numbers internally.  We implement this
49
/// trait for `f64`, so that two numbers can be considered "close
50
/// enough to equal" if their absolute difference is smaller than the
51
/// smallest fixed-point fraction that Cairo can represent.
52
///
53
/// Note that this trait is reliable even if the given numbers are
54
/// outside of the range that Cairo's fixed-point numbers can
55
/// represent.  In that case, we check for the absolute difference,
56
/// and finally allow a difference of 1 unit-in-the-last-place (ULP)
57
/// for very large f64 values.
58
pub trait ApproxEqCairo: ApproxEq {
59
    fn approx_eq_cairo(self, other: Self) -> bool;
60
}
61

            
62
impl ApproxEqCairo for f64 {
63
2955208
    fn approx_eq_cairo(self, other: f64) -> bool {
64
2955208
        let cairo_smallest_fraction = 1.0 / f64::from(1 << CAIRO_FIXED_FRAC_BITS);
65
2955208
        self.approx_eq(other, (cairo_smallest_fraction, 1))
66
2955208
    }
67
}
68

            
69
// Macro for usage in unit tests
70
#[doc(hidden)]
71
#[macro_export]
72
macro_rules! assert_approx_eq_cairo {
73
    ($left:expr, $right:expr) => {{
74
        match ($left, $right) {
75
            (l, r) => {
76
                if !l.approx_eq_cairo(r) {
77
                    panic!(
78
                        r#"assertion failed: `(left == right)`
79
  left: `{:?}`,
80
 right: `{:?}`"#,
81
                        l, r
82
                    )
83
                }
84
            }
85
        }
86
    }};
87
}
88

            
89
#[cfg(test)]
90
mod tests {
91
    use super::*;
92

            
93
    #[test]
94
2
    fn numbers_equal_in_cairo_fixed_point() {
95
1
        assert!(1.0_f64.fixed_eq_cairo(&1.0_f64));
96

            
97
1
        assert!(1.0_f64.fixed_eq_cairo(&1.001953125_f64)); // 1 + 1/512 - cairo rounds to 1
98

            
99
1
        assert!(!1.0_f64.fixed_eq_cairo(&1.00390625_f64)); // 1 + 1/256 - cairo can represent it
100
2
    }
101

            
102
    #[test]
103
2
    fn numbers_approx_equal() {
104
        // 0 == 1/256 - cairo can represent it, so not equal
105
1
        assert!(!0.0_f64.approx_eq_cairo(0.00390635_f64));
106

            
107
        // 1 == 1 + 1/256 - cairo can represent it, so not equal
108
1
        assert!(!1.0_f64.approx_eq_cairo(1.00390635_f64));
109

            
110
        // 0 == 1/256 - cairo can represent it, so not equal
111
1
        assert!(!0.0_f64.approx_eq_cairo(-0.00390635_f64));
112

            
113
        // 1 == 1 - 1/256 - cairo can represent it, so not equal
114
1
        assert!(!1.0_f64.approx_eq_cairo(0.99609365_f64));
115

            
116
        // 0 == 1/512 - cairo approximates to 0, so equal
117
1
        assert!(0.0_f64.approx_eq_cairo(0.001953125_f64));
118

            
119
        // 1 == 1 + 1/512 - cairo approximates to 1, so equal
120
1
        assert!(1.0_f64.approx_eq_cairo(1.001953125_f64));
121

            
122
        // 0 == -1/512 - cairo approximates to 0, so equal
123
1
        assert!(0.0_f64.approx_eq_cairo(-0.001953125_f64));
124

            
125
        // 1 == 1 - 1/512 - cairo approximates to 1, so equal
126
1
        assert!(1.0_f64.approx_eq_cairo(0.998046875_f64));
127

            
128
        // This is 2^53 compared to (2^53 + 2).  When represented as
129
        // f64, they are 1 unit-in-the-last-place (ULP) away from each
130
        // other, since the mantissa has 53 bits (52 bits plus 1
131
        // "hidden" bit).  The first number is an exact double, and
132
        // the second one is the next biggest double.  We consider a
133
        // difference of 1 ULP to mean that numbers are "equal", to
134
        // account for slight imprecision in floating-point
135
        // calculations.  Most of the time, for small values, we will
136
        // be using the cairo_smallest_fraction from the
137
        // implementation of approx_eq_cairo() above.  For large
138
        // values, we want the ULPs.
139
        //
140
        // In the second assertion, we compare 2^53 with (2^53 + 4).  Those are
141
        // 2 ULPs away, and we don't consider them equal.
142
1
        assert!(9_007_199_254_740_992.0.approx_eq_cairo(9_007_199_254_740_994.0));
143
1
        assert!(!9_007_199_254_740_992.0.approx_eq_cairo(9_007_199_254_740_996.0));
144
2
    }
145

            
146
    #[test]
147
2
    fn assert_approx_eq_cairo_should_not_panic() {
148
1
        assert_approx_eq_cairo!(42_f64, 42_f64);
149
2
    }
150

            
151
    #[test]
152
    #[should_panic]
153
2
    fn assert_approx_eq_cairo_should_panic() {
154
1
        assert_approx_eq_cairo!(3_f64, 42_f64);
155
1
    }
156
}