qtty-core 0.7.1

Core types for zero-cost strongly-typed physical quantities.
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (C) 2026 Vallés Puig, Ramon

//! Angular quantities and utilities.
//!
//! This module defines the **`Angular` dimension**, a blanket [`AngularUnit`] trait that extends
//! [`Unit`] for all angular units, common angular units (degrees, radians, arcseconds, etc.), and a set of
//! convenience methods on [`Quantity<U>`] where `U: AngularUnit`.
//!
//! # Design overview
//!
//! * **Canonical unit:** Degrees are taken as the canonical *scaling* unit for this dimension. That is,
//!   `Degree::RATIO == 1.0`, and all other angular units express how many *degrees* correspond to one of that unit.
//!   For example, `Radian::RATIO == 180.0 / PI` because 1 radian = 180/π degrees.
//! * **Associated constants:** The `AngularUnit` trait exposes precomputed constants (`FULL_TURN`, `HALF_TURN`,
//!   `QUARTER_TURN`) expressed *in the receiving unit* for ergonomic range‑wrapping. These are derived from `τ`
//!   radians and then converted to the target unit to avoid cumulative error from chained conversions.
//! * **Trigonometry:** `sin`, `cos`, `tan`, and `sin_cos` methods are provided on angular quantities; they convert to
//!   radians internally and then call the corresponding `f64` intrinsic.
//! * **Wrapping helpers:** Utility methods to wrap any angle into common ranges — `[0, 360)` (or unit equivalent),
//!   `(-180, 180]`, and the latitude‑style quarter fold `[-90, 90]`.
//!
//! ## Edge cases
//!
//! Wrapping and trig operations follow IEEE‑754 semantics from `f64`: if the underlying numeric is `NaN` or
//! `±∞`, results will generally be `NaN`.
//!
//! ## Unit symbols
//!
//! Unit `SYMBOL`s are used for display (e.g., `format!("{}", angle)`) and follow conventional unit symbols.
//! Unicode symbols are used where standard notation requires them (e.g., `°`, `′`, `″`, `μas`).
//!
//! ## Examples
//!
//! Convert between degrees and radians and evaluate a trig function:
//!
//! ```rust
//! use qtty_core::angular::{Degrees, Radians};
//!
//! let angle: Degrees = Degrees::new(90.0);
//! let r: Radians = angle.to();
//! assert!((r.value() - core::f64::consts::FRAC_PI_2).abs() < 1e-12);
//! assert!((angle.sin() - 1.0).abs() < 1e-12);
//! ```
//!
//! Wrap into the conventional signed range:
//!
//! ```rust
//! use qtty_core::angular::Degrees;
//! let a = Degrees::new(370.0).wrap_signed();
//! assert_eq!(a.value(), 10.0);
//! ```

use crate::scalar::Transcendental;
use crate::{Quantity, Unit};
use core::f64::consts::TAU;
use qtty_derive::Unit;

#[inline]
fn rem_euclid(x: f64, modulus: f64) -> f64 {
    #[cfg(feature = "std")]
    {
        x.rem_euclid(modulus)
    }
    #[cfg(not(feature = "std"))]
    {
        let r = crate::libm::fmod(x, modulus);
        if r < 0.0 {
            r + modulus
        } else {
            r
        }
    }
}

/// Re-export from the dimension module.
pub use crate::dimension::Angular;

/// Blanket extension trait for any [`Unit`] whose dimension is [`Angular`].
///
/// These associated constants provide the size of key turn fractions *expressed in the implementing unit*.
/// They are computed via a compile-time conversion from `TAU` radians (i.e., a full revolution) and then scaled.
/// This keeps all fractions derived from the same base value.
///
pub trait AngularUnit: Unit<Dim = Angular> {
    /// One full revolution (τ radians / 360°) expressed in this unit.
    const FULL_TURN: f64;
    /// Half a revolution (π radians / 180°) expressed in this unit.
    const HALF_TURN: f64;
    /// A quarter revolution (π/2 radians / 90°) expressed in this unit.
    const QUARTER_TURN: f64;
}
impl<T: Unit<Dim = Angular>> AngularUnit for T {
    /// One full revolution (360°) expressed in T unit.
    const FULL_TURN: f64 = Radians::new(TAU).to_const::<T>().value();
    /// Half a revolution (180°) expressed in T unit.
    const HALF_TURN: f64 = Radians::new(TAU).to_const::<T>().value() * 0.5;
    /// Quarter revolution (90°) expressed in T unit.
    const QUARTER_TURN: f64 = Radians::new(TAU).to_const::<T>().value() * 0.25;
}

#[cfg(feature = "astro")]
mod astro;
#[cfg(feature = "astro")]
pub use astro::*;
#[cfg(feature = "navigation")]
mod navigation;
#[cfg(feature = "navigation")]
pub use navigation::*;

impl<U: AngularUnit + Copy> Quantity<U> {
    /// Constant representing τ radians (2π rad == 360°).
    ///
    /// For angular quantities, `TAU` and [`Self::FULL_TURN`] are identical by construction.
    pub const TAU: Quantity<U> = Quantity::<U>::new(U::FULL_TURN);
    /// One full revolution (360°) expressed as `Quantity<U>`.
    pub const FULL_TURN: Quantity<U> = Quantity::<U>::new(U::FULL_TURN);
    /// Half a revolution (180°) expressed as `Quantity<U>`.
    pub const HALF_TURN: Quantity<U> = Quantity::<U>::new(U::HALF_TURN);
    /// Quarter revolution (90°) expressed as `Quantity<U>`.
    pub const QUARTER_TURN: Quantity<U> = Quantity::<U>::new(U::QUARTER_TURN);

    /// Sign of the *raw numeric* in this unit (same semantics as `f64::signum()`).
    #[inline]
    pub const fn signum_const(self) -> f64 {
        self.value().signum()
    }

    /// Normalize into the canonical positive range `[0, FULL_TURN)`.
    ///
    /// Shorthand for [`Self::wrap_pos`].
    #[inline]
    pub fn normalize(self) -> Self {
        self.wrap_pos()
    }

    /// Wrap into the positive range `[0, FULL_TURN)` using Euclidean remainder.
    ///
    /// IEEE‑754 note: `NaN`/`±∞` inputs generally produce `NaN`.
    #[inline]
    pub fn wrap_pos(self) -> Self {
        Self::new(rem_euclid(self.value(), U::FULL_TURN))
    }

