fin_decimal 0.3.0

A high-performance decimal fixed-point arithmetic library for financial applications
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
//! 128-bit backed decimal fixed-point type.
//!
//! [`Decimal128`] follows the same power-of-10 scaling design as
//! [`Decimal`](crate::Decimal), widened to an `i128` backing. Addition and
//! subtraction stay native integer operations; multiplication and division go
//! through a 256-bit intermediate built from 64-bit limbs and are re-scaled
//! with a single word division (the divisor 10^DIGITS always fits in 64
//! bits), using [`div_2by1`] — the native `x86_64` `div` instruction with the
//! `asm` feature, or a fast portable long division otherwise.

use crate::AmountErrorKind;
use crate::Rounding;
use crate::ipow10_i128;
use crate::limbs::{
    cmp_twice_rem_u64, dec_div, dec_mul, div_knuth, div_rem_u128_pow10, div_words_by_word,
    mul_add_word, parse_decimal_mag_rounded, round_up_by_cmp, upow10,
};
use core::cmp::Ordering;
use core::ops::*;

/// Decimal fixed-point number backed by a 128-bit signed integer, with
/// `DIGITS` fractional decimal digits (`DIGITS <= 19`).
///
/// Same decimal semantics as [`Decimal`](crate::Decimal), with roughly twice
/// the range: about ±1.7 * 10^(38 - DIGITS).
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Decimal128<const DIGITS: u8>(pub i128);

/// Amount128 is a 128-bit integer based decimal type with 4 decimal digits precision.
pub type Amount128 = Decimal128<4>;
/// Rate128 is a 128-bit integer based decimal type with 8 decimal digits precision.
pub type Rate128 = Decimal128<8>;

/// Splits an i128 into a sign and a two-limb magnitude.
#[inline]
const fn i128_sign_mag(v: i128) -> (bool, [u64; 2]) {
    let m = v.unsigned_abs();
    (v < 0, [m as u64, (m >> 64) as u64])
}

/// Rebuilds an i128 from a sign and a two-limb magnitude with the top bit
/// clear (guaranteed by the shared cores' symmetric-range check).
#[inline]
const fn i128_from_sign_mag(neg: bool, mag: [u64; 2]) -> i128 {
    let v = ((mag[0] as u128) | ((mag[1] as u128) << 64)) as i128;
    if neg { v.wrapping_neg() } else { v }
}

/// Truncated remainder `a % b` (`b != 0`) on sign-magnitude limbs. A native
/// 128-bit `%` is a `__modti3` libcall on every architecture — no platform
/// has a 128-by-128 division primitive — so this goes through the crate's
/// word division ([`div_2by1`](crate::limbs::div_2by1) steps) instead, like
/// [`Decimal256`](crate::Decimal256)'s remainder.
fn i128_rem(a: i128, b: i128) -> i128 {
    let neg = a < 0;
    let am = a.unsigned_abs();
    let bm = b.unsigned_abs();
    let r = if bm >> 64 == 0 {
        let mut q = [am as u64, (am >> 64) as u64];
        div_words_by_word(&mut q, bm as u64) as u128
    } else if am < bm {
        am // the divisor is larger, so the dividend is the remainder
    } else {
        let mut q = [0u64];
        let mut r = [0u64; 2];
        div_knuth(
            &mut q,
            &mut r,
            &[am as u64, (am >> 64) as u64],
            &[bm as u64, (bm >> 64) as u64],
        );
        ((r[1] as u128) << 64) | r[0] as u128
    };
    // r < |b| <= 2^127 (and in the pass-through branch |a| < |b|), so the
    // remainder always fits an i128 with either sign.
    if neg { -(r as i128) } else { r as i128 }
}

impl<const DIGITS: u8> Decimal128<DIGITS> {
    /// The decimal scale: the number of fractional digits.
    pub const SCALE: i32 = DIGITS as i32;

    /// The multiplier used to scale values up, as a single 64-bit word.
    /// Constrains `DIGITS <= 19` at compile time.
    pub(crate) const SCALE_U64: u64 = upow10(DIGITS as u32);

    /// The multiplier used to scale values up. Equal to 10^DIGITS.
    pub const SCALE_INT: i128 = Self::SCALE_U64 as i128;

    /// Half of the scaling factor, used for rounding.
    pub const SCALE_INT_HALF: i128 = Self::SCALE_INT / 2;

    /// The largest value that can be represented by this type.
    pub const MAX: Self = Decimal128::<DIGITS>(i128::MAX);
    /// The smallest value that can be represented by this type.
    pub const MIN: Self = Decimal128::<DIGITS>(-i128::MAX); // make MIN symmetric

    /// Constant equal to '1'
    pub const ONE: Self = Decimal128::<DIGITS>(Self::SCALE_INT);
    /// Constant equal to '-1'
    pub const MINUS_ONE: Self = Decimal128::<DIGITS>(-Self::SCALE_INT);
    /// Constant equal to '0'
    pub const ZERO: Self = Decimal128::<DIGITS>(0);

    /// The smallest integer value that can be represented by this type.
    pub const INT_MIN: i128 = (i128::MIN + 1) / Self::SCALE_INT;
    /// The largest integer value that can be represented by this type.
    pub const INT_MAX: i128 = i128::MAX / Self::SCALE_INT;

    /// The multiplier used to scale values up, as f64.
    pub const SCALE_F64: f64 = Self::SCALE_INT as f64;

    /// The smallest f64 value that can be represented.
    pub const F64_MIN: f64 = (i128::MIN + 1) as f64 / Self::SCALE_F64;
    /// The largest f64 value that can be represented.
    pub const F64_MAX: f64 = i128::MAX as f64 / Self::SCALE_F64;

    /// Constructs a new decimal integer with value 0.
    ///
    /// # Examples
    /// ```rust
    /// use fin_decimal::Amount128;
    /// let i = Amount128::new();
    /// assert_eq!(i, Amount128::ZERO);
    /// ```
    #[inline]
    pub fn new() -> Self {
        Decimal128::<DIGITS>(0)
    }

    /// Tries to convert a f32 to a Decimal128.
    #[inline]
    pub fn from_f32(val: f32) -> Result<Self, AmountErrorKind> {
        Self::from_f64(val as f64)
    }

    /// Tries to convert a f64 to a Decimal128.
    #[inline]
    pub fn from_f64(val: f64) -> Result<Self, AmountErrorKind> {
        if (Self::F64_MIN..=Self::F64_MAX).contains(&val) {
            Ok(Decimal128::<DIGITS>((val * Self::SCALE_F64) as i128))
        } else {
            Err(AmountErrorKind::Overflow)
        }
    }

