ordecimal 0.1.0

Order-preserving binary encoding for arbitrary-precision decimals (decimalInfinite)
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
use crate::decoder::{decode_to_parts, DecodedDecimal, DecodedValue};
use crate::encoder::{encode_from_parts, encode_special_byte};
use crate::error::{DecodeError, EncodeError, EncodeResult};
use std::cmp::Ordering;
use std::fmt;
use std::fmt::Write as _;
use std::str::FromStr;

/// Special decimal values
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpecialValue {
    NegativeInfinity,
    NegativeZero,
    PositiveZero,
    PositiveInfinity,
    NaN,
}

/// Represents a decimal number in pre-encoded STEM format
///
/// This struct stores the decimal as encoded bytes, providing:
/// - Zero-copy access via `as_bytes()`
/// - Direct byte comparison for Ord (order-preserving)
/// - Smaller memory footprint than storing semantic fields
///
/// To access semantic fields (sign, exponent, significand), use `decode()`.
///
/// # NaN semantics
///
/// Unlike IEEE 754 floating-point, [`Decimal`] treats NaN as a concrete value:
/// - `NaN == NaN` is **true** (reflexive equality)
/// - NaN has a defined sort position: it is **greater than** all other values,
///   including positive infinity
///
/// This is intentional for use as database sort keys, where
/// every value must have a deterministic, total ordering. Code that expects
/// IEEE 754 NaN behavior (`NaN != NaN`, unordered comparisons) should not
/// rely on [`Decimal`]'s [`Eq`] and [`Ord`] implementations for NaN values.
#[derive(Debug, Clone)]
pub struct Decimal {
    bytes: Vec<u8>,
}

impl Decimal {
    /// Create from pre-encoded bytes without validation
    #[must_use]
    pub const fn from_bytes_unchecked(bytes: Vec<u8>) -> Self {
        Self { bytes }
    }

