skymath 0.5.0

Planning-grade astronomy math for astrophotography tooling: angles, equatorial coordinates, sexagesimal parsing and formatting, angular separation and offsets, precession, MJD/JD/calendar conversions, sidereal time, and observer-local quantities (alt-az, airmass, transit).
Documentation
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Angle primitives and sexagesimal parsing/formatting.
//!
//! Provenance: [`Angle`], the sexagesimal tokenizer, and their tests are
//! extracted from the sibling crate `target-match`; the explicit
//! [`ParseMode`]/[`SexaStyle`] surface and hour normalization are new here.
//!
//! - [`Angle`] — a unit-aware angle (degrees / radians / arcminutes /
//!   arcseconds / hours) stored internally in radians.
//! - [`parse_ra`] / [`parse_dec`] — sexagesimal (or bare-decimal) parsing in
//!   [`ParseMode::Strict`] or [`ParseMode::Lenient`]. Lenient tolerates
//!   missing minutes/seconds and mixed separators; **corrupt tokens are an
//!   error in every mode** — no input is ever silently dropped.
//! - [`format_ra`] / [`format_dec`] — sexagesimal formatting with rounding
//!   carry (never emits `60` in a minutes or seconds field).
//! - [`circular_mean`] / [`circular_distance`] — circular statistics over
//!   full-turn angles (vector-sum mean, shortest-arc distance).
//!
//! ```
//! use skymath::{format_dec, format_ra, parse_dec, parse_ra, ParseMode, SexaStyle};
//!
//! // M31's canonical J2000 position.
//! let ra = parse_ra("00:42:44.3", ParseMode::Strict).unwrap();
//! let dec = parse_dec("+41:16:09", ParseMode::Strict).unwrap();
//! assert_eq!(format_ra(ra, SexaStyle::default()), "00:42:44.30");
//! assert_eq!(format_dec(dec, SexaStyle::default()), "+41:16:09.00");
//! ```

use core::f64::consts::PI;
use core::ops::{Add, Div, Mul, Neg, Sub};

use crate::error::{Error, Result};

const DEG_PER_RAD: f64 = 180.0 / PI;
const RAD_PER_DEG: f64 = PI / 180.0;
/// Exact number of arcseconds in one radian.
pub const ARCSEC_PER_RADIAN: f64 = 206_264.806_247_096_36;

// ── Angle ──────────────────────────────────────────────────────────────────────

/// A unit-aware angle, stored internally in radians.
///
/// Construction and read-out are available in degrees, radians, arcminutes,
/// arcseconds, and hours (1 hour = 15°). Normalization is explicit — an `Angle`
/// holds whatever finite value it was given until you ask for a normalized form.
///
/// ```
/// use skymath::Angle;
///
/// // M31's right ascension, 00:42:44.3, expressed in hours then read out in degrees.
/// let ra = Angle::from_hours(42.0 / 60.0 + 44.3 / 3600.0);
/// assert!((ra.degrees() - 10.685).abs() < 1e-3);
/// assert!((Angle::from_degrees(370.0).normalized_0_360().degrees() - 10.0).abs() < 1e-9);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Angle {
    radians: f64,
}