    /// Tries to convert an i128 to a Decimal128.
    #[inline]
    pub const fn from_i128(val: i128) -> Result<Self, AmountErrorKind> {
        if (val <= Self::INT_MAX) && (val >= Self::INT_MIN) {
            Ok(Decimal128::<DIGITS>(val * Self::SCALE_INT))
        } else {
            Err(AmountErrorKind::Overflow)
        }
    }

    /// Tries to convert an i64 to a Decimal128.
    #[inline]
    pub const fn from_i64(val: i64) -> Result<Self, AmountErrorKind> {
        Self::from_i128(val as i128)
    }

    /// Converts the Decimal128 back into an f64.
    #[inline]
    pub fn to_f64(self) -> f64 {
        self.0 as f64 / Self::SCALE_F64
    }

    /// Converts the Decimal128 back into an i128 (truncating the fractional part).
    #[inline]
    pub const fn to_i128(self) -> i128 {
        self.0 / Self::SCALE_INT
    }

    /// Returns the raw backing value as `i128`.
    ///
    /// This is the unscaled mantissa: the stored integer such that the value
    /// equals `mantissa * 10^(-SCALE)`. For this type the backing already is
    /// `i128`, so the conversion is free.
    ///
    /// # Examples
    /// ```
    /// use fin_decimal::Amount128;
    /// let a = Amount128::from(3); // SCALE = 4
    /// assert_eq!(a.mantissa(), 30000);
    /// ```
    #[inline]
    pub const fn mantissa(self) -> i128 {
        self.0
    }

    /// Decomposes the value into `(mantissa, exponent)` such that the value
    /// equals `mantissa * 10^exponent`. The exponent is always `-SCALE`.
    ///
    /// # Examples
    /// ```
    /// use fin_decimal::Amount128;
    /// let a = Amount128::from(3); // SCALE = 4
    /// assert_eq!(a.to_decimal_parts(), (30000, -4));
    /// ```
    #[inline]
    pub const fn to_decimal_parts(self) -> (i128, i32) {
        (self.0, -Self::SCALE)
    }

    /// Builds a value from `(mantissa, exponent)`, where the represented
    /// number is `mantissa * 10^exponent`.
    ///
    /// Same contract as [`Decimal::from_decimal_parts`]: scaling up is exact
    /// (or [`Overflow`](AmountErrorKind::Overflow)); scaling down is
    /// exact-or-error, dropping surplus trailing zeros exactly and returning
    /// [`Inexact`](AmountErrorKind::Inexact) for non-zero dropped digits.
    ///
    /// [`Decimal::from_decimal_parts`]: crate::Decimal::from_decimal_parts
    ///
    /// # Examples
    /// ```
    /// use fin_decimal::{Amount128, AmountErrorKind};
    /// assert_eq!(Amount128::from_decimal_parts(123, -2), Ok(Amount128::from_bits(12300)));
    /// let a = Amount128::from(7);
    /// let (m, e) = a.to_decimal_parts();
    /// assert_eq!(Amount128::from_decimal_parts(m, e), Ok(a));
    /// assert_eq!(Amount128::from_decimal_parts(1, -5), Err(AmountErrorKind::Inexact));
    /// ```
    pub const fn from_decimal_parts(
        mantissa: i128,
        exponent: i32,
    ) -> Result<Self, AmountErrorKind> {
        if mantissa == 0 {
            return Ok(Decimal128::<DIGITS>(0));
        }
        let shift = exponent as i64 + Self::SCALE as i64;
        let scaled = if shift >= 0 {
            // Scale up: mantissa * 10^shift.
            let factor = match ipow10_i128(shift) {
                Some(f) => f,
                None => return Err(AmountErrorKind::Overflow),
            };
            match mantissa.checked_mul(factor) {
                Some(v) => v,
                None => return Err(AmountErrorKind::Overflow),
            }
        } else {
            // Scale down: mantissa / 10^(-shift), but only if exact.
            let divisor = match ipow10_i128(-shift) {
                Some(d) => d,
                None => return Err(AmountErrorKind::Inexact),
            };
            if mantissa % divisor != 0 {
                return Err(AmountErrorKind::Inexact);
            }
            mantissa / divisor
        };
        Ok(Decimal128::<DIGITS>(scaled))
    }

    /// Builds a value from `(mantissa, exponent)`, rounding to this type's
    /// scale with the given [`Rounding`] mode when the input carries more
    /// fractional digits than can be held exactly. Never returns
    /// [`Inexact`](AmountErrorKind::Inexact); only
    /// [`Overflow`](AmountErrorKind::Overflow) can fail.
    ///
    /// # Examples
    /// ```
    /// use fin_decimal::{Amount128, Rounding};
    /// assert_eq!(
    ///     Amount128::from_decimal_parts_rounded(123456, -5, Rounding::HalfUp),
    ///     Ok(Amount128::from_bits(12346)),
    /// );
    /// ```
    pub const fn from_decimal_parts_rounded(
        mantissa: i128,
        exponent: i32,
        mode: Rounding,
    ) -> Result<Self, AmountErrorKind> {
        if mantissa == 0 {
            return Ok(Decimal128::<DIGITS>(0));
        }
        let shift = exponent as i64 + Self::SCALE as i64;
        let scaled = if shift >= 0 {
            let factor = match ipow10_i128(shift) {
                Some(f) => f,
                None => return Err(AmountErrorKind::Overflow),
            };
            match mantissa.checked_mul(factor) {
                Some(v) => v,
                None => return Err(AmountErrorKind::Overflow),
            }
        } else {
            let is_neg = mantissa < 0;
            let mag = mantissa.unsigned_abs();
            let divisor = match ipow10_i128(-shift) {
                Some(d) => d as u128,
                // |value| < 0.5 ULP: rounds to 0, except `Up`.
                None => {
                    let one = match mode {
                        Rounding::Up => {
                            if is_neg {
                                -1
                            } else {
                                1
                            }
                        }
                        _ => 0,
                    };
                    return Ok(Decimal128::<DIGITS>(one));
                }
            };
            let quo = mag / divisor;
            let rem = mag % divisor;
            // The divisor is a power of ten >= 10, hence always even.
            let half = divisor / 2;
            let round_up = match mode {
                Rounding::HalfEven => rem > half || (rem == half && !quo.is_multiple_of(2)),
                Rounding::HalfUp => rem >= half,
                Rounding::HalfDown => rem > half,
                Rounding::Down => false,
                Rounding::Up => rem != 0,
            };
            let q = if round_up { quo + 1 } else { quo };
            if q > i128::MAX as u128 {
                return Err(AmountErrorKind::Overflow);
            }
            if is_neg { -(q as i128) } else { q as i128 }
        };
        Ok(Decimal128::<DIGITS>(scaled))
    }

