Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 98ae0df

Browse files
authored
Merge pull request #179 from m1el/issue178_exp2_wrap
Fixed u32 overflow in exp2
2 parents d19f45a + cc5255e commit 98ae0df

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/math/exp2.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ pub fn exp2(mut x: f64) -> f64 {
373373
/* Reduce x, computing z, i0, and k. */
374374
let ui = f64::to_bits(x + redux);
375375
let mut i0 = ui as u32;
376-
i0 += TBLSIZE as u32 / 2;
376+
i0 = i0.wrapping_add(TBLSIZE as u32 / 2);
377377
let ku = i0 / TBLSIZE as u32 * TBLSIZE as u32;
378378
let ki = ku as i32 / TBLSIZE as i32;
379379
i0 %= TBLSIZE as u32;
@@ -387,3 +387,9 @@ pub fn exp2(mut x: f64) -> f64 {
387387

388388
scalbn(r, ki)
389389
}
390+
391+
#[test]
392+
fn i0_wrap_test() {
393+
let x = -3.0 / 256.0;
394+
assert_eq!(exp2(x), f64::from_bits(0x3fefbdba3692d514));
395+
}

0 commit comments

Comments
 (0)