impl Angle {
    /// Construct from radians.
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// let right_angle = Angle::from_radians(std::f64::consts::FRAC_PI_2);
    /// assert!((right_angle.degrees() - 90.0).abs() < 1e-9);
    /// ```
    #[must_use]
    pub const fn from_radians(radians: f64) -> Self {
        Self { radians }
    }
    /// Construct from decimal degrees.
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_degrees(180.0).radians() - std::f64::consts::PI).abs() < 1e-12);
    /// ```
    #[must_use]
    pub fn from_degrees(degrees: f64) -> Self {
        Self {
            radians: degrees * RAD_PER_DEG,
        }
    }
    /// Construct from arcminutes (1/60 degree).
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_arcminutes(30.0).degrees() - 0.5).abs() < 1e-12);
    /// ```
    #[must_use]
    pub fn from_arcminutes(arcmin: f64) -> Self {
        Self::from_degrees(arcmin / 60.0)
    }
    /// Construct from arcseconds (1/3600 degree).
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_arcseconds(3600.0).degrees() - 1.0).abs() < 1e-9);
    /// ```
    #[must_use]
    pub fn from_arcseconds(arcsec: f64) -> Self {
        Self::from_degrees(arcsec / 3600.0)
    }
    /// Construct from hours of right ascension (1 hour = 15°).
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_hours(6.0).degrees() - 90.0).abs() < 1e-9);
    /// ```
    #[must_use]
    pub fn from_hours(hours: f64) -> Self {
        Self::from_degrees(hours * 15.0)
    }

    /// Value in radians.
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_degrees(90.0).radians() - std::f64::consts::FRAC_PI_2).abs() < 1e-12);
    /// ```
    #[must_use]
    pub const fn radians(self) -> f64 {
        self.radians
    }
    /// Value in decimal degrees.
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_radians(std::f64::consts::PI).degrees() - 180.0).abs() < 1e-9);
    /// ```
    #[must_use]
    pub fn degrees(self) -> f64 {
        self.radians * DEG_PER_RAD
    }
    /// Value in arcminutes.
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_degrees(1.0).arcminutes() - 60.0).abs() < 1e-9);
    /// ```
    #[must_use]
    pub fn arcminutes(self) -> f64 {
        self.degrees() * 60.0
    }
    /// Value in arcseconds.
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_degrees(1.0).arcseconds() - 3600.0).abs() < 1e-6);
    /// ```
    #[must_use]
    pub fn arcseconds(self) -> f64 {
        self.degrees() * 3600.0
    }
    /// Value in hours (degrees / 15).
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_degrees(90.0).hours() - 6.0).abs() < 1e-9);
    /// ```
    #[must_use]
    pub fn hours(self) -> f64 {
        self.degrees() / 15.0
    }

    /// Return an equivalent angle wrapped into `[0, 360)` degrees.
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_degrees(-10.0).normalized_0_360().degrees() - 350.0).abs() < 1e-9);
    /// ```
    #[must_use]
    pub fn normalized_0_360(self) -> Self {
        let mut d = self.degrees() % 360.0;
        if d < 0.0 {
            d += 360.0;
        }
        Self::from_degrees(d)
    }
    /// Return an equivalent angle wrapped into `(-180, 180]` degrees.
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_degrees(350.0).normalized_pm_180().degrees() + 10.0).abs() < 1e-9);
    /// ```
    #[must_use]
    pub fn normalized_pm_180(self) -> Self {
        let mut d = self.normalized_0_360().degrees();
        if d > 180.0 {
            d -= 360.0;
        }
        Self::from_degrees(d)
    }
    /// Return an equivalent angle wrapped into `[0, 24)` hours.
    ///
    /// ```
    /// use skymath::Angle;
    ///
    /// assert!((Angle::from_hours(25.0).normalized_hours().hours() - 1.0).abs() < 1e-9);
    /// ```
    #[must_use]
    pub fn normalized_hours(self) -> Self {
        self.normalized_0_360()
    }
}

impl Add for Angle {
    type Output = Angle;
    fn add(self, rhs: Angle) -> Angle {
        Angle::from_radians(self.radians + rhs.radians)
    }
}
impl Sub for Angle {
    type Output = Angle;
    fn sub(self, rhs: Angle) -> Angle {
        Angle::from_radians(self.radians - rhs.radians)
    }
}
impl Neg for Angle {
    type Output = Angle;
    fn neg(self) -> Angle {
        Angle::from_radians(-self.radians)
    }
}
impl Mul<f64> for Angle {
    type Output = Angle;
    fn mul(self, rhs: f64) -> Angle {
        Angle::from_radians(self.radians * rhs)
    }
}
impl Div<f64> for Angle {
    type Output = Angle;
    fn div(self, rhs: f64) -> Angle {
        Angle::from_radians(self.radians / rhs)
    }
}

// ── Circular statistics ────────────────────────────────────────────────────────

