decimal-scaled 0.4.1

Const-generic base-10 fixed-point decimals (D9/D18/D38/D76/D153/D307) with integer-only transcendentals correctly rounded to within 0.5 ULP — exact at the type's last representable place. Deterministic across every platform; no_std-friendly.
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
//! Trigonometric, hyperbolic, and angle-conversion methods for [`D38`].
//!
//! # Surface
//!
//! Fifteen mathematical functions:
//!
//! - **Forward trig (radians input):** [`D38::sin`] / [`D38::cos`] /
//! [`D38::tan`].
//! - **Inverse trig (returns radians):** [`D38::asin`] / [`D38::acos`]
//! / [`D38::atan`] / [`D38::atan2`].
//! - **Hyperbolic:** [`D38::sinh`] / [`D38::cosh`] / [`D38::tanh`] /
//! [`D38::asinh`] / [`D38::acosh`] / [`D38::atanh`].
//! - **Angle conversions:** [`D38::to_degrees`] / [`D38::to_radians`].
//!
//! # The four-variant matrix
//!
//! Each function ships with four entry points so a single name covers
//! every (precision × rounding) combination the surface needs:
//!
//! | Method            | Guard width    | Rounding mode               |
//! |-------------------|----------------|------------------------------|
//! | `<fn>_strict`     | crate default  | crate default ([`RoundingMode::HalfToEven`] unless a `rounding-*` feature is set) |
//! | `<fn>_strict_with`| crate default  | caller-supplied              |
//! | `<fn>_approx`     | caller-chosen  | crate default               |
//! | `<fn>_approx_with`| caller-chosen  | caller-supplied              |
//!
//! All four variants are integer-only, `no_std`-compatible, and
//! correctly rounded under the selected mode. Without the `strict`
//! feature, the plain `<fn>` is an f64-bridge instead.
//!
//! # Layering
//!
//! Every public method on this file is a one-line delegate into
//! `policy::trig::TrigPolicy`. The correctly-rounded kernels
//! (`sin_fixed`, `atan_fixed`, `atan2_kernel`, `to_fixed`, `wide_pi`,
//! `wide_half_pi`, `small_x_linear_threshold`, and every per-method
//! `*_strict` / `*_with` `Fixed`-shape function for sin / cos / tan /
//! atan / asin / acos / atan2 / sinh / cosh / tanh / asinh / acosh /
//! atanh / to_degrees / to_radians) live in
//! [`crate::algos::trig::fixed_d38`]. This file is a typed-shell
//! surface; there are zero `crate::algos::*` or
//! `crate::algos::fixed_d38::*` references in it.
//!
//! [`RoundingMode::HalfToEven`]: crate::RoundingMode::HalfToEven
//!
//! # `atan2` signature
//!
//! `f64::atan2(self, other)` treats `self` as `y` and `other` as `x`.
//! This module matches that signature exactly so generic numeric code
//! calling `y.atan2(x)` works with `T = D38`.

use crate::types::widths::D38;
use crate::types::log_exp::STRICT_GUARD;