    /// Create from pre-encoded bytes with validation
    ///
    /// # Errors
    ///
    /// Returns [`DecodeError`] if the bytes do not represent a valid encoding.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
        // Validate by attempting to decode
        decode_to_parts(bytes)?;
        Ok(Self {
            bytes: bytes.to_vec(),
        })
    }

    /// Get the encoded bytes (zero-copy)
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Consume and return the encoded bytes
    #[must_use]
    pub fn into_bytes(self) -> Vec<u8> {
        self.bytes
    }

    /// Decode to get semantic fields (positive, exponent, significand).
    ///
    /// Returns `None` for special values (±0, ±∞, NaN) since they have no
    /// exponent or significand. Use [`is_zero()`](Self::is_zero),
    /// [`is_nan()`](Self::is_nan), and [`is_infinity()`](Self::is_infinity)
    /// to identify special values before decoding.
    #[must_use]
    pub fn decode(&self) -> Option<DecodedDecimal> {
        match decode_to_parts(&self.bytes).ok()? {
            DecodedValue::Regular(d) => Some(d),
            DecodedValue::Special(_) => None,
        }
    }

    /// Check if this is zero (either +0 or -0)
    #[must_use]
    pub fn is_zero(&self) -> bool {
        self.bytes.len() == 1 && (self.bytes[0] == 0x80 || self.bytes[0] == 0x40)
    }

    /// Check if this is NaN
    #[must_use]
    pub fn is_nan(&self) -> bool {
        self.bytes.len() == 1 && self.bytes[0] == 0b1110_0000
    }

    /// Check if this is infinity (either + or -)
    #[must_use]
    pub fn is_infinity(&self) -> bool {
        self.is_pos_infinity() || self.is_neg_infinity()
    }

    /// Check if this is positive infinity
    #[must_use]
    pub fn is_pos_infinity(&self) -> bool {
        self.bytes.len() == 1 && self.bytes[0] == 0xC0
    }

    /// Check if this is negative infinity
    #[must_use]
    pub fn is_neg_infinity(&self) -> bool {
        self.bytes.len() == 1 && self.bytes[0] == 0x00
    }

    /// Check if this is a finite number (not infinity or NaN)
    #[must_use]
    pub fn is_finite(&self) -> bool {
        !self.is_infinity() && !self.is_nan()
    }

    /// Create positive infinity
    #[must_use]
    pub fn infinity() -> Self {
        Self {
            bytes: vec![encode_special_byte(SpecialValue::PositiveInfinity)],
        }
    }

    /// Create negative infinity
    #[must_use]
    pub fn neg_infinity() -> Self {
        Self {
            bytes: vec![encode_special_byte(SpecialValue::NegativeInfinity)],
        }
    }

    /// Create NaN
    #[must_use]
    pub fn nan() -> Self {
        Self {
            bytes: vec![encode_special_byte(SpecialValue::NaN)],
        }
    }

    /// Create zero (positive zero)
    #[must_use]
    pub fn zero() -> Self {
        Self {
            bytes: vec![encode_special_byte(SpecialValue::PositiveZero)],
        }
    }

    /// Parse a decimal from string and encode immediately (internal helper)
    fn parse_and_encode(s: &str) -> EncodeResult<Self> {
        let s = s.trim();

        // Handle special values (case-insensitive without allocation)
        if s.eq_ignore_ascii_case("inf")
            || s.eq_ignore_ascii_case("+inf")
            || s.eq_ignore_ascii_case("infinity")
            || s.eq_ignore_ascii_case("+infinity")
        {
            return Ok(Self::infinity());
        }
        if s.eq_ignore_ascii_case("-inf") || s.eq_ignore_ascii_case("-infinity") {
            return Ok(Self::neg_infinity());
        }
        if s.eq_ignore_ascii_case("nan") {
            return Ok(Self::nan());
        }

        // Check for negative
        #[allow(clippy::option_if_let_else)]
        let (positive, s) = if let Some(stripped) = s.strip_prefix('-') {
            (false, stripped)
        } else if let Some(stripped) = s.strip_prefix('+') {
            (true, stripped)
        } else {
            (true, s)
        };

        // Reject empty or digit-free inputs (e.g. "", ".", "+", "-")
        if s.is_empty() || !s.bytes().any(|b| b.is_ascii_digit()) {
            return Err(EncodeError::InvalidFormat(
                "input contains no digits".to_string(),
            ));
        }

        // Handle zero
        if s == "0" || s == "0.0" || s.bytes().all(|b| b == b'0' || b == b'.') {
            return if positive {
                Ok(Self::zero())
            } else {
                Ok(Self {
                    bytes: vec![encode_special_byte(SpecialValue::NegativeZero)],
                })
            };
        }

        // Parse the number - split without allocation
        let (integer_part, fractional_part) = match s.find('.') {
            Some(pos) => {
                let (int, rest) = s.split_at(pos);
                if rest[1..].contains('.') {
                    return Err(EncodeError::InvalidFormat(
                        "multiple decimal points".to_string(),
                    ));
                }
                (int, &rest[1..])
            }
            None => (s, ""),
        };

        // Validate all characters are digits (no intermediate String allocation)
        for b in integer_part.bytes().chain(fractional_part.bytes()) {
            if !b.is_ascii_digit() {
                return Err(EncodeError::InvalidFormat(format!(
                    "invalid digit: {}",
                    b as char
                )));
            }
        }

        // Count leading zeros in the concatenated digit stream (integer_part ++ fractional_part)
        let leading_zeros = integer_part
            .bytes()
            .chain(fractional_part.bytes())
            .take_while(|&b| b == b'0')
            .count();

        // Count trailing zeros in the concatenated digit stream
        let total_len = integer_part.len() + fractional_part.len();
        let trailing_zeros = fractional_part
            .bytes()
            .rev()
            .chain(integer_part.bytes().rev())
            .take_while(|&b| b == b'0')
            .count();

        let significant_len = total_len - leading_zeros - trailing_zeros;
        if significant_len == 0 {
            return if positive {
                Ok(Self::zero())
            } else {
                Ok(Self {
                    bytes: vec![encode_special_byte(SpecialValue::NegativeZero)],
                })
            };
        }

        // Calculate exponent from the position of the decimal point relative to
        // the first significant digit
        let decimal_point_position = integer_part.trim_start_matches('0').len();

        #[allow(clippy::cast_possible_truncation)]
        let (exponent, exponent_positive) = if decimal_point_position > 0 {
            // Number >= 1: exponent = number_of_integer_significant_digits - 1
            ((decimal_point_position - 1) as u64, true)
        } else {
            // Number < 1 (0.xxx): exponent = leading_zeros_in_fractional + 1
            let frac_leading_zeros =
                fractional_part.len() - fractional_part.trim_start_matches('0').len();
            ((frac_leading_zeros + 1) as u64, false)
        };

        // Build significand digits directly into Vec<u8> — single allocation,
        // iterating over the two slices without concatenating them first
        let mut significand = Vec::with_capacity(significant_len);
        for b in integer_part
            .bytes()
            .chain(fractional_part.bytes())
            .skip(leading_zeros)
        {
            significand.push(b - b'0');
        }
        // Trim trailing zeros
        while significand.last() == Some(&0) && significand.len() > 1 {
            significand.pop();
        }

        // Encode immediately
        let bytes = encode_from_parts(positive, exponent_positive, exponent, &significand);

        Ok(Self { bytes })
    }
}

