Skip to main content

wrap_0_2pi

Function wrap_0_2pi 

Source
pub fn wrap_0_2pi(x: f64) -> f64
Expand description

Wraps an angle to [0, 2pi) radians.

Use for quantities that are conventionally non-negative, with the discontinuity at 0/360 degrees (midnight/noon for time-like quantities).

§Arguments

  • x - Angle in radians (any value, including negative or > 2pi)

§Returns

The equivalent angle in [0, 2pi).

§When to Use

  • Right ascension: 0h to 24h, never negative
  • Azimuth: 0 to 360 degrees, measured from north through east
  • Sidereal time: 0h to 24h
  • Mean anomaly, true anomaly: orbital angles

§Examples

use celestial_core::angle::wrap_0_2pi;
use std::f64::consts::PI;

// Negative angle -> positive equivalent
let x = wrap_0_2pi(-PI / 2.0);  // -90 deg -> 270 deg
assert!((x - 3.0 * PI / 2.0).abs() < 1e-10);

// Angle > 2pi -> reduced
let y = wrap_0_2pi(5.0 * PI);  // 900 deg -> 180 deg
assert!((y - PI).abs() < 1e-10);

// Already in range: unchanged
let z = wrap_0_2pi(1.0);
assert!((z - 1.0).abs() < 1e-10);

§Algorithm

  1. Reduce to (-2pi, +2pi) via fmod(x, 2pi)
  2. If result is negative, add 2pi to make it positive