impl<const SCALE: u32> D38<SCALE> {
    // ── Plain dispatchers (strict-feature) ────────────────────────

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn sin(self) -> Self {
        self.sin_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn cos(self) -> Self {
        self.cos_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn tan(self) -> Self {
        self.tan_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn asin(self) -> Self {
        self.asin_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn acos(self) -> Self {
        self.acos_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn atan(self) -> Self {
        self.atan_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn atan2(self, other: Self) -> Self {
        self.atan2_strict(other)
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn sinh(self) -> Self {
        self.sinh_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn cosh(self) -> Self {
        self.cosh_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn tanh(self) -> Self {
        self.tanh_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn asinh(self) -> Self {
        self.asinh_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn acosh(self) -> Self {
        self.acosh_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn atanh(self) -> Self {
        self.atanh_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn to_degrees(self) -> Self {
        self.to_degrees_strict()
    }

    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[inline]
    #[must_use]
    pub fn to_radians(self) -> Self {
        self.to_radians_strict()
    }

    // ── Forward trig (one-line policy delegates) ──────────────────

    /// Sine of `self` (radians). Correctly rounded.
    #[inline]
    #[must_use]
    pub fn sin_strict(self) -> Self {
        self.sin_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn sin_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::sin_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn sin_approx(self, working_digits: u32) -> Self {
        self.sin_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn sin_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.sin_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::sin_with_impl(self, working_digits, mode)
    }

    /// Cosine of `self` (radians). `cos(x) = sin(x + π/2)`.
    #[inline]
    #[must_use]
    pub fn cos_strict(self) -> Self {
        self.cos_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn cos_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::cos_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn cos_approx(self, working_digits: u32) -> Self {
        self.cos_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn cos_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.cos_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::cos_with_impl(self, working_digits, mode)
    }

    /// Tangent. Panics if `cos(self)` is zero.
    #[inline]
    #[must_use]
    pub fn tan_strict(self) -> Self {
        self.tan_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn tan_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::tan_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn tan_approx(self, working_digits: u32) -> Self {
        self.tan_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn tan_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.tan_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::tan_with_impl(self, working_digits, mode)
    }

    /// Arctangent.
    #[inline]
    #[must_use]
    pub fn atan_strict(self) -> Self {
        self.atan_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn atan_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::atan_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn atan_approx(self, working_digits: u32) -> Self {
        self.atan_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn atan_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.atan_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::atan_with_impl(self, working_digits, mode)
    }

    /// Arcsine. Panics if `|self| > 1`.
    #[inline]
    #[must_use]
    pub fn asin_strict(self) -> Self {
        self.asin_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn asin_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::asin_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn asin_approx(self, working_digits: u32) -> Self {
        self.asin_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn asin_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.asin_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::asin_with_impl(self, working_digits, mode)
    }

    /// Arccosine. Panics if `|self| > 1`.
    #[inline]
    #[must_use]
    pub fn acos_strict(self) -> Self {
        self.acos_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn acos_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::acos_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn acos_approx(self, working_digits: u32) -> Self {
        self.acos_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn acos_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.acos_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::acos_with_impl(self, working_digits, mode)
    }

    /// Four-quadrant arctangent of `self` (`y`) and `other` (`x`).
    #[inline]
    #[must_use]
    pub fn atan2_strict(self, other: Self) -> Self {
        self.atan2_strict_with(other, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn atan2_strict_with(self, other: Self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::atan2_impl(self, other, mode)
    }

    #[inline]
    #[must_use]
    pub fn atan2_approx(self, other: Self, working_digits: u32) -> Self {
        self.atan2_approx_with(other, working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn atan2_approx_with(self, other: Self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.atan2_strict_with(other, mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::atan2_with_impl(self, other, working_digits, mode)
    }

    // ── Hyperbolic family (one-line policy delegates) ─────────────

    /// Hyperbolic sine. Correctly rounded.
    #[inline]
    #[must_use]
    pub fn sinh_strict(self) -> Self {
        self.sinh_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn sinh_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::sinh_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn sinh_approx(self, working_digits: u32) -> Self {
        self.sinh_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn sinh_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.sinh_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::sinh_with_impl(self, working_digits, mode)
    }

    /// Hyperbolic cosine.
    #[inline]
    #[must_use]
    pub fn cosh_strict(self) -> Self {
        self.cosh_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn cosh_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::cosh_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn cosh_approx(self, working_digits: u32) -> Self {
        self.cosh_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn cosh_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.cosh_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::cosh_with_impl(self, working_digits, mode)
    }

    /// Hyperbolic tangent.
    #[inline]
    #[must_use]
    pub fn tanh_strict(self) -> Self {
        self.tanh_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn tanh_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::tanh_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn tanh_approx(self, working_digits: u32) -> Self {
        self.tanh_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn tanh_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.tanh_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::tanh_with_impl(self, working_digits, mode)
    }

    /// Inverse hyperbolic sine. `asinh(x) = sign · ln(|x| + √(x²+1))`.
    #[inline]
    #[must_use]
    pub fn asinh_strict(self) -> Self {
        self.asinh_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn asinh_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::asinh_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn asinh_approx(self, working_digits: u32) -> Self {
        self.asinh_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn asinh_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.asinh_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::asinh_with_impl(self, working_digits, mode)
    }

    /// Inverse hyperbolic cosine. Panics if `self < 1`.
    #[inline]
    #[must_use]
    pub fn acosh_strict(self) -> Self {
        self.acosh_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn acosh_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::acosh_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn acosh_approx(self, working_digits: u32) -> Self {
        self.acosh_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn acosh_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.acosh_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::acosh_with_impl(self, working_digits, mode)
    }

    /// Inverse hyperbolic tangent. Panics if `|self| >= 1`.
    #[inline]
    #[must_use]
    pub fn atanh_strict(self) -> Self {
        self.atanh_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn atanh_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::atanh_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn atanh_approx(self, working_digits: u32) -> Self {
        self.atanh_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn atanh_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.atanh_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::atanh_with_impl(self, working_digits, mode)
    }

    // ── Angle conversions (one-line policy delegates) ─────────────

    /// Convert radians to degrees: `self · (180 / π)`.
    #[inline]
    #[must_use]
    pub fn to_degrees_strict(self) -> Self {
        self.to_degrees_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn to_degrees_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::to_degrees_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn to_degrees_approx(self, working_digits: u32) -> Self {
        self.to_degrees_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn to_degrees_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.to_degrees_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::to_degrees_with_impl(self, working_digits, mode)
    }

    /// Convert degrees to radians: `self · (π / 180)`.
    #[inline]
    #[must_use]
    pub fn to_radians_strict(self) -> Self {
        self.to_radians_strict_with(crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn to_radians_strict_with(self, mode: crate::support::rounding::RoundingMode) -> Self {
        <Self as crate::policy::trig::TrigPolicy>::to_radians_impl(self, mode)
    }

    #[inline]
    #[must_use]
    pub fn to_radians_approx(self, working_digits: u32) -> Self {
        self.to_radians_approx_with(working_digits, crate::support::rounding::DEFAULT_ROUNDING_MODE)
    }

    #[inline]
    #[must_use]
    pub fn to_radians_approx_with(self, working_digits: u32, mode: crate::support::rounding::RoundingMode) -> Self {
        if working_digits == STRICT_GUARD {
            return self.to_radians_strict_with(mode);
        }
        <Self as crate::policy::trig::TrigPolicy>::to_radians_with_impl(self, working_digits, mode)
    }
}

#[cfg(test)]
mod tests {
    use crate::types::consts::DecimalConstants;
    use crate::types::widths::D38s12;

    // Tolerance for single-operation results. The f64-bridge build is
    // one f64 round-trip (≤ 2 LSB); the integer-only `strict` build is
    // correctly rounded (≤ 0.5 ULP per call) and is held to the same
    // 2-LSB bound — a couple of LSB for the test's own expected-value
    // rounding.
    const TWO_LSB: i128 = 2;

    // Tolerance for results that chain multiple trig calls.
    const FOUR_LSB: i128 = 4;

    // Angle conversions amplify the f64 reference's pi quantization;
    // 32 LSB at SCALE = 12.
    const ANGLE_TOLERANCE_LSB: i128 = 32;

    fn within_lsb(actual: D38s12, expected: D38s12, lsb: i128) -> bool {
        let diff = (actual.to_bits() - expected.to_bits()).abs();
        diff <= lsb
    }

    // ── Forward trig ──────────────────────────────────────────────────

    /// The strict trig / hyperbolic family is correctly rounded:
    /// cross-check every method against the f64 bridge at D38<9>.
    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[test]
    fn strict_trig_family_matches_f64() {
        use crate::types::widths::D38;
        macro_rules! check {
            ($name:literal, $raw:expr, $strict:expr, $f64expr:expr) => {{
                let strict: i128 = $strict;
                let v = $raw as f64 / 1e9;
                let reference = ($f64expr(v) * 1e9).round() as i128;
                assert!(
                    (strict - reference).abs() <= 2,
                    concat!($name, "({}) = {}, f64 reference {}"),
                    $raw,
                    strict,
                    reference
                );
            }};
        }
        for &raw in &[
            -7_000_000_000_i128, -1_000_000_000, -100_000_000, 1,
            500_000_000, 1_000_000_000, 1_570_796_327, 3_000_000_000,
            6_283_185_307, 12_000_000_000,
        ] {
            let x = D38::<9>::from_bits(raw);
            check!("sin", raw, x.sin_strict().to_bits(), f64::sin);
            check!("cos", raw, x.cos_strict().to_bits(), f64::cos);
            check!("atan", raw, x.atan_strict().to_bits(), f64::atan);
            check!("sinh", raw, x.sinh_strict().to_bits(), f64::sinh);
            check!("cosh", raw, x.cosh_strict().to_bits(), f64::cosh);
            check!("tanh", raw, x.tanh_strict().to_bits(), f64::tanh);
            check!("asinh", raw, x.asinh_strict().to_bits(), f64::asinh);
        }
        for &raw in &[
            -1_000_000_000_i128, -700_000_000, -100_000_000, 0,
            250_000_000, 500_000_000, 999_999_999,
        ] {
            let x = D38::<9>::from_bits(raw);
            check!("asin", raw, x.asin_strict().to_bits(), f64::asin);
            check!("acos", raw, x.acos_strict().to_bits(), f64::acos);
        }
        for &raw in &[-900_000_000_i128, -300_000_000, 1, 300_000_000, 900_000_000] {
            let x = D38::<9>::from_bits(raw);
            check!("atanh", raw, x.atanh_strict().to_bits(), f64::atanh);
        }
        for &raw in &[1_000_000_000_i128, 1_500_000_000, 3_000_000_000, 50_000_000_000] {
            let x = D38::<9>::from_bits(raw);
            check!("acosh", raw, x.acosh_strict().to_bits(), f64::acosh);
        }
        for &raw in &[-1_000_000_000_i128, 1, 500_000_000, 1_000_000_000, 1_400_000_000] {
            let x = D38::<9>::from_bits(raw);
            check!("tan", raw, x.tan_strict().to_bits(), f64::tan);
        }
    }

    /// `sin(0) == 0`.
    #[test]
    fn sin_zero_is_zero() {
        assert_eq!(D38s12::ZERO.sin(), D38s12::ZERO);
    }

    /// Regression: D38 strict trig at high SCALE drives the working
    /// scale `w = SCALE + STRICT_GUARD` past the old hard-coded
    /// 63-digit π constant.
    #[cfg(all(feature = "strict", not(feature = "fast")))]
    #[test]
    fn sin_one_correct_past_63_digit_pi_window() {
        use crate::types::widths::D38;
        let expected_35: i128 = 84_147_098_480_789_650_665_250_232_163_029_900;
        let expected_37: i128 =
            8_414_709_848_078_965_066_525_023_216_302_989_996;

        let got_35 = D38::<35>::ONE.sin_strict().to_bits();
        assert!(
            (got_35 - expected_35).abs() <= 1,
            "sin(1) @ D38<35>: got {got_35}, expected {expected_35}"
        );

        let got_37 = D38::<37>::ONE.sin_strict().to_bits();
        assert!(
            (got_37 - expected_37).abs() <= 1,
            "sin(1) @ D38<37>: got {got_37}, expected {expected_37}"
        );
    }

    /// `cos(0) == 1`.
    #[test]
    fn cos_zero_is_one() {
        assert_eq!(D38s12::ZERO.cos(), D38s12::ONE);
    }

    /// `tan(0) == 0`.
    #[test]
    fn tan_zero_is_zero() {
        assert_eq!(D38s12::ZERO.tan(), D38s12::ZERO);
    }

    /// Pythagorean identity.
    #[test]
    fn sin_squared_plus_cos_squared_is_one() {
        for raw in [
            1_234_567_890_123_i128,
            -2_345_678_901_234_i128,
            500_000_000_000_i128,
            -500_000_000_000_i128,
            4_567_891_234_567_i128,
        ] {
            let x = D38s12::from_bits(raw);
            let s = x.sin();
            let c = x.cos();
            let sum = (s * s) + (c * c);
            assert!(
                within_lsb(sum, D38s12::ONE, FOUR_LSB),
                "sin^2 + cos^2 != 1 for raw={raw}: got bits {} (delta {})",
                sum.to_bits(),
                (sum.to_bits() - D38s12::ONE.to_bits()).abs(),
            );
        }
    }

    // ── Inverse trig ──────────────────────────────────────────────────

    #[test]
    fn asin_zero_is_zero() {
        assert_eq!(D38s12::ZERO.asin(), D38s12::ZERO);
    }

    #[test]
    fn acos_one_is_zero() {
        assert_eq!(D38s12::ONE.acos(), D38s12::ZERO);
    }

    #[test]
    fn acos_zero_is_half_pi() {
        let result = D38s12::ZERO.acos();
        assert!(
            within_lsb(result, D38s12::half_pi(), FOUR_LSB),
            "acos(0) bits {}, half_pi bits {}",
            result.to_bits(),
            D38s12::half_pi().to_bits(),
        );
    }

    #[test]
    fn atan_zero_is_zero() {
        assert_eq!(D38s12::ZERO.atan(), D38s12::ZERO);
    }

    #[test]
    fn asin_of_sin_round_trip() {
        for raw in [
            123_456_789_012_i128,
            -123_456_789_012_i128,
            456_789_012_345_i128,
            -456_789_012_345_i128,
            1_234_567_890_123_i128,
            -1_234_567_890_123_i128,
        ] {
            let x = D38s12::from_bits(raw);
            let recovered = x.sin().asin();
            assert!(
                within_lsb(recovered, x, FOUR_LSB),
                "asin(sin(x)) != x for raw={raw}: got bits {} (delta {})",
                recovered.to_bits(),
                (recovered.to_bits() - x.to_bits()).abs(),
            );
        }
    }

    // ── atan2 ─────────────────────────────────────────────────────────

    #[test]
    fn atan2_first_quadrant_diagonal() {
        let one = D38s12::ONE;
        let result = one.atan2(one);
        assert!(
            within_lsb(result, D38s12::quarter_pi(), TWO_LSB),
            "atan2(1, 1) bits {}, quarter_pi bits {}",
            result.to_bits(),
            D38s12::quarter_pi().to_bits(),
        );
    }

    #[test]
    fn atan2_third_quadrant_diagonal() {
        let neg_one = -D38s12::ONE;
        let result = neg_one.atan2(neg_one);
        let three = D38s12::from_int(3);
        let expected = -(D38s12::quarter_pi() * three);
        assert!(
            within_lsb(result, expected, TWO_LSB),
            "atan2(-1, -1) bits {}, expected -3pi/4 bits {}",
            result.to_bits(),
            expected.to_bits(),
        );
    }

    #[test]
    fn atan2_second_quadrant_diagonal() {
        let one = D38s12::ONE;
        let neg_one = -D38s12::ONE;
        let result = one.atan2(neg_one);
        let three = D38s12::from_int(3);
        let expected = D38s12::quarter_pi() * three;
        assert!(
            within_lsb(result, expected, TWO_LSB),
            "atan2(1, -1) bits {}, expected 3pi/4 bits {}",
            result.to_bits(),
            expected.to_bits(),
        );
    }

    #[test]
    fn atan2_fourth_quadrant_diagonal() {
        let one = D38s12::ONE;
        let neg_one = -D38s12::ONE;
        let result = neg_one.atan2(one);
        let expected = -D38s12::quarter_pi();
        assert!(
            within_lsb(result, expected, TWO_LSB),
            "atan2(-1, 1) bits {}, expected -pi/4 bits {}",
            result.to_bits(),
            expected.to_bits(),
        );
    }

    #[test]
    fn atan2_positive_x_axis_is_zero() {
        let zero = D38s12::ZERO;
        let one = D38s12::ONE;
        assert_eq!(zero.atan2(one), D38s12::ZERO);
    }

    // ── Hyperbolic ────────────────────────────────────────────────────

    #[test]
    fn sinh_zero_is_zero() {
        assert_eq!(D38s12::ZERO.sinh(), D38s12::ZERO);
    }

    #[test]
    fn cosh_zero_is_one() {
        assert_eq!(D38s12::ZERO.cosh(), D38s12::ONE);
    }

    #[test]
    fn tanh_zero_is_zero() {
        assert_eq!(D38s12::ZERO.tanh(), D38s12::ZERO);
    }

    #[test]
    fn asinh_zero_is_zero() {
        assert_eq!(D38s12::ZERO.asinh(), D38s12::ZERO);
    }

    #[test]
    fn acosh_one_is_zero() {
        assert_eq!(D38s12::ONE.acosh(), D38s12::ZERO);
    }

    #[test]
    fn atanh_zero_is_zero() {
        assert_eq!(D38s12::ZERO.atanh(), D38s12::ZERO);
    }

    #[test]
    fn cosh_squared_minus_sinh_squared_is_one() {
        if !crate::support::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
        for raw in [
            500_000_000_000_i128,
            -500_000_000_000_i128,
            1_234_567_890_123_i128,
            -1_234_567_890_123_i128,
            2_500_000_000_000_i128,
        ] {
            let x = D38s12::from_bits(raw);
            let ch = x.cosh();
            let sh = x.sinh();
            let diff = (ch * ch) - (sh * sh);
            assert!(
                within_lsb(diff, D38s12::ONE, FOUR_LSB),
                "cosh^2 - sinh^2 != 1 for raw={raw}: got bits {} (delta {})",
                diff.to_bits(),
                (diff.to_bits() - D38s12::ONE.to_bits()).abs(),
            );
        }
    }

    // ── Angle conversions ─────────────────────────────────────────────

    #[test]
    fn to_degrees_pi_is_180() {
        if !crate::support::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
        let pi = D38s12::pi();
        let result = pi.to_degrees();
        let expected = D38s12::from_int(180);
        assert!(
            within_lsb(result, expected, ANGLE_TOLERANCE_LSB),
            "to_degrees(pi) bits {}, expected 180 bits {} (delta {})",
            result.to_bits(),
            expected.to_bits(),
            (result.to_bits() - expected.to_bits()).abs(),
        );
    }

    #[test]
    fn to_radians_180_is_pi() {
        let one_eighty = D38s12::from_int(180);
        let result = one_eighty.to_radians();
        let expected = D38s12::pi();
        assert!(
            within_lsb(result, expected, ANGLE_TOLERANCE_LSB),
            "to_radians(180) bits {}, expected pi bits {} (delta {})",
            result.to_bits(),
            expected.to_bits(),
            (result.to_bits() - expected.to_bits()).abs(),
        );
    }

    #[test]
    fn to_degrees_zero_is_zero() {
        assert_eq!(D38s12::ZERO.to_degrees(), D38s12::ZERO);
    }

    #[test]
    fn to_radians_zero_is_zero() {
        assert_eq!(D38s12::ZERO.to_radians(), D38s12::ZERO);
    }

    #[test]
    fn to_radians_to_degrees_round_trip() {
        for raw in [
            500_000_000_000_i128,
            -500_000_000_000_i128,
            1_234_567_890_123_i128,
            -2_345_678_901_234_i128,
        ] {
            let x = D38s12::from_bits(raw);
            let recovered = x.to_degrees().to_radians();
            assert!(
                within_lsb(recovered, x, FOUR_LSB),
                "to_radians(to_degrees(x)) != x for raw={raw}: got bits {} (delta {})",
                recovered.to_bits(),
                (recovered.to_bits() - x.to_bits()).abs(),
            );
        }
    }

    #[test]
    fn to_degrees_half_pi_is_90() {
        if !crate::support::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
        let result = D38s12::half_pi().to_degrees();
        let expected = D38s12::from_int(90);
        assert!(
            within_lsb(result, expected, ANGLE_TOLERANCE_LSB),
            "to_degrees(half_pi) bits {}, expected 90 bits {} (delta {})",
            result.to_bits(),
            expected.to_bits(),
            (result.to_bits() - expected.to_bits()).abs(),
        );
    }

    #[test]
    fn to_degrees_quarter_pi_is_45() {
        let result = D38s12::quarter_pi().to_degrees();
        let expected = D38s12::from_int(45);
        assert!(
            within_lsb(result, expected, ANGLE_TOLERANCE_LSB),
            "to_degrees(quarter_pi) bits {}, expected 45 bits {} (delta {})",
            result.to_bits(),
            expected.to_bits(),
            (result.to_bits() - expected.to_bits()).abs(),
        );
    }

    // ── Cross-method consistency ──────────────────────────────────────

    #[test]
    fn tan_matches_sin_over_cos() {
        for raw in [
            500_000_000_000_i128,
            -500_000_000_000_i128,
            1_000_000_000_000_i128,
            -1_000_000_000_000_i128,
            123_456_789_012_i128,
        ] {
            let x = D38s12::from_bits(raw);
            let t = x.tan();
            let sc = x.sin() / x.cos();
            assert!(
                within_lsb(t, sc, FOUR_LSB),
                "tan(x) != sin/cos for raw={raw}: tan bits {}, sin/cos bits {}",
                t.to_bits(),
                sc.to_bits(),
            );
        }
    }

    #[test]
    fn tanh_matches_sinh_over_cosh() {
        for raw in [
            500_000_000_000_i128,
            -500_000_000_000_i128,
            1_234_567_890_123_i128,
            -2_345_678_901_234_i128,
        ] {
            let x = D38s12::from_bits(raw);
            let t = x.tanh();
            let sc = x.sinh() / x.cosh();
            assert!(
                within_lsb(t, sc, FOUR_LSB),
                "tanh(x) != sinh/cosh for raw={raw}: tanh bits {}, sinh/cosh bits {}",
                t.to_bits(),
                sc.to_bits(),
            );
        }
    }
}