| 1 |
// Copyright 2014-2018 Optimal Computing (NZ) Ltd. |
| 2 |
// Licensed under the MIT license. See LICENSE for details. |
| 3 |
|
| 4 |
#[cfg(feature="num_traits")] |
| 5 |
use num_traits::NumCast; |
| 6 |
|
| 7 |
/// A trait for floating point numbers which computes the number of representable |
| 8 |
/// values or ULPs (Units of Least Precision) that separate the two given values. |
| 9 |
#[cfg(feature="num_traits")] |
| 10 |
pub trait Ulps { |
| 11 |
type U: Copy + NumCast; |
| 12 |
|
| 13 |
/// The number of representable values or ULPs (Units of Least Precision) that |
| 14 |
/// separate `self` and `other`. The result `U` is an integral value, and will |
| 15 |
/// be zero if `self` and `other` are exactly equal. |
| 16 |
fn ulps(&self, other: &Self) -> <Self as Ulps>::U; |
| 17 |
|
| 18 |
/// The next representable number above this one |
| 19 |
fn next(&self) -> Self; |
| 20 |
|
| 21 |
/// The previous representable number below this one |
| 22 |
fn prev(&self) -> Self; |
| 23 |
} |
| 24 |
|
| 25 |
#[cfg(not(feature="num_traits"))] |
| 26 |
pub trait Ulps { |
| 27 |
type U: Copy; |
| 28 |
|
| 29 |
/// The number of representable values or ULPs (Units of Least Precision) that |
| 30 |
/// separate `self` and `other`. The result `U` is an integral value, and will |
| 31 |
/// be zero if `self` and `other` are exactly equal. |
| 32 |
fn ulps(&self, other: &Self) -> <Self as Ulps>::U; |
| 33 |
|
| 34 |
/// The next representable number above this one |
| 35 |
fn next(&self) -> Self; |
| 36 |
|
| 37 |
/// The previous representable number below this one |
| 38 |
fn prev(&self) -> Self; |
| 39 |
} |
| 40 |
|
| 41 |
impl Ulps for f32 { |
| 42 |
type U = i32; |
| 43 |
|
| 44 |
fn ulps(&self, other: &f32) -> i32 { |
| 45 |
|
| 46 |
// IEEE754 defined floating point storage representation to |
| 47 |
// maintain their order when their bit patterns are interpreted as |
| 48 |
// integers. This is a huge boon to the task at hand, as we can |
| 49 |
// reinterpret them as integers to find out how many ULPs apart any |
| 50 |
// two floats are |
| 51 |
|
| 52 |
// Setup integer representations of the input |
| 53 |
let ai32: i32 = self.to_bits() as i32; |
| 54 |
let bi32: i32 = other.to_bits() as i32; |
| 55 |
|
| 56 |
ai32.wrapping_sub(bi32) |
| 57 |
} |
| 58 |
|
| 59 |
fn next(&self) -> Self { |
| 60 |
if self.is_infinite() && *self > 0.0 { |
| 61 |
*self |
| 62 |
} else if *self == -0.0 && self.is_sign_negative() { |
| 63 |
0.0 |
| 64 |
} else { |
| 65 |
let mut u = self.to_bits(); |
| 66 |
if *self >= 0.0 { |
| 67 |
u += 1; |
| 68 |
} else { |
| 69 |
u -= 1; |
| 70 |
} |
| 71 |
f32::from_bits(u) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
fn prev(&self) -> Self { |
| 76 |
if self.is_infinite() && *self < 0.0 { |
| 77 |
*self |
| 78 |
} else if *self == 0.0 && self.is_sign_positive() { |
| 79 |
-0.0 |
| 80 |
} else { |
| 81 |
let mut u = self.to_bits(); |
| 82 |
if *self <= -0.0 { |
| 83 |
u += 1; |
| 84 |
} else { |
| 85 |
u -= 1; |
| 86 |
} |
| 87 |
f32::from_bits(u) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn f32_ulps_test1() { |
| 94 |
let x: f32 = 1000000_f32; |
| 95 |
let y: f32 = 1000000.1_f32; |
| 96 |
println!("DIST IS {}",x.ulps(&y)); |
| 97 |
assert!(x.ulps(&y) == -2); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn f32_ulps_test2() { |
| 102 |
let pzero: f32 = f32::from_bits(0x00000000_u32); |
| 103 |
let nzero: f32 = f32::from_bits(0x80000000_u32); |
| 104 |
println!("DIST IS {}",pzero.ulps(&nzero)); |
| 105 |
assert!(pzero.ulps(&nzero) == -2147483648); |
| 106 |
} |
| 107 |
#[test] |
| 108 |
fn f32_ulps_test3() { |
| 109 |
let pinf: f32 = f32::from_bits(0x7f800000_u32); |
| 110 |
let ninf: f32 = f32::from_bits(0xff800000_u32); |
| 111 |
println!("DIST IS {}",pinf.ulps(&ninf)); |
| 112 |
assert!(pinf.ulps(&ninf) == -2147483648); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn f32_ulps_test4() { |
| 117 |
let x: f32 = f32::from_bits(0x63a7f026_u32); |
| 118 |
let y: f32 = f32::from_bits(0x63a7f023_u32); |
| 119 |
println!("DIST IS {}",x.ulps(&y)); |
| 120 |
assert!(x.ulps(&y) == 3); |
| 121 |
} |
| 122 |
|
| 123 |
#[test] |
| 124 |
fn f32_ulps_test5() { |
| 125 |
let x: f32 = 2.0; |
| 126 |
let ulps: i32 = x.to_bits() as i32; |
| 127 |
let x2: f32 = <f32>::from_bits(ulps as u32); |
| 128 |
assert_eq!(x, x2); |
| 129 |
} |
| 130 |
|
| 131 |
#[test] |
| 132 |
fn f32_ulps_test6() { |
| 133 |
let negzero: f32 = -0.; |
| 134 |
let zero: f32 = 0.; |
| 135 |
assert_eq!(negzero.next(), zero); |
| 136 |
assert_eq!(zero.prev(), negzero); |
| 137 |
assert!(negzero.prev() < 0.0); |
| 138 |
assert!(zero.next() > 0.0); |
| 139 |
} |
| 140 |
|
| 141 |
impl Ulps for f64 { |
| 142 |
type U = i64; |
| 143 |
|
| 144 |
fn ulps(&self, other: &f64) -> i64 { |
| 145 |
|
| 146 |
// IEEE754 defined floating point storage representation to |
| 147 |
// maintain their order when their bit patterns are interpreted as |
| 148 |
// integers. This is a huge boon to the task at hand, as we can |
| 149 |
// reinterpret them as integers to find out how many ULPs apart any |
| 150 |
// two floats are |
| 151 |
|
| 152 |
// Setup integer representations of the input |
| 153 |
let ai64: i64 = self.to_bits() as i64; |
| 154 |
let bi64: i64 = other.to_bits() as i64; |
| 155 |
|
| 156 |
ai64.wrapping_sub(bi64) |
| 157 |
} |
| 158 |
|
| 159 |
fn next(&self) -> Self { |
| 160 |
if self.is_infinite() && *self > 0.0 { |
| 161 |
*self |
| 162 |
} else if *self == -0.0 && self.is_sign_negative() { |
| 163 |
0.0 |
| 164 |
} else { |
| 165 |
let mut u = self.to_bits(); |
| 166 |
if *self >= 0.0 { |
| 167 |
u += 1; |
| 168 |
} else { |
| 169 |
u -= 1; |
| 170 |
} |
| 171 |
f64::from_bits(u) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
fn prev(&self) -> Self { |
| 176 |
if self.is_infinite() && *self < 0.0 { |
| 177 |
*self |
| 178 |
} else if *self == 0.0 && self.is_sign_positive() { |
| 179 |
-0.0 |
| 180 |
} else { |
| 181 |
let mut u = self.to_bits(); |
| 182 |
if *self <= -0.0 { |
| 183 |
u += 1; |
| 184 |
} else { |
| 185 |
u -= 1; |
| 186 |
} |
| 187 |
f64::from_bits(u) |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
#[test] |
| 193 |
fn f64_ulps_test1() { |
| 194 |
let x: f64 = 1000000_f64; |
| 195 |
let y: f64 = 1000000.00000001_f64; |
| 196 |
println!("DIST IS {}",x.ulps(&y)); |
| 197 |
assert!(x.ulps(&y) == -86); |
| 198 |
} |
| 199 |
|
| 200 |
#[test] |
| 201 |
fn f64_ulps_test2() { |
| 202 |
let pzero: f64 = f64::from_bits(0x0000000000000000_u64); |
| 203 |
let nzero: f64 = f64::from_bits(0x8000000000000000_u64); |
| 204 |
println!("DIST IS {}",pzero.ulps(&nzero)); |
| 205 |
assert!(pzero.ulps(&nzero) == -9223372036854775808i64); |
| 206 |
} |
| 207 |
#[test] |
| 208 |
fn f64_ulps_test3() { |
| 209 |
let pinf: f64 = f64::from_bits(0x7f80000000000000_u64); |
| 210 |
let ninf: f64 = f64::from_bits(0xff80000000000000_u64); |
| 211 |
println!("DIST IS {}",pinf.ulps(&ninf)); |
| 212 |
assert!(pinf.ulps(&ninf) == -9223372036854775808i64); |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn f64_ulps_test4() { |
| 217 |
let x: f64 = f64::from_bits(0xd017f6cc63a7f026_u64); |
| 218 |
let y: f64 = f64::from_bits(0xd017f6cc63a7f023_u64); |
| 219 |
println!("DIST IS {}",x.ulps(&y)); |
| 220 |
assert!(x.ulps(&y) == 3); |
| 221 |
} |
| 222 |
|
| 223 |
#[test] |
| 224 |
fn f64_ulps_test5() { |
| 225 |
let x: f64 = 2.0; |
| 226 |
let ulps: i64 = x.to_bits() as i64; |
| 227 |
let x2: f64 = <f64>::from_bits(ulps as u64); |
| 228 |
assert_eq!(x, x2); |
| 229 |
} |
| 230 |
|
| 231 |
#[test] |
| 232 |
fn f64_ulps_test6() { |
| 233 |
let negzero: f64 = -0.; |
| 234 |
let zero: f64 = 0.; |
| 235 |
assert_eq!(negzero.next(), zero); |
| 236 |
assert_eq!(zero.prev(), negzero); |
| 237 |
assert!(negzero.prev() < 0.0); |
| 238 |
assert!(zero.next() > 0.0); |
| 239 |
} |
| 240 |
|