From 9c4332e2860dccc6c58645cf4ebce280416c872c Mon Sep 17 00:00:00 2001 From: Igor null Date: Tue, 16 Oct 2018 10:26:39 +0300 Subject: [PATCH 1/2] fixed uint underflow in floorf for negative exponents --- src/math/floorf.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/math/floorf.rs b/src/math/floorf.rs index 8699be060..7d631df02 100644 --- a/src/math/floorf.rs +++ b/src/math/floorf.rs @@ -12,7 +12,7 @@ pub fn floorf(x: f32) -> f32 { } } let mut ui = x.to_bits(); - let e = (((ui >> 23) & 0xff) - 0x7f) as i32; + let e = (((ui >> 23) as i32) & 0xff) - 0x7f; if e >= 23 { return x; From ce7dd93907cdfe6cda65795cb2010623a6bc179f Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 2 May 2019 12:34:05 -0700 Subject: [PATCH 2/2] Add a test that overflow does not happen --- src/math/floorf.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/math/floorf.rs b/src/math/floorf.rs index 7d631df02..8a63874eb 100644 --- a/src/math/floorf.rs +++ b/src/math/floorf.rs @@ -37,3 +37,11 @@ pub fn floorf(x: f32) -> f32 { } return f32::from_bits(ui); } + +#[cfg(test)] +mod tests { + #[test] + fn no_overflow() { + assert_eq!(super::floorf(0.5), 0.0); + } +}