    /// Wrap into the signed range `(-HALF_TURN, HALF_TURN]`.
    ///
    /// *Upper bound is inclusive*; lower bound is exclusive. Useful for computing minimal signed angular differences.
    ///
    /// IEEE‑754 note: `NaN`/`±∞` inputs generally produce `NaN`.
    #[inline]
    pub fn wrap_signed(self) -> Self {
        let full = U::FULL_TURN;
        let half = 0.5 * full;
        let x = self.value();
        let y = rem_euclid(x + half, full) - half;
        let norm = if y <= -half { y + full } else { y };
        Self::new(norm)
    }

    /// Wrap into the alternate signed range `[-HALF_TURN, HALF_TURN)`.
    ///
    /// Lower bound inclusive; upper bound exclusive. Equivalent to `self.wrap_signed()` with the boundary flipped.
    ///
    /// IEEE‑754 note: `NaN`/`±∞` inputs generally produce `NaN`.
    #[inline]
    pub fn wrap_signed_lo(self) -> Self {
        let mut y = self.wrap_signed().value(); // now in (-half, half]
        let half = 0.5 * U::FULL_TURN;
        if y >= half {
            // move +half to -half
            y -= U::FULL_TURN;
        }
        Self::new(y)
    }

    /// "Latitude fold": map into `[-QUARTER_TURN, +QUARTER_TURN]`.
    ///
    /// Useful for folding polar coordinates (e.g., converting declination‑like angles to a limited range).
    ///
    /// IEEE‑754 note: `NaN`/`±∞` inputs generally produce `NaN`.
    #[inline]
    pub fn wrap_quarter_fold(self) -> Self {
        let full = U::FULL_TURN;
        let half = 0.5 * full;
        let quarter = 0.25 * full;
        let y = rem_euclid(self.value() + quarter, full);
        // quarter - |y - half| yields [-quarter, quarter]
        Self::new(quarter - (y - half).abs())
    }

    /// Signed smallest angular separation in `(-HALF_TURN, HALF_TURN]`.
    #[inline]
    pub fn signed_separation(self, other: Self) -> Self {
        (self - other).wrap_signed()
    }

    /// Absolute smallest angular separation (magnitude only).
    #[inline]
    pub fn abs_separation(self, other: Self) -> Self {
        let sep = self.signed_separation(other);
        Self::new(sep.value().abs())
    }

    /// Normalize into the signed half-open interval `(-HALF_TURN, +HALF_TURN]`.
    ///
    /// For radians this is `(-π, π]`; for degrees this is `(-180°, 180°]`. This is the conventional
    /// "shortest signed difference" range. It is an alias for [`Self::wrap_signed`] provided to
    /// match the naming used by downstream callers (e.g. NSB).
    ///
    /// IEEE‑754 note: `NaN`/`±∞` inputs generally produce `NaN`.
    ///
    /// # Examples
    ///
    /// In‑range value:
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::FRAC_PI_2;
    /// assert!((Radians::new(FRAC_PI_2).wrap_to_signed_pi().value() - FRAC_PI_2).abs() < 1e-12);
    /// ```
    ///
    /// Slightly out of range (just past π):
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::PI;
    /// let v = Radians::new(PI + 0.1).wrap_to_signed_pi().value();
    /// assert!((v - (-PI + 0.1)).abs() < 1e-12);
    /// ```
    ///
    /// Far out of range (7π wraps to π):
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::PI;
    /// let v = Radians::new(7.0 * PI).wrap_to_signed_pi().value();
    /// assert!((v - PI).abs() < 1e-12);
    /// ```
    ///
    /// Boundary at +π is inclusive:
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::PI;
    /// assert!((Radians::new(PI).wrap_to_signed_pi().value() - PI).abs() < 1e-12);
    /// ```
    ///
    /// Negative values:
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::PI;
    /// let v = Radians::new(-3.0 * PI / 2.0).wrap_to_signed_pi().value();
    /// assert!((v - PI / 2.0).abs() < 1e-12);
    /// ```
    ///
    /// `NaN` propagates:
    /// ```
    /// use qtty_core::angular::Radians;
    /// assert!(Radians::new(f64::NAN).wrap_to_signed_pi().value().is_nan());
    /// ```
    #[inline]
    pub fn wrap_to_signed_pi(self) -> Self {
        self.wrap_signed()
    }

    /// Normalize into the unsigned range `[0, FULL_TURN)`.
    ///
    /// For radians this is `[0, 2π)`; for degrees this is `[0°, 360°)`. Alias for
    /// [`Self::wrap_pos`] provided to match the naming used by downstream callers (e.g. NSB).
    ///
    /// IEEE‑754 note: `NaN`/`±∞` inputs generally produce `NaN`.
    ///
    /// # Examples
    ///
    /// In‑range value passes through:
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::FRAC_PI_2;
    /// assert!((Radians::new(FRAC_PI_2).wrap_to_unsigned_pi().value() - FRAC_PI_2).abs() < 1e-12);
    /// ```
    ///
    /// Slightly out of range:
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::TAU;
    /// let v = Radians::new(TAU + 0.5).wrap_to_unsigned_pi().value();
    /// assert!((v - 0.5).abs() < 1e-12);
    /// ```
    ///
    /// Far out of range (7π → π):
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::PI;
    /// let v = Radians::new(7.0 * PI).wrap_to_unsigned_pi().value();
    /// assert!((v - PI).abs() < 1e-12);
    /// ```
    ///
    /// Boundary 2π folds back to 0:
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::TAU;
    /// assert!(Radians::new(TAU).wrap_to_unsigned_pi().value().abs() < 1e-12);
    /// ```
    ///
    /// Negative values are folded into the positive range:
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::{PI, TAU};
    /// let v = Radians::new(-PI / 2.0).wrap_to_unsigned_pi().value();
    /// assert!((v - (TAU - PI / 2.0)).abs() < 1e-12);
    /// ```
    ///
    /// `NaN` propagates:
    /// ```
    /// use qtty_core::angular::Radians;
    /// assert!(Radians::new(f64::NAN).wrap_to_unsigned_pi().value().is_nan());
    /// ```
    #[inline]
    pub fn wrap_to_unsigned_pi(self) -> Self {
        self.wrap_pos()
    }