    /// Parses a decimal string, rounding any fractional digits beyond this
    /// type's scale with the given [`Rounding`] mode.
    ///
    /// # Examples
    /// ```
    /// use fin_decimal::{Amount128, Rounding};
    /// assert_eq!(
    ///     Amount128::from_str_rounded("1.23455", Rounding::HalfEven),
    ///     Ok(Amount128::from_bits(12346)),
    /// );
    /// ```
    pub const fn from_str_rounded(src: &str, mode: Rounding) -> Result<Self, AmountErrorKind> {
        let (neg, mag) = match parse_decimal_mag_rounded::<2>(src, DIGITS, mode) {
            Ok(v) => v,
            Err(e) => return Err(e),
        };
        if mag[1] >> 63 != 0 {
            return Err(AmountErrorKind::Overflow);
        }
        Ok(Decimal128::<DIGITS>(i128_from_sign_mag(neg, mag)))
    }

    /// Parses a decimal string with [`Rounding::HalfUp`], panicking on invalid
    /// input — intended for compile-time constants, where the panic becomes a
    /// compile error.
    ///
    /// # Examples
    /// ```
    /// use fin_decimal::Amount128;
    /// const FEE: Amount128 = Amount128::from_str_const("0.0035");
    /// assert_eq!(FEE, Amount128::from_bits(35));
    /// ```
    pub const fn from_str_const(src: &str) -> Self {
        match Self::from_str_rounded(src, Rounding::HalfUp) {
            Ok(v) => v,
            Err(_) => panic!("invalid decimal literal"),
        }
    }

    /// Sign and 4-limb magnitude (zero-extended), for the shared
    /// formatting code in [`common`](crate::common).
    #[inline]
    pub(crate) const fn sign_mag4(self) -> (bool, [u64; 4]) {
        let (neg, m) = i128_sign_mag(self.0);
        (neg, [m[0], m[1], 0, 0])
    }

    /// Computes the absolute value of self.
    #[inline]
    pub const fn abs(self) -> Self {
        Decimal128::<DIGITS>(self.0.abs())
    }

    /// Checked addition. Computes `self + rhs`, returning `None` if overflow occurred.
    #[inline]
    pub const fn checked_add(self, rhs: Self) -> Option<Self> {
        match self.0.checked_add(rhs.0) {
            Some(v) => Some(Decimal128::<DIGITS>(v)),
            None => None,
        }
    }

