pub fn shift_nearest(x: f64, target: f64, period: f64) -> f64Expand description
The whole-period shift that moves x nearest to target: k·period
with k the integer nearest (target − x) / period (halves away from
zero). Adding it to x is the translate of x that lies closest to
target; it is 0.0 when x already is.
use arris_math::shift_nearest;
use core::f64::consts::TAU;
assert_eq!(shift_nearest(0.5, 0.5 + TAU, TAU), TAU);
assert_eq!(shift_nearest(0.5, 0.5 - 2.0 * TAU, TAU), -2.0 * TAU);
assert_eq!(shift_nearest(1.0, 1.4, TAU), 0.0);
// A period that is not a turn.
assert_eq!(shift_nearest(0.0, 7.0, 3.0), 6.0);