impl FromStr for Decimal {
    type Err = EncodeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse_and_encode(s)
    }
}

impl fmt::Display for Decimal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match decode_to_parts(&self.bytes) {
            Ok(DecodedValue::Regular(d)) => {
                if !d.positive {
                    f.write_str("-")?;
                }

                // First significand digit + decimal point
                write!(f, "{}.", d.significand[0])?;

                // Remaining significand digits — written char-by-char to avoid allocation
                for &digit in &d.significand[1..] {
                    write!(f, "{digit}")?;
                }

                f.write_str(" × 10^")?;

                if !d.exponent_positive {
                    f.write_str("-")?;
                }

                write!(f, "{}", d.exponent)
            }
            Ok(DecodedValue::Special(s)) => {
                let name = match s {
                    SpecialValue::NegativeInfinity => "-∞",
                    SpecialValue::NegativeZero => "-0",
                    SpecialValue::PositiveZero => "0",
                    SpecialValue::PositiveInfinity => "∞",
                    SpecialValue::NaN => "NaN",
                };
                f.write_str(name)
            }
            Err(_) => f.write_str("<invalid>"),
        }
    }
}

impl PartialEq for Decimal {
    fn eq(&self, other: &Self) -> bool {
        // Special case: +0 and -0 are equal
        if self.is_zero() && other.is_zero() {
            return true;
        }
        // Otherwise compare bytes
        self.bytes == other.bytes
    }
}

impl Eq for Decimal {}