    /// Checked subtraction. Computes `self - rhs`, returning `None` if overflow occurred.
    #[inline]
    pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
        match self.0.checked_sub(rhs.0) {
            Some(v) => Some(Decimal128::<DIGITS>(v)),
            None => None,
        }
    }

    /// Checked multiplication. Computes `self * rhs`, returning `None` if overflow occurred.
    #[inline]
    pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
        let (an, am) = i128_sign_mag(self.0);
        let (bn, bm) = i128_sign_mag(rhs.0);
        match dec_mul::<DIGITS, 2, 4>(an, &am, bn, &bm, Rounding::HalfUp) {
            Some((neg, mag)) => Some(Decimal128::<DIGITS>(i128_from_sign_mag(neg, mag))),
            None => None,
        }
    }

    /// Checked division. Computes `self / rhs`, returning `None` if `rhs == 0`
    /// or the division results in overflow.
    #[inline]
    pub fn checked_div(self, rhs: Self) -> Option<Self> {
        let (an, am) = i128_sign_mag(self.0);
        let (bn, bm) = i128_sign_mag(rhs.0);
        dec_div::<DIGITS, 2, 3>(an, &am, bn, &bm, Rounding::HalfUp)
            .map(|(neg, mag)| Decimal128::<DIGITS>(i128_from_sign_mag(neg, mag)))
    }

    /// Takes the reciprocal (inverse) of a number, 1/x.
    #[inline]
    pub fn recip(self) -> Self {
        Self::ONE / self
    }

    /// Signed fractional remainder, `self.0 % 10^DIGITS`.
    ///
    /// Computed on the magnitude via [`div_rem_u128_pow10`] because a direct
    /// 128-bit `%` by a constant is not strength-reduced by the compiler (it
    /// lowers to a `__umodti3` call).
    #[inline]
    const fn frac_rem(self) -> i128 {
        let (_, r) = div_rem_u128_pow10::<DIGITS>(self.0.unsigned_abs());
        if self.0 < 0 { -(r as i128) } else { r as i128 }
    }

    /// Returns the integer part of a number.
    ///
    /// # Examples
    /// ```
    /// use fin_decimal::Amount128;
    /// assert_eq!(Amount128::from_f64(3.7).unwrap().trunc(), Amount128::from(3));
    /// assert_eq!(Amount128::from_f64(-3.7).unwrap().trunc(), Amount128::from(-3));
    /// ```
    #[inline]
    pub const fn trunc(self) -> Self {
        Decimal128::<DIGITS>(self.0 - self.frac_rem())
    }

    /// Returns the largest integer less than or equal to a number.
    ///
    /// # Examples
    /// ```
    /// use fin_decimal::Amount128;
    /// assert_eq!(Amount128::from_f64(-3.7).unwrap().floor(), Amount128::from(-4));
    /// ```
    pub const fn floor(self) -> Self {
        let frac = self.frac_rem();
        if self.0 < 0 && frac != 0 {
            Decimal128::<DIGITS>(self.0 - frac - Self::SCALE_INT)
        } else {
            Decimal128::<DIGITS>(self.0 - frac)
        }
    }

    /// Returns the smallest integer greater than or equal to a number.
    ///
    /// # Examples
    /// ```
    /// use fin_decimal::Amount128;
    /// assert_eq!(Amount128::from_f64(3.01).unwrap().ceil(), Amount128::from(4));
    /// ```
    pub const fn ceil(self) -> Self {
        let mut frac = self.frac_rem();
        if frac != 0 {
            if self.0 < 0 {
                frac += Self::SCALE_INT
            } else {
                frac -= Self::SCALE_INT
            }
            Decimal128::<DIGITS>(self.0 - frac)
        } else {
            self
        }
    }

    /// Returns the nearest integer to a number. Rounds half-way cases away
    /// from `0.0`.
    ///
    /// # Examples
    /// ```
    /// use fin_decimal::Amount128;
    /// assert_eq!(Amount128::from_f64(3.5).unwrap().round(), Amount128::from(4));
    /// assert_eq!(Amount128::from_f64(-3.5).unwrap().round(), Amount128::from(-4));
    /// ```
    pub const fn round(self) -> Self {
        let mut frac = self.frac_rem();
        if frac >= Self::SCALE_INT_HALF {
            frac -= Self::SCALE_INT
        } else if frac <= -Self::SCALE_INT_HALF {
            frac += Self::SCALE_INT
        }
        Decimal128::<DIGITS>(self.0 - frac)
    }

    /// Explicitly rounds the value to an integer using the specified rounding mode.
    ///
    /// # Panics
    /// Panics if rounding up at the very edge of the range overflows.
    pub const fn round_to(self, mode: Rounding) -> Self {
        // Magnitude-based, like the other rounding paths: a direct 128-bit
        // `%` by a constant would lower to a `__umodti3` call.
        let neg = self.0 < 0;
        let (q, rem) = div_rem_u128_pow10::<DIGITS>(self.0.unsigned_abs());
        if rem == 0 {
            return self;
        }
        let up = round_up_by_cmp(
            cmp_twice_rem_u64(rem, const { upow10(DIGITS as u32) }),
            false,
            q & 1 != 0,
            neg,
            mode,
        );
        // Rebuild (q + up) * 10^DIGITS; q * 10^DIGITS <= |self.0|, so the
        // u128 arithmetic cannot overflow.
        let scaled = (q + up as u128) * const { upow10(DIGITS as u32) } as u128;
        if scaled > i128::MAX as u128 {
            panic!("attempt to round with overflow");
        }
        let v = scaled as i128;
        Decimal128::<DIGITS>(if neg { v.wrapping_neg() } else { v })
    }

    /// Multiply by another decimal, explicitly applying the given rounding mode.
    ///
    /// The right-hand side may have a different scale: the exact product is
    /// rounded once to `self`'s scale, so an amount times a high-precision
    /// rate needs no intermediate conversion.
    ///
    /// Usable in const contexts, so multiplication chains can be evaluated at
    /// compile time.
    ///
    /// # Panics
    /// Panics if the result overflows (like core integer arithmetic in debug
    /// builds); [`checked_mul_rounded`](Self::checked_mul_rounded) is the
    /// non-panicking form.
    pub const fn mul_rounded<const RHS_DIGITS: u8>(
        self,
        rhs: Decimal128<RHS_DIGITS>,
        mode: Rounding,
    ) -> Self {
        match self.checked_mul_rounded(rhs, mode) {
            Some(v) => v,
            None => panic!("attempt to multiply with overflow"),
        }
    }

    /// Checked form of [`mul_rounded`](Self::mul_rounded): `None` if the
    /// result overflows.
    #[inline]
    pub const fn checked_mul_rounded<const RHS_DIGITS: u8>(
        self,
        rhs: Decimal128<RHS_DIGITS>,
        mode: Rounding,
    ) -> Option<Self> {
        let (an, am) = i128_sign_mag(self.0);
        let (bn, bm) = i128_sign_mag(rhs.0);
        match dec_mul::<RHS_DIGITS, 2, 4>(an, &am, bn, &bm, mode) {
            Some((neg, mag)) => Some(Decimal128::<DIGITS>(i128_from_sign_mag(neg, mag))),
            None => None,
        }
    }

    /// Divide by another decimal, explicitly applying the given rounding mode.
    ///
    /// # Panics
    /// Panics if `rhs` is zero (like core's `/`) or the result overflows;
    /// [`checked_div_rounded`](Self::checked_div_rounded) is the
    /// non-panicking form.
    pub fn div_rounded(self, rhs: Self, mode: Rounding) -> Self {
        self.div_rounded_to::<DIGITS>(rhs, mode)
    }

    /// Checked form of [`div_rounded`](Self::div_rounded): `None` if `rhs`
    /// is zero or the result overflows.
    #[inline]
    pub fn checked_div_rounded(self, rhs: Self, mode: Rounding) -> Option<Self> {
        self.checked_div_rounded_to::<DIGITS>(rhs, mode)
    }

    /// Divide by another decimal of the same scale, producing the quotient at
    /// an explicitly chosen scale with the given rounding mode. The exact
    /// quotient is rounded once at `TO_DIGITS` digits, so a proportion of two
    /// amounts can be taken directly as a higher-precision rate.
    ///
    /// # Panics
    /// Panics if `rhs` is zero (like core's `/`) or the result overflows;
    /// [`checked_div_rounded_to`](Self::checked_div_rounded_to) is the
    /// non-panicking form.
    pub fn div_rounded_to<const TO_DIGITS: u8>(
        self,
        rhs: Self,
        mode: Rounding,
    ) -> Decimal128<TO_DIGITS> {
        if rhs.0 == 0 {
            panic!("attempt to divide by zero");
        }
        match self.checked_div_rounded_to::<TO_DIGITS>(rhs, mode) {
            Some(v) => v,
            None => panic!("attempt to divide with overflow"),
        }
    }

    /// Checked form of [`div_rounded_to`](Self::div_rounded_to): `None` if
    /// `rhs` is zero or the result overflows.
    #[inline]
    pub fn checked_div_rounded_to<const TO_DIGITS: u8>(
        self,
        rhs: Self,
        mode: Rounding,
    ) -> Option<Decimal128<TO_DIGITS>> {
        let (an, am) = i128_sign_mag(self.0);
        let (bn, bm) = i128_sign_mag(rhs.0);
        dec_div::<TO_DIGITS, 2, 3>(an, &am, bn, &bm, mode)
            .map(|(neg, mag)| Decimal128::<TO_DIGITS>(i128_from_sign_mag(neg, mag)))
    }

    /// Divide by an integer, explicitly applying the given rounding mode: the
    /// exact quotient is rounded once at the type's own scale. Equivalent to
    /// `div_rounded` by `Decimal128::from(n)` but skips re-scaling the
    /// dividend: the divisor is a single word, so this is plain word division
    /// (a native `i128 / i64` would lower to a `__udivti3` call).
    ///
    /// # Panics
    /// Panics if `n` is zero (like core's `/`);
    /// [`checked_div_int_rounded`](Self::checked_div_int_rounded) is the
    /// non-panicking form. The result itself cannot overflow: its magnitude
    /// never exceeds `self`'s (rounding up only happens for `|n| >= 2`).
    pub fn div_int_rounded(self, n: i64, mode: Rounding) -> Self {
        match self.checked_div_int_rounded(n, mode) {
            Some(v) => v,
            None => panic!("attempt to divide by zero"),
        }
    }

    /// Checked form of [`div_int_rounded`](Self::div_int_rounded): `None`
    /// only if `n` is zero (the result cannot overflow).
    #[inline]
    pub fn checked_div_int_rounded(self, n: i64, mode: Rounding) -> Option<Self> {
        if n == 0 {
            return None;
        }
        let (an, mut mag) = i128_sign_mag(self.0);
        let neg = an != (n < 0);
        let d = n.unsigned_abs();
        let r = div_words_by_word(&mut mag, d);
        if round_up_by_cmp(cmp_twice_rem_u64(r, d), r == 0, mag[0] & 1 != 0, neg, mode) {
            mul_add_word(&mut mag, 1, 1);
        }
        Some(Decimal128::<DIGITS>(i128_from_sign_mag(neg, mag)))
    }

    /// Returns the fractional part of a number.
    #[inline]
    pub const fn fract(self) -> Self {
        // Via the magnitude reciprocal path: a direct `%` by the scale
        // constant would lower to a `__umodti3` call.
        Decimal128::<DIGITS>(self.frac_rem())
    }

    /// Returns `true` if `self` is positive and `false` if the number is zero or negative.
    #[inline]
    pub const fn is_positive(self) -> bool {
        self.0 > 0
    }

    /// Returns `true` if `self` is negative and `false` if the number is zero or positive.
    #[inline]
    pub const fn is_negative(self) -> bool {
        self.0 < 0
    }

    /// Returns a number that represents the sign of `self`.
    pub const fn signum(self) -> Self {
        match (self.0 < 0, self.0 > 0) {
            (true, _) => Self::MINUS_ONE,
            (false, false) => Self::ZERO,
            (_, _) => Self::ONE,
        }
    }

    /// Raw transmutation to the backing scaled integer.
    #[inline]
    pub const fn to_bits(self) -> i128 {
        self.0
    }

    /// Raw transmutation from a backing scaled integer.
    ///
    /// # Examples
    /// ```
    /// use fin_decimal::Amount128;
    /// assert_eq!(Amount128::from_bits(25000), Amount128::from_f64(2.5).unwrap());
    /// ```
    #[inline]
    pub const fn from_bits(v: i128) -> Self {
        Decimal128::<DIGITS>(v)
    }

    /// Return the memory representation of this value as a byte array in
    /// big-endian (network) byte order.
    #[inline]
    pub const fn to_be_bytes(self) -> [u8; 16] {
        self.0.to_be_bytes()
    }

    /// Return the memory representation of this value as a byte array in
    /// little-endian byte order.
    #[inline]
    pub const fn to_le_bytes(self) -> [u8; 16] {
        self.0.to_le_bytes()
    }

    /// Create a value from its representation as a byte array in big endian.
    #[inline]
    pub const fn from_be_bytes(bytes: [u8; 16]) -> Self {
        Decimal128::<DIGITS>(i128::from_be_bytes(bytes))
    }

    /// Create a value from its representation as a byte array in little endian.
    #[inline]
    pub const fn from_le_bytes(bytes: [u8; 16]) -> Self {
        Decimal128::<DIGITS>(i128::from_le_bytes(bytes))
    }

    /// Raises a number to an integer power. Usable in const contexts.
    pub const fn powi(self, mut exp: u32) -> Self {
        let mut base = self;
        let mut acc = Self::ONE;
        while exp > 1 {
            if (exp & 1) == 1 {
                acc = base.mul_rounded(acc, Rounding::HalfUp);
            }
            exp /= 2;
            base = base.mul_rounded(base, Rounding::HalfUp);
        }
        if exp == 1 {
            acc = base.mul_rounded(acc, Rounding::HalfUp);
        }
        acc
    }

    /// Restrict a value to a certain interval.
    #[inline]
    pub fn clamp(self, min: Self, max: Self) -> Self {
        if self.0 < min.0 {
            min
        } else if self.0 > max.0 {
            max
        } else {
            self
        }
    }

    /// Returns the minimum of the two numbers.
    #[inline]
    pub fn min(self, other: Self) -> Self {
        if self <= other { self } else { other }
    }

    /// Returns the maximum of the two numbers.
    #[inline]
    pub fn max(self, other: Self) -> Self {
        if self >= other { self } else { other }
    }
}

