From 5d27461fa633f261a6af037bac9b8ce0953a25ba Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Sun, 1 Nov 2026 02:55:58 +0100 Subject: [PATCH] Keep Xfrm::rot_norm within 0..360 for tiny negative rotations Found while testing crates/geom/src/lib.rs:155-158 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/geom/src/lib.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/geom/src/lib.rs b/crates/geom/src/lib.rs index 3be72e03..6f695375 100644 --- a/crates/geom/src/lib.rs +++ b/crates/geom/src/lib.rs @@ -153,8 +153,8 @@ impl Xfrm { } /// Rotation normalised to 0..360. pub fn rot_norm(&self) -> f64 { - let r = self.rot % 360.0; - if r < 0.0 { r + 360.0 } else { r } + let r = self.rot.rem_euclid(360.0); + if r >= 360.0 { 0.0 } else { r } } } @@ -266,6 +266,14 @@ mod tests { assert!((p.x - 300.0).abs() < 1e-9 && (p.y - 200.0).abs() < 1e-9); } + #[test] + fn rot_norm_stays_below_360() { + for rot in [-1e-15, -1e-13, -f64::EPSILON, -0.0, 360.0, 720.0, -360.0] { + let n = Xfrm { rot, ..Default::default() }.rot_norm(); + assert!((0.0..360.0).contains(&n), "rot {rot} -> {n}"); + } + } + #[test] fn segment_distance() { assert!((dist_to_segment(Point::new(5.0, 5.0), Point::new(0.0, 0.0), Point::new(10.0, 0.0)) - 5.0).abs() < 1e-9);