    /// Fold into the closed interval `[0, HALF_TURN]`.
    ///
    /// For radians this is `[0, π]`; for degrees this is `[0°, 180°]`. This is the magnitude of
    /// the shortest signed angular distance from the origin and matches the semantics used by
    /// callers such as NSB when reducing ecliptic longitude separations.
    ///
    /// Implementation: takes the absolute value of [`Self::wrap_signed`], so the result is in
    /// `[0, HALF_TURN]` (the upper bound is reachable when the input maps to exactly `+HALF_TURN`).
    ///
    /// IEEE‑754 note: `NaN`/`±∞` inputs generally produce `NaN`.
    ///
    /// # Examples
    ///
    /// In‑range value:
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::FRAC_PI_3;
    /// assert!((Radians::new(FRAC_PI_3).fold_to_pi().value() - FRAC_PI_3).abs() < 1e-12);
    /// ```
    ///
    /// Slightly out of range (just past π folds back symmetrically):
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::PI;
    /// let v = Radians::new(PI + 0.1).fold_to_pi().value();
    /// assert!((v - (PI - 0.1)).abs() < 1e-12);
    /// ```
    ///
    /// Far out of range (7π folds to π):
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::PI;
    /// let v = Radians::new(7.0 * PI).fold_to_pi().value();
    /// assert!((v - PI).abs() < 1e-12);
    /// ```
    ///
    /// Boundary at π is reachable:
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::PI;
    /// assert!((Radians::new(PI).fold_to_pi().value() - PI).abs() < 1e-12);
    /// ```
    ///
    /// Negative values are folded:
    /// ```
    /// use qtty_core::angular::Radians;
    /// use core::f64::consts::PI;
    /// let v = Radians::new(-PI / 4.0).fold_to_pi().value();
    /// assert!((v - PI / 4.0).abs() < 1e-12);
    /// ```
    ///
    /// `NaN` propagates:
    /// ```
    /// use qtty_core::angular::Radians;
    /// assert!(Radians::new(f64::NAN).fold_to_pi().value().is_nan());
    /// ```
    #[inline]
    pub fn fold_to_pi(self) -> Self {
        Self::new(self.wrap_signed().value().abs())
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Generic trigonometric implementations for any Transcendental scalar type
// ─────────────────────────────────────────────────────────────────────────────

impl<U: AngularUnit + Copy, S: Transcendental> Quantity<U, S> {
    /// Sine of the angle.
    ///
    /// Converts the angle to radians and computes the sine.
    /// Works with any scalar type that implements [`Transcendental`] (e.g., `f32`, `f64`).
    #[inline]
    pub fn sin(self) -> S {
        let x_rad = self.to::<Radian>().value();
        x_rad.sin()
    }

    /// Cosine of the angle.
    ///
    /// Converts the angle to radians and computes the cosine.
    /// Works with any scalar type that implements [`Transcendental`] (e.g., `f32`, `f64`).
    #[inline]
    pub fn cos(self) -> S {
        let x_rad = self.to::<Radian>().value();
        x_rad.cos()
    }

    /// Tangent of the angle.
    ///
    /// Converts the angle to radians and computes the tangent.
    /// Works with any scalar type that implements [`Transcendental`] (e.g., `f32`, `f64`).
    #[inline]
    pub fn tan(self) -> S {
        let x_rad = self.to::<Radian>().value();
        x_rad.tan()
    }

    /// Simultaneously compute sine and cosine.
    ///
    /// Converts the angle to radians and computes both sine and cosine.
    /// Works with any scalar type that implements [`Transcendental`] (e.g., `f32`, `f64`).
    #[inline]
    pub fn sin_cos(self) -> (S, S) {
        let x_rad = self.to::<Radian>().value();
        x_rad.sin_cos()
    }
}

/// Degree.
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "°", dimension = Angular, ratio = 1.0)]
pub struct Degree;
/// Type alias shorthand for [`Degree`].
pub type Deg = Degree;
/// Convenience alias for a degree quantity.
pub type Degrees = Quantity<Deg>;
/// One degree.
pub const DEG: Degrees = Degrees::new(1.0);

/// Radian.
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "rad", dimension = Angular, ratio = 180.0 / core::f64::consts::PI)]
pub struct Radian;
/// Type alias shorthand for [`Radian`].
pub type Rad = Radian;
/// Convenience alias for a radian quantity.
pub type Radians = Quantity<Rad>;
/// One radian.
pub const RAD: Radians = Radians::new(1.0);

/// Milliradian (`1/1000` radian).
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "mrad", dimension = Angular, ratio = (180.0 / core::f64::consts::PI) / 1_000.0)]
pub struct Milliradian;
/// Type alias shorthand for [`Milliradian`].
pub type Mrad = Milliradian;
/// Convenience alias for a milliradian quantity.
pub type Milliradians = Quantity<Mrad>;
/// One milliradian.
pub const MRAD: Milliradians = Milliradians::new(1.0);

/// Turn (full revolution; `360` degrees).
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "tr", dimension = Angular, ratio = 360.0)]
pub struct Turn;
/// Convenience alias for a turn quantity.
pub type Turns = Quantity<Turn>;
/// One turn.
pub const TURN: Turns = Turns::new(1.0);

impl Degrees {
    /// Construct from **DMS** components (`deg`, `min`, `sec`).
    ///
    /// Sign is taken from `deg`; the magnitude of `min` and `sec` is always added.
    /// No range checking is performed. Use one of the wrapping helpers if you need a canonical range.
    ///
    /// ```rust
    /// use qtty_core::angular::Degrees;
    /// let lat = Degrees::from_dms(-33, 52, 0.0); // −33°52′00″
    /// assert!(lat.value() < 0.0);
    /// ```
    pub const fn from_dms(deg: i32, min: u32, sec: f64) -> Self {
        let sign = if deg < 0 { -1.0 } else { 1.0 };
        let d_abs = if deg < 0 { -(deg as f64) } else { deg as f64 };
        let m = min as f64 / 60.0;
        let s = sec / 3600.0;
        let total = sign * (d_abs + m + s);
        Self::new(total)
    }

