1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//! Angle normalization for astronomical coordinate systems.
//!
//! Different astronomical quantities require different angular ranges:
//!
//! | Quantity | Range | Function |
//! |----------|-------|----------|
//! | Right Ascension | [0, 2pi) | [`wrap_0_2pi`] |
//! | Hour Angle | [-pi, +pi) | [`wrap_pm_pi`] |
//! | Longitude (celestial) | [-pi, +pi) | [`wrap_pm_pi`] |
//! | Declination | [-pi/2, +pi/2] | [`clamp_dec`] |
//! | Latitude | [-pi/2, +pi/2] | [`clamp_dec`] |
//!
//! # Wrapping vs Clamping
//!
//! **Wrapping** preserves the direction on the sphere. An angle of 370 degrees
//! represents the same direction as 10 degrees, so `wrap_0_2pi` returns 10 degrees.
//!
//! **Clamping** enforces physical limits. Declination cannot exceed +/-90 degrees
//! because you cannot go "past" the pole. [`clamp_dec`] enforces this by saturating
//! at the limits rather than wrapping.
//!
//! # Why Two Wrapping Functions?
//!
//! Right ascension and hour angle are both cyclic, but their conventions differ:
//!
//! - **Right Ascension** uses [0, 24h) or [0, 360 deg) because negative RA makes no sense.
//! Stars at RA = 23h 59m are close to RA = 0h 01m on the sky.
//!
//! - **Hour Angle** uses [-12h, +12h) because it represents "hours from meridian."
//! Negative means east of meridian (not yet crossed), positive means west (already crossed).
//! The discontinuity at +/-180 degrees is at the anti-meridian, far from the observing position.
//!
//! - **Celestial Longitude** (e.g., in galactic or ecliptic coordinates) typically uses
//! [-180, +180) to center the "interesting" region (galactic center, vernal equinox)
//! at zero, with the discontinuity 180 degrees away.
//!
//! # Example
//!
//! ```
//! use celestial_core::angle::{wrap_0_2pi, wrap_pm_pi, clamp_dec};
//! use std::f64::consts::PI;
//!
//! // Right ascension: always positive
//! let ra = wrap_0_2pi(-0.5); // -0.5 rad -> ~5.78 rad
//! assert!(ra > 0.0 && ra < 2.0 * PI);
//!
//! // Hour angle: centered on zero
//! let ha = wrap_pm_pi(3.5); // 3.5 rad -> ~-2.78 rad (wrapped)
//! assert!(ha >= -PI && ha < PI);
//!
//! // Declination: cannot exceed poles
//! let dec = clamp_dec(2.0); // 2.0 rad -> pi/2 (clamped to +90 deg)
//! assert!((dec - PI / 2.0).abs() < 1e-10);
//! ```
//!
//! # Algorithm Notes
//!
//! The wrapping functions use `libm::fmod` (via [`crate::math::fmod`]) rather than
//! the `%` operator because Rust's `%` is a remainder, not a modulo. For negative
//! numbers, remainder and modulo differ:
//!
//! - `-1.0 % 360.0` = `-1.0` (remainder, keeps sign of dividend)
//! - `fmod(-1.0, 360.0)` = `-1.0` (same as %, but well-defined for floats)
//!
//! After `fmod`, we adjust for the desired range.
use crate;
use cratefmod;
/// Specifies which normalization convention to apply.
///
/// Used when a function needs to normalize angles but the appropriate range
/// depends on what the angle represents.
/// Wraps an angle to [-pi, +pi) radians.
///
/// Use for quantities where the discontinuity should be at +/-180 degrees
/// (the "back" of the circle), not at 0/360 degrees.
///
/// # Arguments
///
/// * `x` - Angle in radians (any value, including negative or > 2pi)
///
/// # Returns
///
/// The equivalent angle in [-pi, +pi).
///
/// # When to Use
///
/// - **Hour angle**: hours from meridian, negative = east, positive = west
/// - **Longitude differences**: shortest arc between two longitudes
/// - **Galactic/ecliptic longitude**: if you want galactic center at l=0
/// - **Position angle differences**: relative rotation between two frames
///
/// # Examples
///
/// ```
/// use celestial_core::angle::wrap_pm_pi;
/// use std::f64::consts::PI;
///
/// // 270 degrees -> -90 degrees
/// let x = wrap_pm_pi(3.0 * PI / 2.0);
/// assert!((x - (-PI / 2.0)).abs() < 1e-10);
///
/// // -270 degrees -> +90 degrees
/// let y = wrap_pm_pi(-3.0 * PI / 2.0);
/// assert!((y - (PI / 2.0)).abs() < 1e-10);
///
/// // Already in range: unchanged
/// let z = wrap_pm_pi(1.0);
/// assert!((z - 1.0).abs() < 1e-10);
/// ```
///
/// # Algorithm
///
/// 1. Reduce to [-2pi, +2pi) via `fmod(x, 2pi)`
/// 2. If result is >= pi or <= -pi, subtract/add 2pi to bring into range
/// 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
/// Clamps an angle to [-pi/2, +pi/2] radians (i.e., [-90, +90] degrees).
///
/// Unlike wrapping, clamping saturates at the limits. This is appropriate for
/// quantities that have physical bounds you cannot exceed.
///
/// # Arguments
///
/// * `x` - Angle in radians
///
/// # Returns
///
/// The angle clamped to [-pi/2, +pi/2].
///
/// # When to Use
///
/// - **Declination**: celestial latitude, poles are at +/-90 degrees
/// - **Geographic latitude**: -90 (south pole) to +90 (north pole)
/// - **Altitude**: horizon at 0, zenith at +90 (though altitude can be negative)
///
/// # Why Clamp Instead of Wrap?
///
/// You cannot go "past" the north pole by walking north. If you try, you end up
/// walking south on the other side. This is fundamentally different from longitude,
/// where walking east forever eventually brings you back to where you started.
///
/// Clamping is a safety mechanism. If your calculation produces declination = 100 degrees,
/// something is wrong upstream. Clamping to 90 degrees prevents downstream code from
/// breaking, but you should investigate why the input was out of range.
///
/// # Examples
///
/// ```
/// use celestial_core::angle::clamp_dec;
/// use std::f64::consts::FRAC_PI_2;
///
/// // Within range: unchanged
/// let x = clamp_dec(0.5);
/// assert!((x - 0.5).abs() < 1e-10);
///
/// // Above +90 degrees: clamped to +90
/// let y = clamp_dec(2.0);
/// assert!((y - FRAC_PI_2).abs() < 1e-10);
///
/// // Below -90 degrees: clamped to -90
/// let z = clamp_dec(-2.0);
/// assert!((z - (-FRAC_PI_2)).abs() < 1e-10);
/// ```
///
/// # See Also
///
/// For declinations that might legitimately exceed +/-90 degrees (e.g., pier-flipped
/// telescope positions), see the validation functions in the [`validate`](super::validate)
/// module which offer extended range options.