impl<const DIGITS: u8> From<i32> for Decimal128<DIGITS> {
    #[inline]
    fn from(item: i32) -> Self {
        Decimal128::<DIGITS>(item as i128 * Self::SCALE_INT)
    }
}

impl<const DIGITS: u8> From<i64> for Decimal128<DIGITS> {
    #[inline]
    fn from(item: i64) -> Self {
        Decimal128::<DIGITS>(item as i128 * Self::SCALE_INT)
    }
}

impl<const DIGITS: u8> From<i128> for Decimal128<DIGITS> {
    /// Converts an i128, saturating to `MIN`/`MAX` when out of range.
    #[inline]
    fn from(item: i128) -> Self {
        match Self::from_i128(item) {
            Ok(v) => v,
            Err(_) if item < 0 => Self::MIN,
            Err(_) => Self::MAX,
        }
    }
}

impl<const DIGITS: u8> From<f64> for Decimal128<DIGITS> {
    /// Converts an f64, saturating to `MIN`/`MAX` when out of range.
    #[inline]
    fn from(item: f64) -> Self {
        if (item < Self::F64_MAX) && (item > Self::F64_MIN) {
            Decimal128::<DIGITS>((item * Self::SCALE_F64) as i128)
        } else if item < Self::F64_MIN {
            Self::MIN
        } else {
            Self::MAX
        }
    }
}

impl<const DIGITS: u8> From<f32> for Decimal128<DIGITS> {
    #[inline]
    fn from(item: f32) -> Self {
        Self::from(item as f64)
    }
}

impl<const DIGITS: u8> PartialOrd<i64> for Decimal128<DIGITS> {
    #[inline]
    fn partial_cmp(&self, other: &i64) -> Option<Ordering> {
        PartialOrd::partial_cmp(&self.0, &(*other as i128 * Self::SCALE_INT))
    }
}

impl<const DIGITS: u8> PartialEq<i64> for Decimal128<DIGITS> {
    #[inline]
    fn eq(&self, other: &i64) -> bool {
        self.0 == *other as i128 * Self::SCALE_INT
    }
}