/// Circular (vector-sum) mean of a set of angles, normalized to
/// `[0°, 360°)`; `None` for an empty input.
///
/// Each angle contributes a unit vector; the mean is the direction of the
/// resultant, so wraparound is handled correctly — the mean of 359° and 1°
/// is 0°, not 180°. Useful for averaging right ascensions or camera rotation
/// angles, where an arithmetic mean is wrong across the 0°/360° seam.
///
/// Caution: a near-antipodal set (e.g. `[0°, 180°]`) has a near-zero
/// resultant, so while the result is always a finite angle, its direction is
/// numerically meaningless — floating-point residue decides it, not a
/// meaningful "center" of opposite points.
///
/// ```
/// use skymath::{circular_mean, Angle};
///
/// let mean = circular_mean([359.0, 1.0].map(Angle::from_degrees)).unwrap();
/// assert!(circular_mean(std::iter::empty()).is_none());
/// assert!(mean.normalized_pm_180().degrees().abs() < 1e-9);
/// ```
pub fn circular_mean(angles: impl IntoIterator<Item = Angle>) -> Option<Angle> {
    let mut acc = CircularMean::new();
    for a in angles {
        acc.push(a);
    }
    acc.mean()
}

/// Running circular-mean accumulator: [`circular_mean`] for angles that
/// arrive incrementally (e.g. while clustering), without buffering them.
///
/// Maintains running unit-vector sums, so the result is exact for the pushed
/// set — no incremental-update approximation — and (up to float rounding
/// order) independent of push order. The same antipodal caution as
/// [`circular_mean`] applies.
///
/// ```
/// use skymath::{Angle, CircularMean};
///
/// let mut acc = CircularMean::new();
/// assert!(acc.mean().is_none());
/// acc.push(Angle::from_degrees(359.0));
/// acc.push(Angle::from_degrees(1.0));
/// assert!(acc.mean().unwrap().normalized_pm_180().degrees().abs() < 1e-9);
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct CircularMean {
    sin_sum: f64,
    cos_sum: f64,
    count: u64,
}

impl CircularMean {
    /// An empty accumulator ([`mean`](Self::mean) is `None`).
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }
    /// Fold one angle into the running mean.
    pub fn push(&mut self, angle: Angle) {
        self.sin_sum += angle.radians.sin();
        self.cos_sum += angle.radians.cos();
        self.count += 1;
    }
    /// Circular mean of everything pushed so far, normalized to
    /// `[0°, 360°)`; `None` when nothing has been pushed.
    #[must_use]
    pub fn mean(&self) -> Option<Angle> {
        (self.count > 0)
            .then(|| Angle::from_radians(self.sin_sum.atan2(self.cos_sum)).normalized_0_360())
    }
}

/// Shortest arc between two angles on the full circle, in `[0°, 180°]`.
///
/// Symmetric and wraparound-safe: 350° and 10° are 20° apart, not 340°.
/// Treats the full turn as the period — two directions 180° apart are
/// maximally distant. For axially symmetric quantities where θ and θ+180°
/// are equivalent (e.g. a rotator ignoring image parity), halve the period
/// yourself: `circular_distance(a * 2.0, b * 2.0) / 2.0`.
///
/// ```
/// use skymath::{circular_distance, Angle};
///
/// let d = circular_distance(Angle::from_degrees(350.0), Angle::from_degrees(10.0));
/// assert!((d.degrees() - 20.0).abs() < 1e-9);
/// ```
#[must_use]
pub fn circular_distance(a: Angle, b: Angle) -> Angle {
    let diff = (a.radians - b.radians).rem_euclid(2.0 * PI);
    Angle::from_radians(diff.min(2.0 * PI - diff))
}

// ── Parse modes and styles ─────────────────────────────────────────────────────