impl PartialOrd for Decimal {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Decimal {
    fn cmp(&self, other: &Self) -> Ordering {
        // Special case: +0 and -0 are equal (must be consistent with PartialEq)
        if self.is_zero() && other.is_zero() {
            return Ordering::Equal;
        }
        // Direct byte comparison for order preservation
        self.bytes.cmp(&other.bytes)
    }
}

/// Extract decimal digits from a `u128` into a stack buffer.
///
/// Returns the number of digits written. Digits are stored most-significant
/// first in `buf[0..len]`. Zero produces a single digit `0`.
fn u128_to_digits(mut value: u128, buf: &mut [u8; 39]) -> usize {
    if value == 0 {
        buf[0] = 0;
        return 1;
    }

    // Extract digits in reverse order
    let mut pos = 39;
    while value > 0 {
        pos -= 1;
        #[allow(clippy::cast_possible_truncation)]
        {
            buf[pos] = (value % 10) as u8;
        }
        value /= 10;
    }

    // Shift digits to the front of the buffer
    let len = 39 - pos;
    buf.copy_within(pos..39, 0);
    len
}

/// Core conversion: build a [`Decimal`] from an unsigned magnitude and a sign flag.
///
/// Uses a `[u8; 39]` stack buffer for digit extraction — no heap allocation
/// beyond the final encoded [`Vec<u8>`].
fn from_unsigned_with_sign(value: u128, positive: bool) -> Decimal {
    if value == 0 {
        return Decimal::zero();
    }

    let mut buf = [0u8; 39];
    let len = u128_to_digits(value, &mut buf);
    let digits = &buf[..len];

    // exponent = number_of_digits - 1, always non-negative for integers
    #[allow(clippy::cast_possible_truncation)]
    let exponent = (len - 1) as u64;

    // Strip trailing zeros from the significand (they're encoded in the exponent)
    let sig_end = digits.iter().rposition(|&d| d != 0).map_or(1, |p| p + 1);
    let significand = &digits[..sig_end];

    let bytes = encode_from_parts(positive, true, exponent, significand);
    Decimal { bytes }
}

impl From<u64> for Decimal {
    fn from(value: u64) -> Self {
        from_unsigned_with_sign(u128::from(value), true)
    }
}

impl From<i64> for Decimal {
    fn from(value: i64) -> Self {
        // Handle i64::MIN without overflow: cast to i128 first, then negate
        #[allow(clippy::cast_sign_loss)]
        let (positive, magnitude) = if value >= 0 {
            (true, value as u128)
        } else {
            (false, (-i128::from(value)) as u128)
        };
        from_unsigned_with_sign(magnitude, positive)
    }
}

impl From<u128> for Decimal {
    fn from(value: u128) -> Self {
        from_unsigned_with_sign(value, true)
    }
}

impl From<i128> for Decimal {
    fn from(value: i128) -> Self {
        #[allow(clippy::cast_sign_loss)]
        let (positive, magnitude) = if value >= 0 {
            (true, value as u128)
        } else {
            // i128::MIN: -(i128::MIN as i128) overflows, but
            // (i128::MIN as u128) == 2^127 which is the correct magnitude
            (false, value.unsigned_abs())
        };
        from_unsigned_with_sign(magnitude, positive)
    }
}

// Smaller unsigned types — widen to u64
impl From<u8> for Decimal {
    fn from(value: u8) -> Self {
        Self::from(u64::from(value))
    }
}

impl From<u16> for Decimal {
    fn from(value: u16) -> Self {
        Self::from(u64::from(value))
    }
}

impl From<u32> for Decimal {
    fn from(value: u32) -> Self {
        Self::from(u64::from(value))
    }
}

// Smaller signed types — widen to i64
impl From<i8> for Decimal {
    fn from(value: i8) -> Self {
        Self::from(i64::from(value))
    }
}

impl From<i16> for Decimal {
    fn from(value: i16) -> Self {
        Self::from(i64::from(value))
    }
}

impl From<i32> for Decimal {
    fn from(value: i32) -> Self {
        Self::from(i64::from(value))
    }
}

/// Fixed-capacity stack buffer that implements `fmt::Write`.
///
/// Used by the `From<f64>` / `From<f32>` impls to format a float into
/// a string without heap allocation. 25 bytes is enough for any `f64`
/// (sign + up to 17 significant digits + decimal point + 'e' + exponent).
struct StackBuf {
    buf: [u8; 25],
    len: usize,
}

impl StackBuf {
    const fn new() -> Self {
        Self {
            buf: [0; 25],
            len: 0,
        }
    }

