improve wrap function

This commit is contained in:
Simon Gardling 2025-03-27 23:49:44 -04:00
parent a60847ad6f
commit 6d6794456e
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -1,6 +1,6 @@
#[inline] #[inline]
pub fn wrap(x: f32, max: f32) -> f32 { pub fn wrap(x: f32, max: f32) -> f32 {
x - max * ((x > max) as i32 as f32 - (x < 0.0_f32) as i32 as f32) x - max * ((x > max) as i32 - x.is_sign_negative() as i32) as f32
} }
/// Truncate x and y and return a corresponding index into the data slice. /// Truncate x and y and return a corresponding index into the data slice.
@ -18,7 +18,7 @@ mod test {
#[test] #[test]
fn wrap_test() { fn wrap_test() {
assert_eq!(wrap(1.1, 1.0), 0.100000024); assert_eq!(wrap(1.1, 1.0), 0.100000024); // floating point weirdness
assert_eq!(wrap(0.5, 1.0), 0.5); assert_eq!(wrap(0.5, 1.0), 0.5);
assert_eq!(wrap(-1.0, 2.0), 1.0); assert_eq!(wrap(-1.0, 2.0), 1.0);
} }