/// How permissive sexagesimal parsing is.
///
/// In **every** mode an unparseable token is [`Error::ParseCoord`]; lenient
/// means flexible *format*, never acceptance of corrupt input.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ParseMode {
    /// All three fields present (`HH:MM:SS` / `±DD MM SS`), colon or space
    /// separated, minutes/seconds in `[0, 60)`.
    Strict,
    /// One to three fields; missing minutes/seconds default to zero; separators
    /// may be spaces, colons, or tabs; the sign comes from the leading token.
    /// Bare decimals are accepted.
    #[default]
    Lenient,
}

/// Separator used in formatted sexagesimal output.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Separator {
    /// `HH:MM:SS.sss` (display convention).
    #[default]
    Colons,
    /// `HH MM SS.sss` (FITS keyword convention).
    Spaces,
    /// Unicode astronomy glyphs: `HHhMMmSSs` for right ascension,
    /// `±DD°MM′SS″` for declination. Negative declination uses U+2212 (minus
    /// sign), not the ASCII hyphen `-`.
    ///
    /// ```
    /// use skymath::{format_dec, format_ra, parse_dec, parse_ra, ParseMode, Separator, SexaStyle};
    ///
    /// let style = SexaStyle { separator: Separator::Unicode, seconds_places: 0 };
    /// let ra = parse_ra("00:42:44.3", ParseMode::Strict).unwrap();
    /// assert_eq!(format_ra(ra, style), "00h42m44s");
    /// let dec = parse_dec("-05:30:00", ParseMode::Strict).unwrap();
    /// assert_eq!(format_dec(dec, style), "\u{2212}05\u{b0}30\u{2032}00\u{2033}");
    /// ```
    Unicode,
}

/// Formatting control for sexagesimal output.
///
/// ```
/// use skymath::{Separator, SexaStyle};
///
/// let fits_style = SexaStyle { separator: Separator::Spaces, seconds_places: 0 };
/// assert_eq!(fits_style.seconds_places, 0);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SexaStyle {
    /// Field separator.
    pub separator: Separator,
    /// Fractional digits on the seconds field.
    pub seconds_places: u8,
}

impl Default for SexaStyle {
    /// Colon-separated with 2 fractional seconds digits.
    ///
    /// ```
    /// use skymath::{Separator, SexaStyle};
    ///
    /// let style = SexaStyle::default();
    /// assert_eq!(style.separator, Separator::Colons);
    /// assert_eq!(style.seconds_places, 2);
    /// ```
    fn default() -> Self {
        Self {
            separator: Separator::Colons,
            seconds_places: 2,
        }
    }
}

// ── Parsing ────────────────────────────────────────────────────────────────────

/// Parse a right ascension string into an [`Angle`].
///
/// Sexagesimal input is in **hours** (`"06:00:00"` → 90°); a bare decimal is
/// in **degrees** (matching FITS `RA`/`OBJCTRA` conventions). The result is
/// not domain-checked — wrap/validate at the type that embeds it.
///
/// # Errors
/// [`Error::ParseCoord`] on malformed input (any mode).
///
/// ```
/// use skymath::{parse_ra, ParseMode};
///
/// let ra = parse_ra("00:42:44.3", ParseMode::Strict)?;
/// assert!((ra.hours() - 0.7123).abs() < 1e-3);
/// # Ok::<(), skymath::Error>(())
/// ```
pub fn parse_ra(s: &str, mode: ParseMode) -> Result<Angle> {
    if looks_sexagesimal(s) {
        Ok(Angle::from_hours(parse_sexagesimal(s, mode)?))
    } else {
        decimal_fallback(s, mode).map(Angle::from_degrees)
    }
}

/// Parse a declination (or latitude/longitude) string into an [`Angle`].
///
/// Both sexagesimal and bare-decimal input are in **degrees**. The sign is
/// taken from the leading field and preserved even for `-00 30 00`.
///
/// # Errors
/// [`Error::ParseCoord`] on malformed input (any mode).
///
/// ```
/// use skymath::{parse_dec, ParseMode};
///
/// let dec = parse_dec("+41:16:09", ParseMode::Strict)?;
/// assert!((dec.degrees() - 41.269_17).abs() < 1e-3);
/// # Ok::<(), skymath::Error>(())
/// ```
pub fn parse_dec(s: &str, mode: ParseMode) -> Result<Angle> {
    if looks_sexagesimal(s) {
        Ok(Angle::from_degrees(parse_sexagesimal(s, mode)?))
    } else {
        decimal_fallback(s, mode).map(Angle::from_degrees)
    }
}

