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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};

#[derive(Debug, Default, Clone, Copy)]
pub struct F32(pub f32);

/// This works like `PartialEq` on `f32`, except that `NAN == NAN` is true.
impl PartialEq for F32 {
    fn eq(&self, other: &Self) -> bool {
        if self.0.is_nan() && other.0.is_nan() {
            true
        } else {
            self.0 == other.0
        }
    }
}

impl Eq for F32 {}

/// This works like `PartialOrd` on `f32`, except that `NAN` sorts below all other floats
/// (and is equal to another NAN). This always returns a `Some`.
impl PartialOrd for F32 {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// This works like `PartialOrd` on `f32`, except that `NAN` sorts below all other floats
/// (and is equal to another NAN).
impl Ord for F32 {
    fn cmp(&self, other: &Self) -> Ordering {
        self.0.partial_cmp(&other.0).unwrap_or_else(|| {
            if self.0.is_nan() && !other.0.is_nan() {
                Ordering::Less
            } else if !self.0.is_nan() && other.0.is_nan() {
                Ordering::Greater
            } else {
                Ordering::Equal
            }
        })
    }
}

impl Hash for F32 {
    fn hash<H: Hasher>(&self, state: &mut H) {
        if self.0.is_nan() {
            0x7fc00000u32.hash(state); // a particular bit representation for NAN
        } else if self.0 == 0.0 { // catches both positive and negative zero
            0u32.hash(state);
        } else {
            self.0.to_bits().hash(state);
        }
    }
}

impl From<F32> for f32 {
    fn from(f: F32) -> Self {
        f.0
    }
}

impl From<f32> for F32 {
    fn from(f: f32) -> Self {
        F32(f)
    }
}

impl fmt::Display for F32 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(f)
    }
}

#[derive(Debug, Default, Clone, Copy)]
pub struct F64(pub f64);

/// This works like `PartialEq` on `f64`, except that `NAN == NAN` is true.
impl PartialEq for F64 {
    fn eq(&self, other: &Self) -> bool {
        if self.0.is_nan() && other.0.is_nan() {
            true
        } else {
            self.0 == other.0
        }
    }
}

impl Eq for F64 {}

/// This works like `PartialOrd` on `f64`, except that `NAN` sorts below all other floats
/// (and is equal to another NAN). This always returns a `Some`.
impl PartialOrd for F64 {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// This works like `PartialOrd` on `f64`, except that `NAN` sorts below all other floats
/// (and is equal to another NAN).
impl Ord for F64 {
    fn cmp(&self, other: &Self) -> Ordering {
        self.0.partial_cmp(&other.0).unwrap_or_else(|| {
            if self.0.is_nan() && !other.0.is_nan() {
                Ordering::Less
            } else if !self.0.is_nan() && other.0.is_nan() {
                Ordering::Greater
            } else {
                Ordering::Equal
            }
        })
    }
}

impl Hash for F64 {
    fn hash<H: Hasher>(&self, state: &mut H) {
        if self.0.is_nan() {
            0x7ff8000000000000u64.hash(state); // a particular bit representation for NAN
        } else if self.0 == 0.0 { // catches both positive and negative zero
            0u64.hash(state);
        } else {
            self.0.to_bits().hash(state);
        }
    }
}

impl From<F64> for f64 {
    fn from(f: F64) -> Self {
        f.0
    }
}

impl From<f64> for F64 {
    fn from(f: f64) -> Self {
        F64(f)
    }
}

impl fmt::Display for F64 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(f)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    use super::{F32, F64};

    fn calculate_hash<T: Hash>(t: &T) -> u64 {
        let mut s = DefaultHasher::new();
        t.hash(&mut s);
        s.finish()
    }

    #[test]
    fn f32_eq() {
        assert!(F32(std::f32::NAN) == F32(std::f32::NAN));
        assert!(F32(std::f32::NAN) != F32(5.0));
        assert!(F32(5.0) != F32(std::f32::NAN));
        assert!(F32(0.0) == F32(-0.0));
    }

    #[test]
    fn f32_cmp() {
        assert!(F32(std::f32::NAN) == F32(std::f32::NAN));
        assert!(F32(std::f32::NAN) < F32(5.0));
        assert!(F32(5.0) > F32(std::f32::NAN));
        assert!(F32(0.0) == F32(-0.0));
    }

    #[test]
    fn f32_hash() {
        assert!(calculate_hash(&F32(0.0)) == calculate_hash(&F32(-0.0)));
        assert!(calculate_hash(&F32(std::f32::NAN)) == calculate_hash(&F32(-std::f32::NAN)));
    }

    #[test]
    fn f64_eq() {
        assert!(F64(std::f64::NAN) == F64(std::f64::NAN));
        assert!(F64(std::f64::NAN) != F64(5.0));
        assert!(F64(5.0) != F64(std::f64::NAN));
        assert!(F64(0.0) == F64(-0.0));
    }

    #[test]
    fn f64_cmp() {
        assert!(F64(std::f64::NAN) == F64(std::f64::NAN));
        assert!(F64(std::f64::NAN) < F64(5.0));
        assert!(F64(5.0) > F64(std::f64::NAN));
        assert!(F64(0.0) == F64(-0.0));
    }

    #[test]
    fn f64_hash() {
        assert!(calculate_hash(&F64(0.0)) == calculate_hash(&F64(-0.0)));
        assert!(calculate_hash(&F64(std::f64::NAN)) == calculate_hash(&F64(-std::f64::NAN)));
    }
}