Skip to main content

wrap_position

Function wrap_position 

Source
pub fn wrap_position(p: f64, circumference: f64) -> f64
Expand description

Normalize a position into [0, circumference).

Uses f64::rem_euclid so negative inputs wrap correctly (unlike % which preserves the sign). The >= circumference guard handles the rare case where rem_euclid rounds a tiny negative input up to exactly circumference due to floating-point precision loss — without it the [0, C) invariant would be silently violated.

Returns the input unchanged when circumference <= 0.0 or when p is non-finite.

assert_eq!(wrap_position(0.0, 100.0), 0.0);
assert_eq!(wrap_position(50.0, 100.0), 50.0);
assert_eq!(wrap_position(100.0, 100.0), 0.0);
assert_eq!(wrap_position(125.0, 100.0), 25.0);
assert_eq!(wrap_position(-25.0, 100.0), 75.0);
assert_eq!(wrap_position(-100.0, 100.0), 0.0);