impl<const DIGITS: u8> Neg for Decimal128<DIGITS> {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self::Output {
        Decimal128::<DIGITS>(-self.0)
    }
}

impl<const DIGITS: u8> Add for Decimal128<DIGITS> {
    type Output = Self;
    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        Decimal128::<DIGITS>(self.0 + rhs.0)
    }
}

impl<const DIGITS: u8> Add<i64> for Decimal128<DIGITS> {
    type Output = Self;
    #[inline]
    fn add(self, other: i64) -> Self {
        Decimal128::<DIGITS>(self.0 + other as i128 * Self::SCALE_INT)
    }
}

impl<const DIGITS: u8> Add<i32> for Decimal128<DIGITS> {
    type Output = Self;
    #[inline]
    fn add(self, rhs: i32) -> Self {
        Decimal128::<DIGITS>(self.0 + (rhs as i128) * Self::SCALE_INT)
    }
}

impl<const DIGITS: u8> Sub for Decimal128<DIGITS> {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: Self) -> Self {
        Decimal128::<DIGITS>(self.0 - rhs.0)
    }
}

impl<const DIGITS: u8> Sub<i64> for Decimal128<DIGITS> {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: i64) -> Self {
        Decimal128::<DIGITS>(self.0 - rhs as i128 * Self::SCALE_INT)
    }
}

impl<const DIGITS: u8> Sub<i32> for Decimal128<DIGITS> {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: i32) -> Self {
        Decimal128::<DIGITS>(self.0 - (rhs as i128) * Self::SCALE_INT)
    }
}

impl<const DIGITS: u8> Mul for Decimal128<DIGITS> {
    type Output = Self;
    /// Multiplies with decimal `HalfUp` rounding (half away from zero).
    ///
    /// # Panics
    /// Panics if the result overflows.
    #[inline]
    fn mul(self, rhs: Self) -> Self::Output {
        self.mul_rounded(rhs, Rounding::HalfUp)
    }
}

impl<const DIGITS: u8> Mul<i64> for Decimal128<DIGITS> {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: i64) -> Self {
        Decimal128::<DIGITS>(self.0 * rhs as i128)
    }
}

impl<const DIGITS: u8> Mul<i32> for Decimal128<DIGITS> {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: i32) -> Self {
        Decimal128::<DIGITS>(self.0 * (rhs as i128))
    }
}

impl<const DIGITS: u8> Div for Decimal128<DIGITS> {
    type Output = Self;
    /// Divides with decimal `HalfUp` rounding (half away from zero).
    ///
    /// # Panics
    /// Panics if `rhs` is zero or the result overflows.
    #[inline]
    fn div(self, rhs: Self) -> Self {
        self.div_rounded(rhs, Rounding::HalfUp)
    }
}

impl<const DIGITS: u8> Rem for Decimal128<DIGITS> {
    type Output = Self;
    /// # Panics
    /// Panics if `rhs` is zero.
    #[inline]
    fn rem(self, rhs: Self) -> Self {
        if rhs.0 == 0 {
            panic!("attempt to calculate the remainder with a divisor of zero");
        }
        Decimal128::<DIGITS>(i128_rem(self.0, rhs.0))
    }
}

crate::common::impl_decimal_common!(Decimal128, "Decimal128");

#[cfg(test)]
mod tests {
    extern crate std;
    use super::*;
    use core::str::FromStr;
    use std::format;
    use std::string::ToString;

    #[test]
    fn test_basic_ops() {
        let a = Amount128::from(2);
        let b = Amount128::from_f64(0.5).unwrap();
        assert_eq!(a.0, 20000);
        assert_eq!(b.0, 5000);
        assert_eq!((a + b).0, 25000);
        assert_eq!((a - b).0, 15000);
        assert_eq!((a * b).0, 10000);
        assert_eq!((a / b).0, 40000);
        assert_eq!((-a).0, -20000);
        assert_eq!((a % b).0, 0);
    }

    #[test]
    fn test_mul_rounding_matches_decimal64() {
        // Mirror Decimal<4> test vectors.
        assert_eq!(
            Decimal128::<4>(10001) * Decimal128::<4>(10001),
            Decimal128::<4>(10002)
        );
        assert_eq!(
            Decimal128::<4>(11004) * Decimal128::<4>(10015),
            Decimal128::<4>(11021)
        );
        assert_eq!(
            Decimal128::<4>(11004) * Decimal128::<4>(-10015),
            Decimal128::<4>(-11021)
        );
    }

    #[test]
    fn test_div_rounding_matches_decimal64() {
        assert_eq!(
            Decimal128::<4>(10000) / Decimal128::<4>(110000),
            Decimal128::<4>(909)
        );
        assert_eq!(
            Decimal128::<4>(10000) / Decimal128::<4>(130000),
            Decimal128::<4>(769)
        );
        assert_eq!(
            Decimal128::<4>(10000) / Decimal128::<4>(-130000),
            Decimal128::<4>(-769)
        );
        assert_eq!(
            Decimal128::<4>(-10000) / Decimal128::<4>(130000),
            Decimal128::<4>(-769)
        );
        assert_eq!(
            Decimal128::<4>(-10000) / Decimal128::<4>(-130000),
            Decimal128::<4>(769)
        );
        assert_eq!(
            Decimal128::<4>(10000) / Decimal128::<4>(180000),
            Decimal128::<4>(556)
        );
        assert_eq!(
            Decimal128::<4>(-10000) / Decimal128::<4>(180000),
            Decimal128::<4>(-556)
        );
    }

    #[test]
    fn test_beyond_64_bit_range() {
        // Values that overflow Amount64 are exact here.
        let big = Amount128::from(10_000_000_000_000_000i64); // 10^16
        assert_eq!(big.0, 100_000_000_000_000_000_000i128);
        assert_eq!((big * big).0, 10i128.pow(36));
        assert_eq!(
            format!("{}", big * big),
            "100000000000000000000000000000000"
        );
        // Division with a two-limb divisor.
        let one = Amount128::from(1);
        let r = one / big;
        assert_eq!(r.0, 0); // 10^-16 rounds to 0 at 4 digits
        let r = big / Amount128::from(3);
        assert_eq!(r.0, 33_333_333_333_333_333_333i128);
    }