fn decimal_fallback(s: &str, mode: ParseMode) -> Result<f64> {
    match mode {
        // A single bare field is only acceptable in lenient mode.
        ParseMode::Strict => Err(Error::ParseCoord(format!(
            "strict mode requires HH:MM:SS / DD:MM:SS fields: {s:?}"
        ))),
        ParseMode::Lenient => parse_decimal(s),
    }
}

fn looks_sexagesimal(raw: &str) -> bool {
    let t = raw.trim();
    t.contains(':') || t.split_whitespace().count() > 1
}

fn parse_decimal(raw: &str) -> Result<f64> {
    raw.trim()
        .parse::<f64>()
        .ok()
        .filter(|v| v.is_finite())
        .ok_or_else(|| Error::ParseCoord(format!("not a finite number: {raw:?}")))
}

/// Parse `±A:B:C(.c)` / `±A B C` into a signed decimal value (degrees for Dec,
/// hours for RA). Minutes/seconds must be in `[0, 60)`; the sign comes from the
/// leading field; every token must parse — corrupt tokens are never dropped.
fn parse_sexagesimal(raw: &str, mode: ParseMode) -> Result<f64> {
    let trimmed = raw.trim().trim_matches('\'').trim();
    if trimmed.is_empty() {
        return Err(Error::ParseCoord("empty coordinate".to_owned()));
    }
    let normalized = trimmed.replace([':', '\t'], " ");
    let mut parts = normalized.split_whitespace();
    let lead = parts
        .next()
        .ok_or_else(|| Error::ParseCoord(format!("no leading field: {raw:?}")))?;
    let negative = lead.starts_with('-');
    let lead_val: f64 = lead
        .parse()
        .ok()
        .filter(|v: &f64| v.is_finite())
        .ok_or_else(|| Error::ParseCoord(format!("bad degrees/hours field: {raw:?}")))?;
    let min = next_field(&mut parts, raw, "minutes")?;
    let sec = next_field(&mut parts, raw, "seconds")?;
    if parts.next().is_some() {
        return Err(Error::ParseCoord(format!("too many fields: {raw:?}")));
    }
    let field_count = 1 + usize::from(min.is_some()) + usize::from(sec.is_some());
    if mode == ParseMode::Strict && field_count != 3 {
        return Err(Error::ParseCoord(format!(
            "strict mode requires 3 fields, got {field_count}: {raw:?}"
        )));
    }
    let (min, sec) = (min.unwrap_or(0.0), sec.unwrap_or(0.0));
    if min < 0.0 || sec < 0.0 || min >= 60.0 || sec >= 60.0 {
        return Err(Error::ParseCoord(format!(
            "minutes/seconds out of range: {raw:?}"
        )));
    }
    let magnitude = lead_val.abs() + min / 60.0 + sec / 3600.0;
    Ok(if negative { -magnitude } else { magnitude })
}

fn next_field<'a>(
    parts: &mut impl Iterator<Item = &'a str>,
    raw: &str,
    what: &str,
) -> Result<Option<f64>> {
    match parts.next() {
        None => Ok(None),
        Some(s) => s
            .parse::<f64>()
            .ok()
            .filter(|v| v.is_finite())
            .map(Some)
            .ok_or_else(|| Error::ParseCoord(format!("bad {what} field: {raw:?}"))),
    }
}

// ── Formatting ─────────────────────────────────────────────────────────────────