    fn as_str(&self) -> &str {
        // Safety: fmt::Write for str only writes valid UTF-8
        unsafe { std::str::from_utf8_unchecked(&self.buf[..self.len]) }
    }
}

impl fmt::Write for StackBuf {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        let bytes = s.as_bytes();
        let new_len = self.len + bytes.len();
        if new_len > self.buf.len() {
            return Err(fmt::Error);
        }
        self.buf[self.len..new_len].copy_from_slice(bytes);
        self.len = new_len;
        Ok(())
    }
}

impl From<f64> for Decimal {
    /// Convert an [`f64`] to a [`Decimal`](Self) using the shortest roundtrip representation.
    ///
    /// Special float values map directly: `f64::NAN` → `Decimal::nan()`,
    /// `f64::INFINITY` → `Decimal::infinity()`, etc. Negative zero is preserved.
    ///
    /// Uses a stack buffer for formatting — no heap allocation beyond the
    /// final encoded `Vec<u8>`.
    fn from(value: f64) -> Self {
        if value.is_nan() {
            return Self::nan();
        }
        if value.is_infinite() {
            return if value.is_sign_positive() {
                Self::infinity()
            } else {
                Self::neg_infinity()
            };
        }
        if value == 0.0 {
            return if value.is_sign_positive() {
                Self::zero()
            } else {
                Self {
                    bytes: vec![encode_special_byte(SpecialValue::NegativeZero)],
                }
            };
        }

        let mut buf = StackBuf::new();
        // Rust's f64 Display produces the shortest roundtrip representation
        write!(buf, "{value}").expect("f64 Display should fit in 25 bytes");
        // Delegate to FromStr — the string is already validated decimal
        buf.as_str()
            .parse()
            .expect("f64 Display output should always be a valid decimal")
    }
}