    #[test]
    fn test_checked_math() {
        let max = Amount128::MAX;
        assert_eq!(max.checked_add(Amount128::from(1)), None);
        assert_eq!(max.checked_mul(Amount128::from(2)), None);
        assert_eq!(Amount128::from(2).checked_div(Amount128::ZERO), None);
        assert_eq!(
            Amount128::from(6).checked_div(Amount128::from(2)),
            Some(Amount128::from(3))
        );
        assert_eq!(
            Amount128::from(2).checked_mul(Amount128::from(3)),
            Some(Amount128::from(6))
        );
    }

    #[test]
    fn test_rounding_modes() {
        let a = Amount128::from_f64(1.5).unwrap();
        assert_eq!(a.round_to(Rounding::HalfUp), Amount128::from(2));
        assert_eq!(a.round_to(Rounding::HalfDown), Amount128::from(1));
        assert_eq!(a.round_to(Rounding::HalfEven), Amount128::from(2));
        assert_eq!(a.round_to(Rounding::Down), Amount128::from(1));
        assert_eq!(a.round_to(Rounding::Up), Amount128::from(2));

        // mul_rounded ties: 0.5 * 1.0001 = 0.50005
        let h = Amount128::from_f64(0.5).unwrap();
        let x = Amount128::from_bits(10001);
        assert_eq!(h.mul_rounded(x, Rounding::HalfUp).0, 5001);
        assert_eq!(h.mul_rounded(x, Rounding::HalfDown).0, 5000);
        assert_eq!(h.mul_rounded(x, Rounding::HalfEven).0, 5000);
        assert_eq!(h.mul_rounded(x, Rounding::Down).0, 5000);
        assert_eq!(h.mul_rounded(x, Rounding::Up).0, 5001);
        // Negative: floor/ceil are directional.
        assert_eq!((-h).mul_rounded(x, Rounding::Down).0, -5001);
        assert_eq!((-h).mul_rounded(x, Rounding::Up).0, -5000);
        assert_eq!((-h).mul_rounded(x, Rounding::HalfUp).0, -5001);
    }

    #[test]
    fn test_div_rounded_modes() {
        let one = Amount128::from(1);
        let three = Amount128::from(3);
        assert_eq!(one.div_rounded(three, Rounding::Down).0, 3333);
        assert_eq!(one.div_rounded(three, Rounding::Up).0, 3334);
        assert_eq!((-one).div_rounded(three, Rounding::Down).0, -3334);
        assert_eq!((-one).div_rounded(three, Rounding::Up).0, -3333);
        assert_eq!(one.div_rounded(three, Rounding::HalfUp).0, 3333);
    }

    #[test]
    fn test_from_str_and_display() {
        assert_eq!(Amount128::from_str("1.0001").unwrap().0, 10001);
        assert_eq!(Amount128::from_str("-1.0001").unwrap().0, -10001);
        assert_eq!(Amount128::from_str("1.00005").unwrap().0, 10001);
        assert_eq!(
            Amount128::from_str_rounded("1.00005", Rounding::HalfEven)
                .unwrap()
                .0,
            10000
        );
        assert_eq!(Amount128::from_str(""), Err(AmountErrorKind::Empty));
        assert_eq!(
            Amount128::from_str("1.2.3"),
            Err(AmountErrorKind::InvalidDigit)
        );

        // Max value round-trips through Display/FromStr.
        let s = Amount128::MAX.to_string();
        assert_eq!(s, "17014118346046923173168730371588410.5727");
        assert_eq!(Amount128::from_str(&s).unwrap(), Amount128::MAX);
        let s = Amount128::MIN.to_string();
        assert_eq!(Amount128::from_str(&s).unwrap(), Amount128::MIN);
        // Just past the range.
        assert_eq!(
            Amount128::from_str("17014118346046923173168730371588410.5728"),
            Err(AmountErrorKind::Overflow)
        );

        assert_eq!(&format!("{}", Decimal128::<4>(10000)), "1");
        assert_eq!(&format!("{:+}", Decimal128::<4>(10000)), "+1");
        assert_eq!(&format!("{:4.2}", Decimal128::<4>(10000)), "1.00");
        assert_eq!(&format!("{:03}", Decimal128::<4>(10000)), "001");
        assert_eq!(&format!("{}", Decimal128::<4>(1)), "0.0001");
        assert_eq!(&format!("{}", Decimal128::<4>(-10001)), "-1.0001");
        assert_eq!(&format!("{}", Decimal128::<4>(0)), "0");
    }

    #[test]
    fn test_decimal_parts() {
        let a = Amount128::from(3);
        assert_eq!(a.mantissa(), 30000);
        assert_eq!(a.to_decimal_parts(), (30000, -4));
        for raw in [0i128, 1, -1, 12345, i128::MAX, -i128::MAX] {
            let d = Decimal128::<4>(raw);
            let (m, e) = d.to_decimal_parts();
            assert_eq!(Amount128::from_decimal_parts(m, e), Ok(d));
        }
        assert_eq!(
            Amount128::from_decimal_parts(1, -5),
            Err(AmountErrorKind::Inexact)
        );
        assert_eq!(
            Amount128::from_decimal_parts(12300, -5),
            Ok(Decimal128::<4>(1230))
        );
        assert_eq!(
            Amount128::from_decimal_parts_rounded(123456, -5, Rounding::HalfUp),
            Ok(Decimal128::<4>(12346))
        );
        assert_eq!(
            Amount128::from_decimal_parts(i128::MAX, 1),
            Err(AmountErrorKind::Overflow)
        );
    }

    #[test]
    fn test_misc() {
        assert_eq!(
            Amount128::from_f64(3.7).unwrap().trunc(),
            Amount128::from(3)
        );
        assert_eq!(
            Amount128::from_f64(-3.7).unwrap().floor(),
            Amount128::from(-4)
        );
        assert_eq!(Amount128::from_f64(3.2).unwrap().ceil(), Amount128::from(4));
        assert_eq!(
            Amount128::from_f64(-3.5).unwrap().round(),
            Amount128::from(-4)
        );
        assert_eq!(Amount128::from(10).signum(), Amount128::ONE);
        assert_eq!(Amount128::from(-10).signum(), Amount128::MINUS_ONE);
        assert_eq!(Amount128::from(0).signum(), Amount128::ZERO);
        assert_eq!(Amount128::from(2).recip().0, 5000);
        assert_eq!(Amount128::from(2).powi(10), Amount128::from(1024));
        assert_eq!(
            Amount128::from(5).clamp(Amount128::ZERO, Amount128::ONE),
            Amount128::ONE
        );
        assert!(Amount128::from(1) == 1i64);
        assert!(Amount128::from(2) > 1i64);
        let v: Amount128 = [1, 2, 3].iter().map(|&x| Amount128::from(x)).sum();
        assert_eq!(v, Amount128::from(6));
        let bytes = Amount128::from(1).to_le_bytes();
        assert_eq!(Amount128::from_le_bytes(bytes), Amount128::from(1));
        assert_eq!(Rate128::from_f64(1.12345678).unwrap().0, 112345678);
        assert_eq!(
            &format!("{}", Rate128::from_f64(1.12345678).unwrap()),
            "1.12345678"
        );
    }

