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
pub trait Zero {
const ZERO: Self;
}
impl Zero for f64 {
const ZERO: f64 = 0.0;
}
pub trait IntoOrd
where
Self::Output: Ord,
{
type Output;
fn into_ord(self) -> Self::Output;
}
impl IntoOrd for f64 {
type Output = i64;
fn into_ord(self) -> Self::Output {
debug_assert!(self.is_finite(), "Non-finite weight detected: {}", self);
let x = self.to_bits() as i64;
x ^ (((x >> 63) as u64) >> 1) as i64
}
}
#[test]
fn test_into_ord_f64() {
let ord = |x: f64| x.into_ord();
assert!(ord(0.0) > ord(-1.0));
assert!(ord(0.0) < ord(1.0));
assert!(ord(-1.0) < ord(1.0));
assert!(ord(2.0) > ord(1.0));
assert!(ord(-2.0) < ord(-1.0));
assert!(ord(100.0) > ord(10.0));
}