impl From<f32> for Decimal {
    fn from(value: f32) -> Self {
        Self::from(f64::from(value))
    }
}

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

    #[test]
    fn test_parse_positive() {
        let d: Decimal = "123.456".parse().unwrap();
        let decoded = d.decode().unwrap();
        assert!(decoded.positive);
        assert!(decoded.exponent_positive);
        assert_eq!(decoded.exponent, 2);
        // Significand may have trailing zeros from declet padding
        assert!(decoded.significand.starts_with(&[1, 2, 3, 4, 5, 6]));
    }

    #[test]
    fn test_parse_negative() {
        let d: Decimal = "-103.2".parse().unwrap();
        let decoded = d.decode().unwrap();
        assert!(!decoded.positive);
        assert!(decoded.exponent_positive);
        assert_eq!(decoded.exponent, 2);
        // Significand may have trailing zeros from declet padding
        assert!(decoded.significand.starts_with(&[1, 0, 3, 2]));
    }

    #[test]
    fn test_parse_small() {
        let d: Decimal = "0.0405".parse().unwrap();
        let decoded = d.decode().unwrap();
        assert!(decoded.positive);
        assert!(!decoded.exponent_positive);
        assert_eq!(decoded.exponent, 2);
        // Significand may have trailing zeros from declet padding
        assert!(decoded.significand.starts_with(&[4, 0, 5]));
    }

    #[test]
    fn test_parse_zero() {
        let d: Decimal = "0".parse().unwrap();
        assert!(d.is_zero());
    }

    #[test]
    fn test_parse_infinity() {
        let d: Decimal = "+inf".parse().unwrap();
        assert!(d.is_pos_infinity());
    }

    #[test]
    fn test_zero_equality() {
        let pos_zero: Decimal = "0".parse().unwrap();
        let neg_zero: Decimal = "-0".parse().unwrap();
        assert_eq!(pos_zero, neg_zero, "+0 should equal -0");
    }

    #[test]
    fn test_special_values_case_insensitive() {
        // Test case-insensitive parsing of special values
        assert!("INF".parse::<Decimal>().unwrap().is_pos_infinity());
        assert!("Inf".parse::<Decimal>().unwrap().is_pos_infinity());
        assert!("+INFINITY".parse::<Decimal>().unwrap().is_pos_infinity());
        assert!("-inf".parse::<Decimal>().unwrap().is_neg_infinity());
        assert!("-Infinity".parse::<Decimal>().unwrap().is_neg_infinity());
        assert!("NaN".parse::<Decimal>().unwrap().is_nan());
        assert!("nan".parse::<Decimal>().unwrap().is_nan());
        assert!("NAN".parse::<Decimal>().unwrap().is_nan());
    }

    #[test]
    fn test_multiple_decimal_points() {
        // Should fail with multiple decimal points
        assert!("123.456.789".parse::<Decimal>().is_err());
        assert!("1.2.3".parse::<Decimal>().is_err());
    }

    #[test]
    fn test_zero_ord_consistency() {
        // Ord must agree with PartialEq: +0 == -0 implies cmp == Equal
        let pos_zero: Decimal = "0".parse().unwrap();
        let neg_zero: Decimal = "-0".parse().unwrap();
        assert_eq!(
            pos_zero.cmp(&neg_zero),
            Ordering::Equal,
            "+0.cmp(-0) must be Equal to match PartialEq"
        );
        assert_eq!(
            neg_zero.cmp(&pos_zero),
            Ordering::Equal,
            "-0.cmp(+0) must be Equal to match PartialEq"
        );
    }

    #[test]
    fn test_reject_empty_and_bare_inputs() {
        // Empty string
        assert!("".parse::<Decimal>().is_err(), "empty string should fail");
        // Bare sign
        assert!("+".parse::<Decimal>().is_err(), "bare '+' should fail");
        assert!("-".parse::<Decimal>().is_err(), "bare '-' should fail");
        // Bare dot
        assert!(".".parse::<Decimal>().is_err(), "bare '.' should fail");
        // Sign + dot
        assert!("-.".parse::<Decimal>().is_err(), "'-.' should fail");
        assert!("+.".parse::<Decimal>().is_err(), "'+.' should fail");
        // Whitespace only
        assert!("   ".parse::<Decimal>().is_err(), "whitespace should fail");
    }

    // =========================================================================
    // From<integer> tests
    // =========================================================================

    #[test]
    fn test_from_u64_matches_parse() {
        let cases: &[u64] = &[0, 1, 9, 10, 42, 100, 999, 1000, 123456789, u64::MAX];
        for &n in cases {
            let from_int = Decimal::from(n);
            let from_str: Decimal = n.to_string().parse().unwrap();
            assert_eq!(
                from_int.as_bytes(),
                from_str.as_bytes(),
                "From<u64> mismatch for {n}"
            );
        }
    }

    #[test]
    fn test_from_i64_matches_parse() {
        let cases: &[i64] = &[
            i64::MIN,
            -123456789,
            -1000,
            -42,
            -1,
            0,
            1,
            42,
            1000,
            123456789,
            i64::MAX,
        ];
        for &n in cases {
            let from_int = Decimal::from(n);
            let from_str: Decimal = n.to_string().parse().unwrap();
            assert_eq!(
                from_int.as_bytes(),
                from_str.as_bytes(),
                "From<i64> mismatch for {n}"
            );
        }
    }

    #[test]
    fn test_from_i128_extremes() {
        let cases: &[i128] = &[i128::MIN, -1, 0, 1, i128::MAX];
        for &n in cases {
            let from_int = Decimal::from(n);
            let from_str: Decimal = n.to_string().parse().unwrap();
            assert_eq!(
                from_int.as_bytes(),
                from_str.as_bytes(),
                "From<i128> mismatch for {n}"
            );
        }
    }

    #[test]
    fn test_from_u128_max() {
        let from_int = Decimal::from(u128::MAX);
        let from_str: Decimal = u128::MAX.to_string().parse().unwrap();
        assert_eq!(from_int.as_bytes(), from_str.as_bytes());
    }

    #[test]
    fn test_from_small_types() {
        // Verify widening impls produce the same encoding
        assert_eq!(
            Decimal::from(42u8).as_bytes(),
            Decimal::from(42u64).as_bytes()
        );
        assert_eq!(
            Decimal::from(42u16).as_bytes(),
            Decimal::from(42u64).as_bytes()
        );
        assert_eq!(
            Decimal::from(42u32).as_bytes(),
            Decimal::from(42u64).as_bytes()
        );
        assert_eq!(
            Decimal::from(-7i8).as_bytes(),
            Decimal::from(-7i64).as_bytes()
        );
        assert_eq!(
            Decimal::from(-7i16).as_bytes(),
            Decimal::from(-7i64).as_bytes()
        );
        assert_eq!(
            Decimal::from(-7i32).as_bytes(),
            Decimal::from(-7i64).as_bytes()
        );
    }

    #[test]
    fn test_from_u64_order_preserved() {
        let values: Vec<u64> = vec![0, 1, 2, 9, 10, 99, 100, 999, 1000, u64::MAX];
        let decimals: Vec<Decimal> = values.iter().map(|&v| Decimal::from(v)).collect();
        for i in 1..decimals.len() {
            assert!(
                decimals[i - 1] < decimals[i],
                "Order not preserved: {} < {} failed",
                values[i - 1],
                values[i]
            );
        }
    }

    #[test]
    fn test_from_zero_is_positive_zero() {
        let d = Decimal::from(0u64);
        assert!(d.is_zero());
        assert_eq!(d.as_bytes(), Decimal::zero().as_bytes());
    }

    // =========================================================================
    // From<f64> / From<f32> tests
    // =========================================================================

    #[test]
    fn test_from_f64_matches_parse() {
        let cases: &[f64] = &[1.0, -1.0, 0.5, -0.5, 42.0, 123.456, 0.001, 1e10, 1e-10];
        for &v in cases {
            let from_float = Decimal::from(v);
            let from_str: Decimal = v.to_string().parse().unwrap();
            assert_eq!(
                from_float.as_bytes(),
                from_str.as_bytes(),
                "From<f64> mismatch for {v}"
            );
        }
    }

    #[test]
    fn test_from_f64_special_values() {
        assert!(Decimal::from(f64::NAN).is_nan());
        assert!(Decimal::from(f64::INFINITY).is_pos_infinity());
        assert!(Decimal::from(f64::NEG_INFINITY).is_neg_infinity());
        assert!(Decimal::from(0.0_f64).is_zero());
        assert!(Decimal::from(-0.0_f64).is_zero());
    }

    #[test]
    fn test_from_f64_negative_zero_preserved() {
        let neg_zero = Decimal::from(-0.0_f64);
        let pos_zero = Decimal::from(0.0_f64);
        // Both are zero and compare equal
        assert_eq!(neg_zero, pos_zero);
        // But their underlying bytes differ (-0 = 0x40, +0 = 0x80)
        assert_ne!(neg_zero.as_bytes(), pos_zero.as_bytes());
    }

    #[test]
    fn test_from_f64_order_preserved() {
        let values: Vec<f64> = vec![-1000.0, -1.0, -0.001, 0.001, 1.0, 1000.0];
        let decimals: Vec<Decimal> = values.iter().map(|&v| Decimal::from(v)).collect();
        for i in 1..decimals.len() {
            assert!(
                decimals[i - 1] < decimals[i],
                "Order not preserved: {} < {} failed",
                values[i - 1],
                values[i]
            );
        }
    }

    #[test]
    fn test_from_f32_matches_f64_widening() {
        let v = 2.72_f32;
        let from_f32 = Decimal::from(v);
        let from_f64 = Decimal::from(f64::from(v));
        assert_eq!(from_f32.as_bytes(), from_f64.as_bytes());
    }
}