/// Format an angle as sexagesimal right ascension (hours), wrapped to
/// `[0h, 24h)`, e.g. `06:30:00.00`.
///
/// ```
/// use skymath::{format_ra, parse_ra, ParseMode, SexaStyle};
///
/// let ra = parse_ra("00:42:44.3", ParseMode::Strict)?;
/// assert_eq!(format_ra(ra, SexaStyle::default()), "00:42:44.30");
/// # Ok::<(), skymath::Error>(())
/// ```
#[must_use]
pub fn format_ra(a: Angle, style: SexaStyle) -> String {
    let hours = a.normalized_0_360().degrees() / 15.0;
    let s = format_sexagesimal(hours, false, style, true);
    // Rounding carry can reach 24:00:00 — wrap to 00.
    if s.starts_with("24:") || s.starts_with("24 ") || s.starts_with("24h") {
        format_sexagesimal(0.0, false, style, true)
    } else {
        s
    }
}

/// Format an angle as signed sexagesimal degrees (declination/latitude), e.g.
/// `+41:16:09.00`. The sign is always present; `-0°` keeps its minus sign.
///
/// ```
/// use skymath::{format_dec, parse_dec, ParseMode, SexaStyle};
///
/// let dec = parse_dec("+41:16:09", ParseMode::Strict)?;
/// assert_eq!(format_dec(dec, SexaStyle::default()), "+41:16:09.00");
/// # Ok::<(), skymath::Error>(())
/// ```
#[must_use]
pub fn format_dec(a: Angle, style: SexaStyle) -> String {
    format_sexagesimal(a.degrees(), true, style, false)
}

/// Format a signed decimal value as sexagesimal, with rounding performed at the
/// seconds precision *before* field splitting so `59.9996″` carries into the
/// minute (never emitting a `60` field). `is_ra` selects the RA (`h`/`m`/`s`)
/// vs Dec (`°`/`′`/`″`) glyph set when `style.separator` is
/// [`Separator::Unicode`].
fn format_sexagesimal(value: f64, signed: bool, style: SexaStyle, is_ra: bool) -> String {
    let neg = value.is_sign_negative() && value != 0.0;
    let decimals = usize::from(style.seconds_places);
    let sec_scale = 3600.0 * 10f64.powi(i32::from(style.seconds_places));
    let rounded = (value.abs() * sec_scale).round() / sec_scale;
    let (a, b, c) = decompose_magnitude(rounded);
    let width = if decimals > 0 { decimals + 3 } else { 2 };
    match style.separator {
        Separator::Colons | Separator::Spaces => {
            let sign = if neg {
                "-"
            } else if signed {
                "+"
            } else {
                ""
            };
            let sep = match style.separator {
                Separator::Colons => ':',
                Separator::Spaces => ' ',
                Separator::Unicode => unreachable!(),
            };
            format!("{sign}{a:02}{sep}{b:02}{sep}{c:0width$.decimals$}")
        }
        Separator::Unicode => {
            let sign = if neg {
                "\u{2212}"
            } else if signed {
                "+"
            } else {
                ""
            };
            let (u1, u2, u3) = if is_ra {
                ("h", "m", "s")
            } else {
                ("\u{b0}", "\u{2032}", "\u{2033}")
            };
            format!("{sign}{a:02}{u1}{b:02}{u2}{c:0width$.decimals$}{u3}")
        }
    }
}