    /// Construct from explicit sign and magnitude components.
    ///
    /// `sign < 0` produces a negative result; any other value (including `0`)
    /// produces a positive result.
    pub const fn from_dms_sign(sign: i8, deg: u32, min: u32, sec: f64) -> Self {
        let s = if sign < 0 { -1.0 } else { 1.0 };
        let total = (deg as f64) + (min as f64) / 60.0 + (sec / 3600.0);
        Self::new(s * total)
    }
}

/// Canonical list of always-available (base) angular units.
///
/// Exported (`#[doc(hidden)]`) for use in `qtty`'s scalar alias generation and
/// compile-time consistency checks.  Feature-gated units (astro, navigation)
/// are in their sub-modules and registered via `register_builtin_units_extend!`.
#[macro_export]
#[doc(hidden)]
macro_rules! angular_units {
    ($cb:path) => {
        $cb!(Degree, Radian, Milliradian, Turn);
    };
}

// Generate bidirectional From impls between base angular units.
angular_units!(crate::impl_unit_from_conversions);

// ─────────────────────────────────────────────────────────────────────────────
// Cross-unit ops: default units
// ─────────────────────────────────────────────────────────────────────────────
#[cfg(feature = "cross-unit-ops")]
angular_units!(crate::impl_unit_cross_unit_ops);

// ── Cross-feature: astro × navigation ────────────────────────────────────────
#[cfg(all(feature = "astro", feature = "navigation"))]
crate::__impl_from_each_extra_to_bases!(
    {Arcminute, Arcsecond, MilliArcsecond, MicroArcsecond, HourAngle}
    Gradian
);
#[cfg(all(feature = "astro", feature = "navigation", feature = "cross-unit-ops"))]
crate::__impl_cross_ops_each_extra_to_bases!(
    {Arcminute, Arcsecond, MilliArcsecond, MicroArcsecond, HourAngle}
    Gradian
);

// Compile-time check: every base angular unit is registered as BuiltinUnit.
#[cfg(test)]
angular_units!(crate::assert_units_are_builtin);

#[cfg(all(test, feature = "std"))]
mod tests {
    use super::*;
    use approx::{assert_abs_diff_eq, assert_relative_eq};
    use core::f64::consts::{PI, TAU};
    use proptest::prelude::*;

    // ─────────────────────────────────────────────────────────────────────────────
    // Angular unit constants
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn test_full_turn() {
        assert_abs_diff_eq!(Radian::FULL_TURN, TAU, epsilon = 1e-12);
        assert_eq!(Degree::FULL_TURN, 360.0);
        #[cfg(feature = "astro")]
        assert_eq!(Arcsecond::FULL_TURN, 1_296_000.0);
    }

    #[test]
    fn test_half_turn() {
        assert_abs_diff_eq!(Radian::HALF_TURN, PI, epsilon = 1e-12);
        assert_eq!(Degree::HALF_TURN, 180.0);
        #[cfg(feature = "astro")]
        assert_eq!(Arcsecond::HALF_TURN, 648_000.0);
    }

    #[test]
    fn test_quarter_turn() {
        assert_abs_diff_eq!(Radian::QUARTER_TURN, PI / 2.0, epsilon = 1e-12);
        assert_eq!(Degree::QUARTER_TURN, 90.0);
        #[cfg(feature = "astro")]
        assert_eq!(Arcsecond::QUARTER_TURN, 324_000.0);
    }