    #[test]
    fn test_const_eval() {
        const A: Amount128 = Amount128::from_str_const("123456789012345.6789");
        const B: Amount128 = A.mul_rounded(A, Rounding::HalfUp);
        const C: Amount128 = A.round_to(Rounding::HalfEven);
        const D: Amount128 = A.trunc();
        const E: Amount128 = Amount128::from_str_const("1.01").powi(12);
        const F: Option<Amount128> = A.checked_mul(A);
        const G: Amount128 = A.fract();

        let a = Amount128::from_str("123456789012345.6789").unwrap();
        assert_eq!(A, a);
        assert_eq!(B, a.mul_rounded(a, Rounding::HalfUp));
        assert_eq!(C, a.round_to(Rounding::HalfEven));
        assert_eq!(D, a.trunc());
        assert_eq!(E, Amount128::from_str("1.01").unwrap().powi(12));
        assert_eq!(F, a.checked_mul(a));
        assert_eq!(G, a.fract());
        assert_eq!(G.0, 6789);
    }

    /// The limb-based remainder must agree with the compiler's native i128
    /// `%` across one- and two-limb divisors and all sign combinations.
    #[test]
    fn test_rem_wide() {
        let mut state = 0x9E3779B97F4A7C15u64;
        let mut next = move || {
            state ^= state >> 12;
            state ^= state << 25;
            state ^= state >> 27;
            state.wrapping_mul(0x2545F4914F6CDD1D)
        };

        // A signed value whose magnitude spans a random number of bits.
        let mut rnd = move || {
            let sh = (next() as u32) % 128;
            let neg = next() & 1 != 0;
            let m = ((((next() as u128) << 64) | next() as u128) >> sh) as i128;
            if neg { m.wrapping_neg() } else { m }
        };
        for _ in 0..20000 {
            let a = rnd();
            let b = rnd();
            if b == 0 {
                continue;
            }
            assert_eq!(
                (Decimal128::<4>(a) % Decimal128::<4>(b)).0,
                a % b,
                "{a} % {b}"
            );
        }

        // Extremes: i128::MIN on either side, and |a| < |b|.
        assert_eq!(
            (Decimal128::<4>(i128::MIN) % Decimal128::<4>(3)).0,
            i128::MIN % 3
        );
        assert_eq!(
            (Decimal128::<4>(i128::MIN) % Decimal128::<4>(i128::MIN)).0,
            0
        );
        assert_eq!((Decimal128::<4>(5) % Decimal128::<4>(i128::MIN)).0, 5);
        assert_eq!((Decimal128::<4>(-5) % Decimal128::<4>(i128::MIN)).0, -5);
        assert_eq!((Decimal128::<4>(-5) % Decimal128::<4>(5)).0, 0);
    }

    #[test]
    #[should_panic(expected = "divisor of zero")]
    fn test_rem_by_zero() {
        let _ = Amount128::from(1) % Amount128::ZERO;
    }

    /// Differential test: for values within the i64 range, Decimal128 must
    /// agree with Decimal (the i64-backed type) on every operation and
    /// rounding mode.
    #[test]
    fn test_differential_vs_decimal64() {
        use crate::Decimal;
        use crate::Rounding::*;

        let mut state = 0x853C49E6748FEA9Bu64;
        let mut next = move || {
            state ^= state >> 12;
            state ^= state << 25;
            state ^= state >> 27;
            state.wrapping_mul(0x2545F4914F6CDD1D)
        };

        for _ in 0..20000 {
            // Keep magnitudes below ~2^30 so i64 multiplication cannot overflow.
            let a = (next() as i64) >> 34;
            let b = (next() as i64) >> 34;
            let da = Decimal::<4>(a);
            let db = Decimal::<4>(b);
            let wa = Decimal128::<4>(a as i128);
            let wb = Decimal128::<4>(b as i128);

            assert_eq!((da + db).0 as i128, (wa + wb).0);
            assert_eq!((da - db).0 as i128, (wa - wb).0);
            assert_eq!((da * db).0 as i128, (wa * wb).0, "mul {a} * {b}");
            assert_eq!(format!("{da}"), format!("{wa}"));
            assert_eq!(format!("{da:.2}"), format!("{wa:.2}"));

            // Cross-scale operands (an 8-digit rate) for mul_rounded.
            let rb = Decimal::<8>(b);
            let wrb = Decimal128::<8>(b as i128);
            for mode in [HalfUp, HalfDown, HalfEven, Down, Up] {
                assert_eq!(
                    da.mul_rounded(db, mode).0 as i128,
                    wa.mul_rounded(wb, mode).0,
                    "mul_rounded {a} * {b}"
                );
                assert_eq!(
                    da.mul_rounded(rb, mode).0 as i128,
                    wa.mul_rounded(wrb, mode).0,
                    "mul_rounded cross-scale {a} * {b}"
                );
                assert_eq!(
                    da.round_to(mode).0 as i128,
                    wa.round_to(mode).0,
                    "round_to {a}"
                );
                if b != 0 {
                    assert_eq!(
                        da.div_rounded(db, mode).0 as i128,
                        wa.div_rounded(wb, mode).0,
                        "div_rounded {a} / {b}"
                    );
                    assert_eq!(
                        da.div_rounded_to::<8>(db, mode).0 as i128,
                        wa.div_rounded_to::<8>(wb, mode).0,
                        "div_rounded_to::<8> {a} / {b}"
                    );
                    assert_eq!(
                        da.div_int_rounded(b, mode).0 as i128,
                        wa.div_int_rounded(b, mode).0,
                        "div_int_rounded {a} / {b}"
                    );
                }
            }
            if b != 0 {
                assert_eq!((da / db).0 as i128, (wa / wb).0, "div {a} / {b}");
                assert_eq!((da % db).0 as i128, (wa % wb).0, "rem {a} % {b}");
                assert_eq!(
                    da.checked_div(db).map(|v| v.0 as i128),
                    wa.checked_div(wb).map(|v| v.0),
                    "checked_div {a} / {b}"
                );
            }
        }
    }
}