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
use std::mem;
use num_traits::NumCast;
pub trait Ulps {
type U: Copy + NumCast;
fn ulps(&self, other: &Self) -> <Self as Ulps>::U;
}
impl Ulps for f32 {
type U = i32;
fn ulps(&self, other: &f32) -> i32 {
let ai32: i32 = unsafe { mem::transmute::<f32,i32>(*self) };
let bi32: i32 = unsafe { mem::transmute::<f32,i32>(*other) };
ai32.wrapping_sub(bi32)
}
}
#[test]
fn f32_ulps_test1() {
let x: f32 = 1000000_f32;
let y: f32 = 1000000.1_f32;
println!("DIST IS {}",x.ulps(&y));
assert!(x.ulps(&y) == -2);
}
#[test]
fn f32_ulps_test2() {
let pzero: f32 = unsafe { mem::transmute(0x00000000_u32) };
let nzero: f32 = unsafe { mem::transmute(0x80000000_u32) };
println!("DIST IS {}",pzero.ulps(&nzero));
assert!(pzero.ulps(&nzero) == -2147483648);
}
#[test]
fn f32_ulps_test3() {
let pinf: f32 = unsafe { mem::transmute(0x7f800000_u32) };
let ninf: f32 = unsafe { mem::transmute(0xff800000_u32) };
println!("DIST IS {}",pinf.ulps(&ninf));
assert!(pinf.ulps(&ninf) == -2147483648);
}
#[test]
fn f32_ulps_test4() {
let x: f32 = unsafe { mem::transmute(0x63a7f026_u32) };
let y: f32 = unsafe { mem::transmute(0x63a7f023_u32) };
println!("DIST IS {}",x.ulps(&y));
assert!(x.ulps(&y) == 3);
}
#[test]
fn f32_ulps_test5() {
let x: f32 = 2.0;
let ulps: i32 = unsafe { mem::transmute(x) };
let x2: f32 = unsafe { mem::transmute(ulps) };
assert_eq!(x, x2);
}
impl Ulps for f64 {
type U = i64;
fn ulps(&self, other: &f64) -> i64 {
let ai64: i64 = unsafe { mem::transmute::<f64,i64>(*self) };
let bi64: i64 = unsafe { mem::transmute::<f64,i64>(*other) };
ai64.wrapping_sub(bi64)
}
}
#[test]
fn f64_ulps_test1() {
let x: f64 = 1000000_f64;
let y: f64 = 1000000.00000001_f64;
println!("DIST IS {}",x.ulps(&y));
assert!(x.ulps(&y) == -86);
}
#[test]
fn f64_ulps_test2() {
let pzero: f64 = unsafe { mem::transmute(0x0000000000000000_u64) };
let nzero: f64 = unsafe { mem::transmute(0x8000000000000000_u64) };
println!("DIST IS {}",pzero.ulps(&nzero));
assert!(pzero.ulps(&nzero) == -9223372036854775808i64);
}
#[test]
fn f64_ulps_test3() {
let pinf: f64 = unsafe { mem::transmute(0x7f80000000000000_u64) };
let ninf: f64 = unsafe { mem::transmute(0xff80000000000000_u64) };
println!("DIST IS {}",pinf.ulps(&ninf));
assert!(pinf.ulps(&ninf) == -9223372036854775808i64);
}
#[test]
fn f64_ulps_test4() {
let x: f64 = unsafe { mem::transmute(0xd017f6cc63a7f026_u64) };
let y: f64 = unsafe { mem::transmute(0xd017f6cc63a7f023_u64) };
println!("DIST IS {}",x.ulps(&y));
assert!(x.ulps(&y) == 3);
}
#[test]
fn f64_ulps_test5() {
let x: f64 = 2.0;
let ulps: i64 = unsafe { mem::transmute(x) };
let x2: f64 = unsafe { mem::transmute(ulps) };
assert_eq!(x, x2);
}