Skip to content

use utility function to compare the repr of floats #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 13 additions & 23 deletions src/float/add.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use core::mem;
use core::num::Wrapping;

use float::Float;

macro_rules! add {
Expand Down Expand Up @@ -74,7 +76,7 @@ macro_rules! add {

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

// Extract the exponent and significand from the (possibly swapped) a and b.
Expand Down Expand Up @@ -212,22 +214,14 @@ mod tests {
let (a, b) = (f32::from_repr(a.0), f32::from_repr(b.0));
let x = super::__addsf3(a, b);
let y = a + b;
if !(x.is_nan() && y.is_nan()) {
x.repr() == y.repr()
} else {
true
}
x.eq_repr(y)
}

fn adddf3(a: U64, b: U64) -> bool {
let (a, b) = (f64::from_repr(a.0), f64::from_repr(b.0));
let x = super::__adddf3(a, b);
let y = a + b;
if !(x.is_nan() && y.is_nan()) {
x.repr() == y.repr()
} else {
true
}
x.eq_repr(y)
}
}

Expand All @@ -237,42 +231,42 @@ mod tests {
fn test_float_tiny_plus_tiny() {
let tiny = f32::from_repr(1);
let r = super::__addsf3(tiny, tiny);
assert_eq!(r, tiny + tiny);
assert!(r.eq_repr(tiny + tiny));
}

#[test]
fn test_double_tiny_plus_tiny() {
let tiny = f64::from_repr(1);
let r = super::__adddf3(tiny, tiny);
assert_eq!(r, tiny + tiny);
assert!(r.eq_repr(tiny + tiny));
}

#[test]
fn test_float_small_plus_small() {
let a = f32::from_repr(327);
let b = f32::from_repr(256);
let r = super::__addsf3(a, b);
assert_eq!(r, a + b);
assert!(r.eq_repr(a + b));
}

#[test]
fn test_double_small_plus_small() {
let a = f64::from_repr(327);
let b = f64::from_repr(256);
let r = super::__adddf3(a, b);
assert_eq!(r, a + b);
assert!(r.eq_repr(a + b));
}

#[test]
fn test_float_one_plus_one() {
let r = super::__addsf3(1f32, 1f32);
assert_eq!(r, 1f32 + 1f32);
assert!(r.eq_repr(1f32 + 1f32));
}

#[test]
fn test_double_one_plus_one() {
let r = super::__adddf3(1f64, 1f64);
assert_eq!(r, 1f64 + 1f64);
assert!(r.eq_repr(1f64 + 1f64));
}

#[test]
Expand All @@ -281,9 +275,7 @@ mod tests {
let b = f32::from_repr(0b11111111100100010001001010101010);
let x = super::__addsf3(a, b);
let y = a + b;
if !(x.is_nan() && y.is_nan()) {
assert_eq!(x.repr(), y.repr());
}
assert!(x.eq_repr(y));
}

#[test]
Expand All @@ -293,9 +285,7 @@ mod tests {
0b1111111111110010001000100101010101001000101010000110100011101011);
let x = super::__adddf3(a, b);
let y = a + b;
if !(x.is_nan() && y.is_nan()) {
assert_eq!(x.repr(), y.repr());
}
assert!(x.eq_repr(y));
}

#[test]
Expand Down
22 changes: 22 additions & 0 deletions src/float/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ pub trait Float: Sized {
/// Returns `self` transmuted to `Self::Int`
fn repr(self) -> Self::Int;

#[cfg(test)]
/// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
/// represented in multiple different ways. This methods returns `true` if two NaNs are
/// compared.
fn eq_repr(self, rhs: Self) -> bool;

/// Returns a `Self::Int` transmuted back to `Self`
fn from_repr(a: Self::Int) -> Self;

Expand All @@ -34,6 +40,14 @@ impl Float for f32 {
fn repr(self) -> Self::Int {
unsafe { mem::transmute(self) }
}
#[cfg(test)]
fn eq_repr(self, rhs: Self) -> bool {
if self.is_nan() && rhs.is_nan() {
true
} else {
self.repr() == rhs.repr()
}
}
fn from_repr(a: Self::Int) -> Self {
unsafe { mem::transmute(a) }
}
Expand All @@ -54,6 +68,14 @@ impl Float for f64 {
fn repr(self) -> Self::Int {
unsafe { mem::transmute(self) }
}
#[cfg(test)]
fn eq_repr(self, rhs: Self) -> bool {
if self.is_nan() && rhs.is_nan() {
true
} else {
self.repr() == rhs.repr()
}
}
fn from_repr(a: Self::Int) -> Self {
unsafe { mem::transmute(a) }
}
Expand Down