    #[test]
    fn test_quantity_constants() {
        assert_eq!(Degrees::FULL_TURN.value(), 360.0);
        assert_eq!(Degrees::HALF_TURN.value(), 180.0);
        assert_eq!(Degrees::QUARTER_TURN.value(), 90.0);
        assert_eq!(Degrees::TAU.value(), 360.0);
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // Conversions
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn conversion_degrees_to_radians() {
        let deg = Degrees::new(180.0);
        let rad = deg.to::<Radian>();
        assert_abs_diff_eq!(rad.value(), PI, epsilon = 1e-12);
    }

    #[test]
    fn conversion_radians_to_degrees() {
        let rad = Radians::new(PI);
        let deg = rad.to::<Degree>();
        assert_abs_diff_eq!(deg.value(), 180.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn conversion_degrees_to_arcseconds() {
        let deg = Degrees::new(1.0);
        let arcs = deg.to::<Arcsecond>();
        assert_abs_diff_eq!(arcs.value(), 3600.0, epsilon = 1e-9);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn conversion_arcseconds_to_degrees() {
        let arcs = Arcseconds::new(3600.0);
        let deg = arcs.to::<Degree>();
        assert_abs_diff_eq!(deg.value(), 1.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn conversion_degrees_to_milliarcseconds() {
        let deg = Degrees::new(1.0);
        let mas = deg.to::<MilliArcsecond>();
        assert_abs_diff_eq!(mas.value(), 3_600_000.0, epsilon = 1e-6);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn conversion_hour_angles_to_degrees() {
        let ha = HourAngles::new(1.0);
        let deg = ha.to::<Degree>();
        assert_abs_diff_eq!(deg.value(), 15.0, epsilon = 1e-12);
    }

    #[test]
    fn conversion_roundtrip() {
        let original = Degrees::new(123.456);
        let rad = original.to::<Radian>();
        let back = rad.to::<Degree>();
        assert_abs_diff_eq!(back.value(), original.value(), epsilon = 1e-12);
    }

    #[test]
    fn from_impl_degrees_radians() {
        let deg = Degrees::new(90.0);
        let rad: Radians = deg.into();
        assert_abs_diff_eq!(rad.value(), PI / 2.0, epsilon = 1e-12);

        let rad2 = Radians::new(PI);
        let deg2: Degrees = rad2.into();
        assert_abs_diff_eq!(deg2.value(), 180.0, epsilon = 1e-12);
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // Trig functions
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn test_trig() {
        let a = Degrees::new(90.0);
        assert!((a.sin() - 1.0).abs() < 1e-12);
        assert!(a.cos().abs() < 1e-12);
    }

    #[test]
    fn trig_sin_known_values() {
        assert_abs_diff_eq!(Degrees::new(0.0).sin(), 0.0, epsilon = 1e-12);
        assert_abs_diff_eq!(Degrees::new(30.0).sin(), 0.5, epsilon = 1e-12);
        assert_abs_diff_eq!(Degrees::new(90.0).sin(), 1.0, epsilon = 1e-12);
        assert_abs_diff_eq!(Degrees::new(180.0).sin(), 0.0, epsilon = 1e-12);
        assert_abs_diff_eq!(Degrees::new(270.0).sin(), -1.0, epsilon = 1e-12);
    }

    #[test]
    fn trig_cos_known_values() {
        assert_abs_diff_eq!(Degrees::new(0.0).cos(), 1.0, epsilon = 1e-12);
        assert_abs_diff_eq!(Degrees::new(60.0).cos(), 0.5, epsilon = 1e-12);
        assert_abs_diff_eq!(Degrees::new(90.0).cos(), 0.0, epsilon = 1e-12);
        assert_abs_diff_eq!(Degrees::new(180.0).cos(), -1.0, epsilon = 1e-12);
    }

    #[test]
    fn trig_tan_known_values() {
        assert_abs_diff_eq!(Degrees::new(0.0).tan(), 0.0, epsilon = 1e-12);
        assert_abs_diff_eq!(Degrees::new(45.0).tan(), 1.0, epsilon = 1e-12);
        assert_abs_diff_eq!(Degrees::new(180.0).tan(), 0.0, epsilon = 1e-12);
    }

    #[test]
    fn trig_sin_cos_consistency() {
        let angle = Degrees::new(37.5);
        let (sin, cos) = angle.sin_cos();
        assert_abs_diff_eq!(sin, angle.sin(), epsilon = 1e-15);
        assert_abs_diff_eq!(cos, angle.cos(), epsilon = 1e-15);
    }

    #[test]
    fn trig_pythagorean_identity() {
        let angle = Degrees::new(123.456);
        let sin = angle.sin();
        let cos = angle.cos();
        assert_abs_diff_eq!(sin * sin + cos * cos, 1.0, epsilon = 1e-12);
    }

    #[test]
    fn trig_radians() {
        assert_abs_diff_eq!(Radians::new(0.0).sin(), 0.0, epsilon = 1e-12);
        assert_abs_diff_eq!(Radians::new(PI / 2.0).sin(), 1.0, epsilon = 1e-12);
        assert_abs_diff_eq!(Radians::new(PI).cos(), -1.0, epsilon = 1e-12);
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // signum
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn signum_positive() {
        assert_eq!(Degrees::new(45.0).signum(), 1.0);
    }

    #[test]
    fn signum_negative() {
        assert_eq!(Degrees::new(-45.0).signum(), -1.0);
    }

    #[test]
    fn signum_zero() {
        assert_eq!(Degrees::new(0.0).signum(), 1.0);
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // wrap_pos (normalize)
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn wrap_pos_basic() {
        assert_abs_diff_eq!(
            Degrees::new(370.0).wrap_pos().value(),
            10.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(Degrees::new(720.0).wrap_pos().value(), 0.0, epsilon = 1e-12);
        assert_abs_diff_eq!(Degrees::new(0.0).wrap_pos().value(), 0.0, epsilon = 1e-12);
    }

    #[test]
    fn wrap_pos_negative() {
        assert_abs_diff_eq!(
            Degrees::new(-10.0).wrap_pos().value(),
            350.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(-370.0).wrap_pos().value(),
            350.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(-720.0).wrap_pos().value(),
            0.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn wrap_pos_boundary() {
        assert_abs_diff_eq!(Degrees::new(360.0).wrap_pos().value(), 0.0, epsilon = 1e-12);
        assert_abs_diff_eq!(
            Degrees::new(-360.0).wrap_pos().value(),
            0.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn normalize_is_wrap_pos() {
        let angle = Degrees::new(450.0);
        assert_eq!(angle.normalize().value(), angle.wrap_pos().value());
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // wrap_signed: (-180, 180]
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn test_wrap_signed() {
        let a = Degrees::new(370.0).wrap_signed();
        assert_eq!(a.value(), 10.0);
        let b = Degrees::new(-190.0).wrap_signed();
        assert_eq!(b.value(), 170.0);
    }

    #[test]
    fn wrap_signed_basic() {
        assert_abs_diff_eq!(
            Degrees::new(10.0).wrap_signed().value(),
            10.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(-10.0).wrap_signed().value(),
            -10.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn wrap_signed_over_180() {
        assert_abs_diff_eq!(
            Degrees::new(190.0).wrap_signed().value(),
            -170.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(270.0).wrap_signed().value(),
            -90.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn wrap_signed_boundary_180() {
        assert_abs_diff_eq!(
            Degrees::new(180.0).wrap_signed().value(),
            180.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(-180.0).wrap_signed().value(),
            180.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn wrap_signed_large_values() {
        assert_abs_diff_eq!(
            Degrees::new(540.0).wrap_signed().value(),
            180.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(-540.0).wrap_signed().value(),
            180.0,
            epsilon = 1e-12
        );
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // wrap_quarter_fold: [-90, 90]
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn wrap_quarter_fold_basic() {
        assert_abs_diff_eq!(
            Degrees::new(0.0).wrap_quarter_fold().value(),
            0.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(45.0).wrap_quarter_fold().value(),
            45.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(-45.0).wrap_quarter_fold().value(),
            -45.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn wrap_quarter_fold_boundary() {
        assert_abs_diff_eq!(
            Degrees::new(90.0).wrap_quarter_fold().value(),
            90.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(-90.0).wrap_quarter_fold().value(),
            -90.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn wrap_quarter_fold_over_90() {
        assert_abs_diff_eq!(
            Degrees::new(100.0).wrap_quarter_fold().value(),
            80.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(135.0).wrap_quarter_fold().value(),
            45.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(180.0).wrap_quarter_fold().value(),
            0.0,
            epsilon = 1e-12
        );
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // Separation helpers
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn signed_separation_basic() {
        let a = Degrees::new(30.0);
        let b = Degrees::new(50.0);
        assert_abs_diff_eq!(a.signed_separation(b).value(), -20.0, epsilon = 1e-12);
        assert_abs_diff_eq!(b.signed_separation(a).value(), 20.0, epsilon = 1e-12);
    }

    #[test]
    fn signed_separation_wrap() {
        let a = Degrees::new(10.0);
        let b = Degrees::new(350.0);
        assert_abs_diff_eq!(a.signed_separation(b).value(), 20.0, epsilon = 1e-12);
        assert_abs_diff_eq!(b.signed_separation(a).value(), -20.0, epsilon = 1e-12);
    }

    #[test]
    fn abs_separation() {
        let a = Degrees::new(30.0);
        let b = Degrees::new(50.0);
        assert_abs_diff_eq!(a.abs_separation(b).value(), 20.0, epsilon = 1e-12);
        assert_abs_diff_eq!(b.abs_separation(a).value(), 20.0, epsilon = 1e-12);
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // wrap_to_signed_pi / wrap_to_unsigned_pi / fold_to_pi
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn wrap_to_signed_pi_matches_wrap_signed_radians() {
        for &x in &[
            -7.0 * PI,
            -PI - 0.1,
            -PI,
            -1.0,
            0.0,
            1.0,
            PI,
            PI + 0.1,
            7.0 * PI,
        ] {
            let a = Radians::new(x);
            assert_abs_diff_eq!(
                a.wrap_to_signed_pi().value(),
                a.wrap_signed().value(),
                epsilon = 1e-12
            );
        }
    }

    #[test]
    fn wrap_to_signed_pi_degree_analog() {
        // The same generic helper provides the (-180°, 180°] semantics for Degrees.
        assert_abs_diff_eq!(
            Degrees::new(190.0).wrap_to_signed_pi().value(),
            -170.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(180.0).wrap_to_signed_pi().value(),
            180.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(-180.0).wrap_to_signed_pi().value(),
            180.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(7.0 * 180.0).wrap_to_signed_pi().value(),
            180.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn wrap_to_unsigned_pi_matches_wrap_pos_radians() {
        for &x in &[-7.0 * PI, -PI, -1.0, 0.0, 1.0, PI, TAU, 7.0 * PI] {
            let a = Radians::new(x);
            assert_abs_diff_eq!(
                a.wrap_to_unsigned_pi().value(),
                a.wrap_pos().value(),
                epsilon = 1e-12
            );
        }
    }

    #[test]
    fn wrap_to_unsigned_pi_degree_analog() {
        assert_abs_diff_eq!(
            Degrees::new(370.0).wrap_to_unsigned_pi().value(),
            10.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(-10.0).wrap_to_unsigned_pi().value(),
            350.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(360.0).wrap_to_unsigned_pi().value(),
            0.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn fold_to_pi_radians() {
        assert_abs_diff_eq!(Radians::new(0.5).fold_to_pi().value(), 0.5, epsilon = 1e-12);
        assert_abs_diff_eq!(
            Radians::new(-0.5).fold_to_pi().value(),
            0.5,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Radians::new(PI + 0.1).fold_to_pi().value(),
            PI - 0.1,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Radians::new(7.0 * PI).fold_to_pi().value(),
            PI,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(Radians::new(PI).fold_to_pi().value(), PI, epsilon = 1e-12);
        assert_abs_diff_eq!(Radians::new(0.0).fold_to_pi().value(), 0.0, epsilon = 1e-12);
    }

    #[test]
    fn fold_to_pi_degree_analog() {
        assert_abs_diff_eq!(
            Degrees::new(190.0).fold_to_pi().value(),
            170.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(-30.0).fold_to_pi().value(),
            30.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(360.0 * 7.0 + 50.0).fold_to_pi().value(),
            50.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn fold_to_pi_nan_propagates() {
        assert!(Radians::new(f64::NAN).fold_to_pi().value().is_nan());
        assert!(Degrees::new(f64::NAN).fold_to_pi().value().is_nan());
    }

    /// Parity with NSB's existing fold semantics: `delta_lambda.to_degrees().abs().min(180.0)`
    /// applied to the signed difference of two ecliptic longitudes must equal
    /// `(a - b).fold_to_pi()` (in degrees).
    #[test]
    fn fold_to_pi_matches_nsb_legacy_loop() {
        // Reference implementation mirroring the historic NSB normalization
        // (`while delta > π { delta -= 2π } / while delta < -π { delta += 2π }` then `.abs()`).
        fn nsb_legacy_fold(mut delta: f64) -> f64 {
            while delta > PI {
                delta -= TAU;
            }
            while delta < -PI {
                delta += TAU;
            }
            delta.abs()
        }

        let samples = [
            -7.5 * PI,
            -PI - 1e-9,
            -PI,
            -PI / 2.0,
            -1e-12,
            0.0,
            1e-12,
            PI / 4.0,
            PI - 1e-9,
            PI,
            PI + 1e-9,
            3.0 * PI / 2.0,
            7.5 * PI,
        ];
        for &x in &samples {
            let helper = Radians::new(x).fold_to_pi().value();
            let legacy = nsb_legacy_fold(x);
            assert_abs_diff_eq!(helper, legacy, epsilon = 1e-9);
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // DMS / HMS construction
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn degrees_from_dms_positive() {
        let d = Degrees::from_dms(12, 30, 0.0);
        assert_abs_diff_eq!(d.value(), 12.5, epsilon = 1e-12);
    }

    #[test]
    fn degrees_from_dms_negative() {
        let d = Degrees::from_dms(-33, 52, 0.0);
        assert!(d.value() < 0.0);
        assert_abs_diff_eq!(d.value(), -(33.0 + 52.0 / 60.0), epsilon = 1e-12);
    }

    #[test]
    fn degrees_from_dms_with_seconds() {
        let d = Degrees::from_dms(10, 20, 30.0);
        assert_abs_diff_eq!(
            d.value(),
            10.0 + 20.0 / 60.0 + 30.0 / 3600.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn degrees_from_dms_sign() {
        let pos = Degrees::from_dms_sign(1, 45, 30, 0.0);
        let neg = Degrees::from_dms_sign(-1, 45, 30, 0.0);
        assert_abs_diff_eq!(pos.value(), 45.5, epsilon = 1e-12);
        assert_abs_diff_eq!(neg.value(), -45.5, epsilon = 1e-12);
    }

    #[test]
    fn degrees_from_dms_sign_zero_is_positive() {
        let zero_sign = Degrees::from_dms_sign(0, 45, 30, 0.0);
        assert_abs_diff_eq!(zero_sign.value(), 45.5, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn hour_angles_from_hms() {
        let ha = HourAngles::from_hms(5, 30, 0.0);
        assert_abs_diff_eq!(ha.value(), 5.5, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn hour_angles_from_hms_negative() {
        let ha = HourAngles::from_hms(-3, 15, 0.0);
        assert_abs_diff_eq!(ha.value(), -3.25, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn hour_angles_to_degrees() {
        let ha = HourAngles::new(6.0);
        let deg = ha.to::<Degree>();
        assert_abs_diff_eq!(deg.value(), 90.0, epsilon = 1e-12);
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // Display formatting
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn display_degrees() {
        let d = Degrees::new(45.5);
        assert_eq!(format!("{}", d), "45.5 °");
    }

    #[test]
    fn display_radians() {
        let r = Radians::new(1.0);
        assert_eq!(format!("{}", r), "1 rad");
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // Unit constants
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn unit_constants() {
        assert_eq!(DEG.value(), 1.0);
        assert_eq!(RAD.value(), 1.0);
        assert_eq!(MRAD.value(), 1.0);
        #[cfg(feature = "astro")]
        {
            assert_eq!(ARCM.value(), 1.0);
            assert_eq!(ARCS.value(), 1.0);
            assert_eq!(MAS.value(), 1.0);
            assert_eq!(UAS.value(), 1.0);
            assert_eq!(HOUR_ANGLE.value(), 1.0);
        }
        #[cfg(feature = "navigation")]
        assert_eq!(GON.value(), 1.0);
        assert_eq!(TURN.value(), 1.0);
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // wrap_signed_lo: [-180, 180)
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    fn wrap_signed_lo_boundary_half_turn() {
        // +half turn should map to -half turn to make the upper bound exclusive.
        assert_abs_diff_eq!(
            Degrees::new(180.0).wrap_signed_lo().value(),
            -180.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Degrees::new(-180.0).wrap_signed_lo().value(),
            -180.0,
            epsilon = 1e-12
        );
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // New unit conversions and tests
    // ─────────────────────────────────────────────────────────────────────────────

    #[test]
    #[cfg(feature = "astro")]
    fn conversion_degrees_to_arcminutes() {
        let deg = Degrees::new(1.0);
        let arcm = deg.to::<Arcminute>();
        assert_abs_diff_eq!(arcm.value(), 60.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn conversion_arcminutes_to_degrees() {
        let arcm = Arcminutes::new(60.0);
        let deg = arcm.to::<Degree>();
        assert_abs_diff_eq!(deg.value(), 1.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn conversion_arcminutes_to_arcseconds() {
        let arcm = Arcminutes::new(1.0);
        let arcs = arcm.to::<Arcsecond>();
        assert_abs_diff_eq!(arcs.value(), 60.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn conversion_arcseconds_to_microarcseconds() {
        let arcs = Arcseconds::new(1.0);
        let uas = arcs.to::<MicroArcsecond>();
        assert_abs_diff_eq!(uas.value(), 1_000_000.0, epsilon = 1e-6);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn conversion_microarcseconds_to_degrees() {
        let uas = MicroArcseconds::new(3_600_000_000.0);
        let deg = uas.to::<Degree>();
        assert_abs_diff_eq!(deg.value(), 1.0, epsilon = 1e-9);
    }

    #[test]
    #[cfg(feature = "navigation")]
    fn conversion_degrees_to_gradians() {
        let deg = Degrees::new(90.0);
        let gon = deg.to::<Gradian>();
        assert_abs_diff_eq!(gon.value(), 100.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "navigation")]
    fn conversion_gradians_to_degrees() {
        let gon = Gradians::new(400.0);
        let deg = gon.to::<Degree>();
        assert_abs_diff_eq!(deg.value(), 360.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "navigation")]
    fn conversion_gradians_to_radians() {
        let gon = Gradians::new(200.0);
        let rad = gon.to::<Radian>();
        assert_abs_diff_eq!(rad.value(), PI, epsilon = 1e-12);
    }

    #[test]
    fn conversion_degrees_to_turns() {
        let deg = Degrees::new(360.0);
        let turn = deg.to::<Turn>();
        assert_abs_diff_eq!(turn.value(), 1.0, epsilon = 1e-12);
    }

    #[test]
    fn conversion_milliradians_to_radians() {
        let mrad = Milliradians::new(1_000.0);
        let rad = mrad.to::<Radian>();
        assert_abs_diff_eq!(rad.value(), 1.0, epsilon = 1e-12);
    }

    #[test]
    fn conversion_turns_to_degrees() {
        let turn = Turns::new(2.5);
        let deg = turn.to::<Degree>();
        assert_abs_diff_eq!(deg.value(), 900.0, epsilon = 1e-12);
    }

    #[test]
    fn conversion_turns_to_radians() {
        let turn = Turns::new(1.0);
        let rad = turn.to::<Radian>();
        assert_abs_diff_eq!(rad.value(), TAU, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn from_impl_new_units() {
        // Test From trait implementations for new units
        let deg = Degrees::new(1.0);
        let arcm: Arcminutes = deg.into();
        assert_abs_diff_eq!(arcm.value(), 60.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "navigation")]
    fn from_impl_gradian_to_deg() {
        let gon = Gradians::new(100.0);
        let deg: Degrees = gon.into();
        assert_abs_diff_eq!(deg.value(), 90.0, epsilon = 1e-12);
    }

    #[test]
    fn from_impl_turn_to_deg() {
        let turn = Turns::new(0.25);
        let deg: Degrees = turn.into();
        assert_abs_diff_eq!(deg.value(), 90.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn roundtrip_arcminute_arcsecond() {
        let original = Arcminutes::new(5.0);
        let arcs = original.to::<Arcsecond>();
        let back = arcs.to::<Arcminute>();
        assert_abs_diff_eq!(back.value(), original.value(), epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "navigation")]
    fn roundtrip_gradian_degree() {
        let original = Gradians::new(123.456);
        let deg = original.to::<Degree>();
        let back = deg.to::<Gradian>();
        assert_abs_diff_eq!(back.value(), original.value(), epsilon = 1e-12);
    }

    #[test]
    fn roundtrip_turn_radian() {
        let original = Turns::new(2.717);
        let rad = original.to::<Radian>();
        let back = rad.to::<Turn>();
        assert_abs_diff_eq!(back.value(), original.value(), epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "navigation")]
    fn gradian_full_turn() {
        assert_abs_diff_eq!(Gradian::FULL_TURN, 400.0, epsilon = 1e-12);
    }

    #[test]
    fn turn_full_turn() {
        assert_abs_diff_eq!(Turn::FULL_TURN, 1.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn arcminute_full_turn() {
        assert_abs_diff_eq!(Arcminute::FULL_TURN, 21_600.0, epsilon = 1e-9);
    }

    #[test]
    #[cfg(feature = "astro")]
    fn microarcsecond_conversion_chain() {
        // Test a long conversion chain
        let uas = MicroArcseconds::new(1e9);
        let mas = uas.to::<MilliArcsecond>();
        let arcs = mas.to::<Arcsecond>();
        let arcm = arcs.to::<Arcminute>();
        let deg = arcm.to::<Degree>();

        assert_abs_diff_eq!(mas.value(), 1_000_000.0, epsilon = 1e-6);
        assert_abs_diff_eq!(arcs.value(), 1_000.0, epsilon = 1e-9);
        assert_abs_diff_eq!(arcm.value(), 1_000.0 / 60.0, epsilon = 1e-9);
        assert_relative_eq!(deg.value(), 1_000.0 / 3600.0, max_relative = 1e-9);
    }

    #[test]
    fn wrap_pos_with_turns() {
        let turn = Turns::new(2.7);
        let wrapped = turn.wrap_pos();
        assert_abs_diff_eq!(wrapped.value(), 0.7, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "navigation")]
    fn wrap_signed_with_gradians() {
        let gon = Gradians::new(350.0);
        let wrapped = gon.wrap_signed();
        assert_abs_diff_eq!(wrapped.value(), -50.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(feature = "navigation")]
    fn trig_with_gradians() {
        let gon = Gradians::new(100.0); // 90 degrees
        assert_abs_diff_eq!(gon.sin(), 1.0, epsilon = 1e-12);
        assert_abs_diff_eq!(gon.cos(), 0.0, epsilon = 1e-12);
    }

    #[test]
    fn trig_with_turns() {
        let turn = Turns::new(0.25); // 90 degrees
        assert_abs_diff_eq!(turn.sin(), 1.0, epsilon = 1e-12);
        assert_abs_diff_eq!(turn.cos(), 0.0, epsilon = 1e-12);
    }

    #[test]
    #[cfg(all(feature = "astro", feature = "navigation"))]
    fn all_units_to_degrees() {
        // Verify all units convert correctly to degrees
        assert_abs_diff_eq!(
            Radians::new(PI).to::<Degree>().value(),
            180.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Arcminutes::new(60.0).to::<Degree>().value(),
            1.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Arcseconds::new(3600.0).to::<Degree>().value(),
            1.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            MilliArcseconds::new(3_600_000.0).to::<Degree>().value(),
            1.0,
            epsilon = 1e-9
        );
        assert_abs_diff_eq!(
            MicroArcseconds::new(3_600_000_000.0).to::<Degree>().value(),
            1.0,
            epsilon = 1e-6
        );
        assert_abs_diff_eq!(
            Gradians::new(100.0).to::<Degree>().value(),
            90.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            Turns::new(1.0).to::<Degree>().value(),
            360.0,
            epsilon = 1e-12
        );
        assert_abs_diff_eq!(
            HourAngles::new(1.0).to::<Degree>().value(),
            15.0,
            epsilon = 1e-12
        );
    }

    // ─────────────────────────────────────────────────────────────────────────────
    // Property-based tests
    // ─────────────────────────────────────────────────────────────────────────────

    proptest! {
        #[test]
        fn prop_wrap_pos_range(angle in -1e6..1e6f64) {
            let wrapped = Degrees::new(angle).wrap_pos();
            prop_assert!(wrapped.value() >= 0.0);
            prop_assert!(wrapped.value() < 360.0);
        }

        #[test]
        fn prop_wrap_signed_range(angle in -1e6..1e6f64) {
            let wrapped = Degrees::new(angle).wrap_signed();
            prop_assert!(wrapped.value() > -180.0);
            prop_assert!(wrapped.value() <= 180.0);
        }

        #[test]
        fn prop_wrap_quarter_fold_range(angle in -1e6..1e6f64) {
            let wrapped = Degrees::new(angle).wrap_quarter_fold();
            prop_assert!(wrapped.value() >= -90.0);
            prop_assert!(wrapped.value() <= 90.0);
        }

        #[test]
        fn prop_pythagorean_identity(angle in -360.0..360.0f64) {
            let a = Degrees::new(angle);
            let sin = a.sin();
            let cos = a.cos();
            assert_abs_diff_eq!(sin * sin + cos * cos, 1.0, epsilon = 1e-12);
        }

        #[test]
        fn prop_conversion_roundtrip(angle in -1e6..1e6f64) {
            let deg = Degrees::new(angle);
            let rad = deg.to::<Radian>();
            let back = rad.to::<Degree>();
            assert_relative_eq!(back.value(), deg.value(), max_relative = 1e-12);
        }

        #[test]
        fn prop_abs_separation_symmetric(a in -360.0..360.0f64, b in -360.0..360.0f64) {
            let da = Degrees::new(a);
            let db = Degrees::new(b);
            assert_abs_diff_eq!(
                da.abs_separation(db).value(),
                db.abs_separation(da).value(),
                epsilon = 1e-12
            );
        }
    }

    /// Invoke derive-generated PartialEq/Clone/Debug on each unit struct to
    /// cover the #[derive] attribute lines tracked by llvm-cov.
    #[test]
    fn derive_coverage_unit_structs() {
        // Direct struct value comparisons invoke <T as PartialEq>::eq() which
        // is the derive-generated implementation, covering the #[derive] line.
        assert!(Degree == Degree);
        assert!(Radian == Radian);
        assert!(Milliradian == Milliradian);
        #[cfg(feature = "astro")]
        {
            assert!(Arcminute == Arcminute);
            assert!(Arcsecond == Arcsecond);
            assert!(MilliArcsecond == MilliArcsecond);
            assert!(MicroArcsecond == MicroArcsecond);
            assert!(HourAngle == HourAngle);
        }
        #[cfg(feature = "navigation")]
        assert!(Gradian == Gradian);
        assert!(Turn == Turn);
        // signum_const: cover both positive and negative branches
        let pos = Degrees::new(90.0);
        let neg = Degrees::new(-45.0);
        assert_eq!(pos.signum_const(), 1.0);
        assert_eq!(neg.signum_const(), -1.0);
    }
}