Merge pull request #179 from m1el/issue178_exp2_wrap

Fixed u32 overflow in exp2
This commit is contained in:
Alex Crichton 2019-06-03 09:26:42 -05:00 committed by GitHub
commit 98ae0dfd7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -373,7 +373,7 @@ pub fn exp2(mut x: f64) -> f64 {
/* Reduce x, computing z, i0, and k. */
let ui = f64::to_bits(x + redux);
let mut i0 = ui as u32;
i0 += TBLSIZE as u32 / 2;
i0 = i0.wrapping_add(TBLSIZE as u32 / 2);
let ku = i0 / TBLSIZE as u32 * TBLSIZE as u32;
let ki = ku as i32 / TBLSIZE as i32;
i0 %= TBLSIZE as u32;
@ -387,3 +387,9 @@ pub fn exp2(mut x: f64) -> f64 {
scalbn(r, ki)
}
#[test]
fn i0_wrap_test() {
let x = -3.0 / 256.0;
assert_eq!(exp2(x), f64::from_bits(0x3fefbdba3692d514));
}