Skip to content

Commit 7811f92

Browse files
author
Jorge Aparicio
authored
Merge pull request #50 from japaric/post-43
use utility function to compare the repr of floats
2 parents 10fdc9b + 2e561b3 commit 7811f92

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

src/float/add.rs

+13-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use core::mem;
12
use core::num::Wrapping;
3+
24
use float::Float;
35

46
macro_rules! add {
@@ -74,7 +76,7 @@ macro_rules! add {
7476

7577
// Swap a and b if necessary so that a has the larger absolute value.
7678
if b_abs > a_abs {
77-
::core::mem::swap(&mut a_rep, &mut b_rep);
79+
mem::swap(&mut a_rep, &mut b_rep);
7880
}
7981

8082
// Extract the exponent and significand from the (possibly swapped) a and b.
@@ -212,22 +214,14 @@ mod tests {
212214
let (a, b) = (f32::from_repr(a.0), f32::from_repr(b.0));
213215
let x = super::__addsf3(a, b);
214216
let y = a + b;
215-
if !(x.is_nan() && y.is_nan()) {
216-
x.repr() == y.repr()
217-
} else {
218-
true
219-
}
217+
x.eq_repr(y)
220218
}
221219

222220
fn adddf3(a: U64, b: U64) -> bool {
223221
let (a, b) = (f64::from_repr(a.0), f64::from_repr(b.0));
224222
let x = super::__adddf3(a, b);
225223
let y = a + b;
226-
if !(x.is_nan() && y.is_nan()) {
227-
x.repr() == y.repr()
228-
} else {
229-
true
230-
}
224+
x.eq_repr(y)
231225
}
232226
}
233227

@@ -237,42 +231,42 @@ mod tests {
237231
fn test_float_tiny_plus_tiny() {
238232
let tiny = f32::from_repr(1);
239233
let r = super::__addsf3(tiny, tiny);
240-
assert_eq!(r, tiny + tiny);
234+
assert!(r.eq_repr(tiny + tiny));
241235
}
242236

243237
#[test]
244238
fn test_double_tiny_plus_tiny() {
245239
let tiny = f64::from_repr(1);
246240
let r = super::__adddf3(tiny, tiny);
247-
assert_eq!(r, tiny + tiny);
241+
assert!(r.eq_repr(tiny + tiny));
248242
}
249243

250244
#[test]
251245
fn test_float_small_plus_small() {
252246
let a = f32::from_repr(327);
253247
let b = f32::from_repr(256);
254248
let r = super::__addsf3(a, b);
255-
assert_eq!(r, a + b);
249+
assert!(r.eq_repr(a + b));
256250
}
257251

258252
#[test]
259253
fn test_double_small_plus_small() {
260254
let a = f64::from_repr(327);
261255
let b = f64::from_repr(256);
262256
let r = super::__adddf3(a, b);
263-
assert_eq!(r, a + b);
257+
assert!(r.eq_repr(a + b));
264258
}
265259

266260
#[test]
267261
fn test_float_one_plus_one() {
268262
let r = super::__addsf3(1f32, 1f32);
269-
assert_eq!(r, 1f32 + 1f32);
263+
assert!(r.eq_repr(1f32 + 1f32));
270264
}
271265

272266
#[test]
273267
fn test_double_one_plus_one() {
274268
let r = super::__adddf3(1f64, 1f64);
275-
assert_eq!(r, 1f64 + 1f64);
269+
assert!(r.eq_repr(1f64 + 1f64));
276270
}
277271

278272
#[test]
@@ -281,9 +275,7 @@ mod tests {
281275
let b = f32::from_repr(0b11111111100100010001001010101010);
282276
let x = super::__addsf3(a, b);
283277
let y = a + b;
284-
if !(x.is_nan() && y.is_nan()) {
285-
assert_eq!(x.repr(), y.repr());
286-
}
278+
assert!(x.eq_repr(y));
287279
}
288280

289281
#[test]
@@ -293,9 +285,7 @@ mod tests {
293285
0b1111111111110010001000100101010101001000101010000110100011101011);
294286
let x = super::__adddf3(a, b);
295287
let y = a + b;
296-
if !(x.is_nan() && y.is_nan()) {
297-
assert_eq!(x.repr(), y.repr());
298-
}
288+
assert!(x.eq_repr(y));
299289
}
300290

301291
#[test]

src/float/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ pub trait Float: Sized {
1616
/// Returns `self` transmuted to `Self::Int`
1717
fn repr(self) -> Self::Int;
1818

19+
#[cfg(test)]
20+
/// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
21+
/// represented in multiple different ways. This methods returns `true` if two NaNs are
22+
/// compared.
23+
fn eq_repr(self, rhs: Self) -> bool;
24+
1925
/// Returns a `Self::Int` transmuted back to `Self`
2026
fn from_repr(a: Self::Int) -> Self;
2127

@@ -34,6 +40,14 @@ impl Float for f32 {
3440
fn repr(self) -> Self::Int {
3541
unsafe { mem::transmute(self) }
3642
}
43+
#[cfg(test)]
44+
fn eq_repr(self, rhs: Self) -> bool {
45+
if self.is_nan() && rhs.is_nan() {
46+
true
47+
} else {
48+
self.repr() == rhs.repr()
49+
}
50+
}
3751
fn from_repr(a: Self::Int) -> Self {
3852
unsafe { mem::transmute(a) }
3953
}
@@ -54,6 +68,14 @@ impl Float for f64 {
5468
fn repr(self) -> Self::Int {
5569
unsafe { mem::transmute(self) }
5670
}
71+
#[cfg(test)]
72+
fn eq_repr(self, rhs: Self) -> bool {
73+
if self.is_nan() && rhs.is_nan() {
74+
true
75+
} else {
76+
self.repr() == rhs.repr()
77+
}
78+
}
5779
fn from_repr(a: Self::Int) -> Self {
5880
unsafe { mem::transmute(a) }
5981
}

0 commit comments

Comments
 (0)