1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::{
    cmp::Ordering,
    hash::{Hash, Hasher},
    ops::Neg,
};

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;

/// A wrapper for floats that implements [`Ord`], [`Eq`], and [`Hash`] traits.
///
/// This is a work around for the fact that the IEEE 754-2008 standard,
/// implemented by Rust's [`f32`] type,
/// doesn't define an ordering for [`NaN`](f32::NAN),
/// and `NaN` is not considered equal to any other `NaN`.
///
/// Wrapping a float with `FloatOrd` breaks conformance with the standard
/// by sorting `NaN` as less than all other numbers and equal to any other `NaN`.
#[derive(Debug, Copy, Clone)]
#[cfg_attr(
    feature = "bevy_reflect",
    derive(Reflect),
    reflect(Debug, PartialEq, Hash)
)]
pub struct FloatOrd(pub f32);

impl PartialOrd for FloatOrd {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }

    fn lt(&self, other: &Self) -> bool {
        !other.le(self)
    }
    // If `self` is NaN, it is equal to another NaN and less than all other floats, so return true.
    // If `self` isn't NaN and `other` is, the float comparison returns false, which match the `FloatOrd` ordering.
    // Otherwise, a standard float comparison happens.
    fn le(&self, other: &Self) -> bool {
        self.0.is_nan() || self.0 <= other.0
    }
    fn gt(&self, other: &Self) -> bool {
        !self.le(other)
    }
    fn ge(&self, other: &Self) -> bool {
        other.le(self)
    }
}

impl Ord for FloatOrd {
    #[allow(clippy::comparison_chain)]
    fn cmp(&self, other: &Self) -> Ordering {
        if self > other {
            Ordering::Greater
        } else if self < other {
            Ordering::Less
        } else {
            Ordering::Equal
        }
    }
}

impl PartialEq for FloatOrd {
    fn eq(&self, other: &Self) -> bool {
        if self.0.is_nan() {
            other.0.is_nan()
        } else {
            self.0 == other.0
        }
    }
}

impl Eq for FloatOrd {}

impl Hash for FloatOrd {
    fn hash<H: Hasher>(&self, state: &mut H) {
        if self.0.is_nan() {
            // Ensure all NaN representations hash to the same value
            state.write(&f32::to_ne_bytes(f32::NAN));
        } else if self.0 == 0.0 {
            // Ensure both zeroes hash to the same value
            state.write(&f32::to_ne_bytes(0.0f32));
        } else {
            state.write(&f32::to_ne_bytes(self.0));
        }
    }
}

impl Neg for FloatOrd {
    type Output = FloatOrd;

    fn neg(self) -> Self::Output {
        FloatOrd(-self.0)
    }
}

#[cfg(test)]
mod tests {
    use std::hash::DefaultHasher;

    use super::*;

    const NAN: FloatOrd = FloatOrd(f32::NAN);
    const ZERO: FloatOrd = FloatOrd(0.0);
    const ONE: FloatOrd = FloatOrd(1.0);

    #[test]
    fn float_ord_eq() {
        assert_eq!(NAN, NAN);

        assert_ne!(NAN, ZERO);
        assert_ne!(ZERO, NAN);

        assert_eq!(ZERO, ZERO);
    }

    #[test]
    fn float_ord_cmp() {
        assert_eq!(NAN.cmp(&NAN), Ordering::Equal);

        assert_eq!(NAN.cmp(&ZERO), Ordering::Less);
        assert_eq!(ZERO.cmp(&NAN), Ordering::Greater);

        assert_eq!(ZERO.cmp(&ZERO), Ordering::Equal);
        assert_eq!(ONE.cmp(&ZERO), Ordering::Greater);
        assert_eq!(ZERO.cmp(&ONE), Ordering::Less);
    }

    #[test]
    #[allow(clippy::nonminimal_bool)]
    fn float_ord_cmp_operators() {
        assert!(!(NAN < NAN));
        assert!(NAN < ZERO);
        assert!(!(ZERO < NAN));
        assert!(!(ZERO < ZERO));
        assert!(ZERO < ONE);
        assert!(!(ONE < ZERO));

        assert!(!(NAN > NAN));
        assert!(!(NAN > ZERO));
        assert!(ZERO > NAN);
        assert!(!(ZERO > ZERO));
        assert!(!(ZERO > ONE));
        assert!(ONE > ZERO);

        assert!(NAN <= NAN);
        assert!(NAN <= ZERO);
        assert!(!(ZERO <= NAN));
        assert!(ZERO <= ZERO);
        assert!(ZERO <= ONE);
        assert!(!(ONE <= ZERO));

        assert!(NAN >= NAN);
        assert!(!(NAN >= ZERO));
        assert!(ZERO >= NAN);
        assert!(ZERO >= ZERO);
        assert!(!(ZERO >= ONE));
        assert!(ONE >= ZERO);
    }

    #[test]
    fn float_ord_hash() {
        let hash = |num| {
            let mut h = DefaultHasher::new();
            FloatOrd(num).hash(&mut h);
            h.finish()
        };

        assert_ne!((-0.0f32).to_bits(), 0.0f32.to_bits());
        assert_eq!(hash(-0.0), hash(0.0));

        let nan_1 = f32::from_bits(0b0111_1111_1000_0000_0000_0000_0000_0001);
        assert!(nan_1.is_nan());
        let nan_2 = f32::from_bits(0b0111_1111_1000_0000_0000_0000_0000_0010);
        assert!(nan_2.is_nan());
        assert_ne!(nan_1.to_bits(), nan_2.to_bits());
        assert_eq!(hash(nan_1), hash(nan_2));
    }
}