/// Split a non-negative decimal value into `(whole, minutes, seconds)`
/// sexagesimal components. Shared by `format_sexagesimal` (after
/// display-precision rounding) and the raw component accessors on
/// `Equatorial` (no rounding there — callers own their own display
/// precision).
pub(crate) fn decompose_magnitude(v: f64) -> (u32, u32, f64) {
    let a = v.trunc();
    let rem_min = (v - a) * 60.0;
    let b = rem_min.trunc();
    let c = (rem_min - b) * 60.0;
    (a as u32, b as u32, c)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn approx(a: f64, b: f64, eps: f64) -> bool {
        (a - b).abs() < eps
    }

    #[test]
    fn angle_conversions() {
        let a = Angle::from_degrees(90.0);
        assert!(approx(a.radians(), PI / 2.0, 1e-12));
        assert!(approx(a.arcminutes(), 5400.0, 1e-6));
        assert!(approx(a.arcseconds(), 324_000.0, 1e-3));
        assert!(approx(Angle::from_hours(1.0).degrees(), 15.0, 1e-12));
        assert!(approx(Angle::from_arcminutes(60.0).degrees(), 1.0, 1e-12));
        assert!(approx(Angle::from_arcseconds(3600.0).degrees(), 1.0, 1e-12));
    }

    #[test]
    fn angle_normalization() {
        assert!(approx(
            Angle::from_degrees(370.0).normalized_0_360().degrees(),
            10.0,
            1e-9
        ));
        assert!(approx(
            Angle::from_degrees(-10.0).normalized_0_360().degrees(),
            350.0,
            1e-9
        ));
        assert!(approx(
            Angle::from_degrees(350.0).normalized_pm_180().degrees(),
            -10.0,
            1e-9
        ));
        assert!(approx(
            Angle::from_hours(25.0).normalized_hours().hours(),
            1.0,
            1e-9
        ));
    }

    #[test]
    fn angle_ops() {
        let s = Angle::from_degrees(10.0) + Angle::from_degrees(5.0);
        assert!(approx(s.degrees(), 15.0, 1e-12));
        assert!(approx(
            (Angle::from_degrees(10.0) * 3.0).degrees(),
            30.0,
            1e-12
        ));
        assert!(approx(
            (Angle::from_degrees(10.0) / 2.0).degrees(),
            5.0,
            1e-12
        ));
        assert!(approx((-Angle::from_degrees(10.0)).degrees(), -10.0, 1e-12));
    }

    #[test]
    fn lenient_accepts_partial_fields() {
        assert!(approx(
            parse_ra("10 30", ParseMode::Lenient).unwrap().hours(),
            10.5,
            1e-9
        ));
        assert!(approx(
            parse_dec("45", ParseMode::Lenient).unwrap().degrees(),
            45.0,
            1e-9
        ));
    }

    #[test]
    fn garbage_errors_in_every_mode() {
        for mode in [ParseMode::Strict, ParseMode::Lenient] {
            assert!(matches!(
                parse_ra("10 xx 30", mode).unwrap_err(),
                Error::ParseCoord(_)
            ));
            assert!(matches!(
                parse_dec("", mode).unwrap_err(),
                Error::ParseCoord(_)
            ));
            assert!(matches!(
                parse_dec("12 70 00", mode).unwrap_err(),
                Error::ParseCoord(_)
            ));
        }
    }

    #[test]
    fn strict_requires_three_fields() {
        assert!(parse_ra("10 30", ParseMode::Strict).is_err());
        assert!(parse_ra("10.5", ParseMode::Strict).is_err());
        assert!(parse_ra("10:30:00", ParseMode::Strict).is_ok());
    }

    #[test]
    fn sign_survives_zero_degrees() {
        let d = parse_dec("-00 30 00", ParseMode::Lenient).unwrap();
        assert!(approx(d.degrees(), -0.5, 1e-9));
        let formatted = format_dec(d, SexaStyle::default());
        assert!(formatted.starts_with("-00:30"), "{formatted}");
    }

    #[test]
    fn format_carries_rounding() {
        // 59.9996″ at 2 decimals must roll into the next minute, never ":60".
        let a = Angle::from_degrees(10.0 + 59.0 / 60.0 + 59.9996 / 3600.0);
        let s = format_dec(a, SexaStyle::default());
        assert_eq!(s, "+11:00:00.00");
        // RA carry across 24h wraps to zero.
        let ra = Angle::from_hours(23.0 + 59.0 / 60.0 + 59.9996 / 3600.0);
        let r = format_ra(ra, SexaStyle::default());
        assert_eq!(r, "00:00:00.00");
    }

    #[test]
    fn format_unicode_glyphs() {
        let style = SexaStyle {
            separator: Separator::Unicode,
            seconds_places: 0,
        };
        let ra = Angle::from_hours(0.712_306);
        assert_eq!(format_ra(ra, style), "00h42m44s");
        let dec = Angle::from_degrees(41.269_167);
        assert_eq!(format_dec(dec, style), "+41\u{b0}16\u{2032}09\u{2033}");
        let neg_dec = Angle::from_degrees(-5.5);
        assert_eq!(
            format_dec(neg_dec, style),
            "\u{2212}05\u{b0}30\u{2032}00\u{2033}"
        );
        assert!(
            !format_dec(neg_dec, style).starts_with('-'),
            "must use U+2212, not ASCII hyphen"
        );
    }

    #[test]
    fn format_unicode_ra_carries_across_24h() {
        let style = SexaStyle {
            separator: Separator::Unicode,
            seconds_places: 2,
        };
        let ra = Angle::from_hours(23.0 + 59.0 / 60.0 + 59.9996 / 3600.0);
        assert_eq!(format_ra(ra, style), "00h00m00.00s");
    }

    #[test]
    fn format_styles() {
        let a = Angle::from_hours(6.5);
        assert_eq!(
            format_ra(
                a,
                SexaStyle {
                    separator: Separator::Spaces,
                    seconds_places: 0
                }
            ),
            "06 30 00"
        );
        assert_eq!(format_ra(a, SexaStyle::default()), "06:30:00.00");
    }

    #[test]
    fn circular_mean_handles_wraparound() {
        let mean = circular_mean([359.0, 1.0].map(Angle::from_degrees)).unwrap();
        // 0° and 360° are the same direction; compare on the signed branch.
        assert!(mean.normalized_pm_180().degrees().abs() < 1e-9);
        let mean = circular_mean([10.0, 20.0, 30.0].map(Angle::from_degrees)).unwrap();
        assert!((mean.degrees() - 20.0).abs() < 1e-9);
        // Result is normalized even for out-of-range inputs.
        let mean = circular_mean([Angle::from_degrees(-90.0)]).unwrap();
        assert!((mean.degrees() - 270.0).abs() < 1e-9);
    }

    #[test]
    fn circular_mean_empty_is_none() {
        assert!(circular_mean(std::iter::empty()).is_none());
    }

    #[test]
    fn circular_mean_antipodal_is_degenerate_but_finite() {
        // Cancelling resultant: direction is float residue, but never NaN
        // and always normalized.
        let mean = circular_mean([0.0, 180.0].map(Angle::from_degrees)).unwrap();
        let deg = mean.degrees();
        assert!(deg.is_finite());
        assert!((0.0..360.0).contains(&deg));
    }

    #[test]
    fn circular_mean_accumulator_matches_batch() {
        let degs = [340.0, 355.0, 5.0, 20.0, 180.0];
        let batch = circular_mean(degs.map(Angle::from_degrees)).unwrap();
        let mut acc = CircularMean::new();
        for d in degs {
            acc.push(Angle::from_degrees(d));
        }
        assert!((acc.mean().unwrap().degrees() - batch.degrees()).abs() < 1e-12);

        // Push order only reorders float additions; results agree tightly.
        let mut rev = CircularMean::new();
        for d in degs.iter().rev() {
            rev.push(Angle::from_degrees(*d));
        }
        assert!((rev.mean().unwrap().degrees() - batch.degrees()).abs() < 1e-9);
    }

    #[test]
    fn circular_mean_accumulator_empty_is_none() {
        assert!(CircularMean::new().mean().is_none());
        assert!(CircularMean::default().mean().is_none());
    }

    #[test]
    fn circular_distance_shortest_arc() {
        let d = |a: f64, b: f64| {
            circular_distance(Angle::from_degrees(a), Angle::from_degrees(b)).degrees()
        };
        assert!((d(350.0, 10.0) - 20.0).abs() < 1e-9); // across the seam
        assert!((d(10.0, 350.0) - 20.0).abs() < 1e-9); // symmetric
        assert!((d(0.0, 180.0) - 180.0).abs() < 1e-9); // antipodal maximum
        assert!(d(42.0, 42.0).abs() < 1e-9);
        assert!((d(-10.0, 10.0) - 20.0).abs() < 1e-9); // unnormalized inputs
    }
}