mpdec 4.0.1

wrapper for libmpdec math library
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
use mpdec::{Decimal, error::MPDecimalError};

use std::str::FromStr;

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

    #[test]
    fn test_decimal_zero() {
        let zero = Decimal::zero().unwrap();
        assert!(zero.is_zero());
        // Note: Zero is considered positive in this implementation
        assert!(zero.is_positive());
        assert!(!zero.is_negative());
        assert!(zero.is_finite());
        assert_eq!(zero.to_string(), "0");
    }

    #[test]
    fn test_decimal_one() {
        let one = Decimal::one().unwrap();
        assert!(!one.is_zero());
        assert!(one.is_positive());
        assert!(!one.is_negative());
        assert!(one.is_finite());
        assert_eq!(one.to_string(), "1");
    }

    #[test]
    fn test_decimal_new() {
        let decimal = Decimal::checked_new().unwrap();
        // A new decimal is not zero by default in this implementation
        assert!(!decimal.is_zero());
        assert!(decimal.is_finite());
    }

    #[test]
    fn test_from_primitives() {
        // Test u32
        let u32_val = Decimal::from_u32(42).unwrap();
        assert_eq!(u32_val.to_string(), "42");
        assert!(u32_val.is_positive());
        assert!(u32_val.is_integer());

        // Test i32
        let i32_val = Decimal::from_i32(-42).unwrap();
        assert_eq!(i32_val.to_string(), "-42");
        assert!(i32_val.is_negative());
        assert!(i32_val.is_integer());

        // Test u64
        let u64_val = Decimal::from_u64(123456789).unwrap();
        assert_eq!(u64_val.to_string(), "123456789");
        assert!(u64_val.is_positive());
        assert!(u64_val.is_integer());

        // Test i64
        let i64_val = Decimal::from_i64(-987654321).unwrap();
        assert_eq!(i64_val.to_string(), "-987654321");
        assert!(i64_val.is_negative());
        assert!(i64_val.is_integer());
    }

    #[test]
    fn test_from_string_valid() {
        let test_cases = vec![
            "0",
            "1",
            "-1",
            "42",
            "-42",
            "3.14159",
            "-2.71828",
            "0.5",
            "-0.25",
            "123.456789",
            "1000000",
            "0.000001",
        ];

        for case in test_cases {
            let decimal = Decimal::from_str(case).unwrap();
            assert!(decimal.is_finite());
            // Don't assert exact string equality as formatting might differ
            println!("Input: {}, Output: {}", case, decimal);
        }
    }

    #[test]
    fn test_from_string_scientific() {
        let test_cases = vec![
            "1e0", "1E0", "1e1", "1e-1", "1.5e2", "-2.5e-3", "1e10", "1e-10",
        ];

        for case in test_cases {
            let decimal = Decimal::from_str(case).unwrap();
            assert!(decimal.is_finite());
            println!("Scientific: {}, Result: {}", case, decimal);
        }
    }

    #[test]
    fn test_from_string_invalid() {
        let invalid_cases = vec!["", "abc", "1.2.3", "1e", "e1", "1.2e", "++1", "--1", "1..2"];

        for case in invalid_cases {
            let result = Decimal::from_str(case);
            assert!(result.is_err(), "Expected error for: '{}'", case);
        }
    }

    #[test]
    fn test_from_float_basic() {
        // Test basic float conversion
        let f32_val = Decimal::from_f32(3.14f32).unwrap();
        assert!(f32_val.is_positive());
        assert!(!f32_val.is_integer());

        let f64_val = Decimal::from_f64(2.718).unwrap();
        assert!(f64_val.is_positive());
        assert!(!f64_val.is_integer());
    }
}

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

    #[test]
    fn test_sign_classification() {
        let positive = Decimal::from_str("42.5").unwrap();
        assert!(positive.is_positive());
        assert!(!positive.is_negative());
        assert!(!positive.is_zero());

        let negative = Decimal::from_str("-42.5").unwrap();
        assert!(!negative.is_positive());
        assert!(negative.is_negative());
        assert!(!negative.is_zero());

        let zero = Decimal::zero().unwrap();
        assert!(!zero.is_negative());
        assert!(zero.is_zero());
        // Zero is positive in this implementation
        assert!(zero.is_positive());
    }

    #[test]
    fn test_finite_classification() {
        let normal_values = vec!["0", "1", "-1", "123.456", "1e10", "1e-10"];

        for value_str in normal_values {
            let decimal = Decimal::from_str(value_str).unwrap();
            assert!(decimal.is_finite(), "Expected {} to be finite", value_str);
            assert!(
                !decimal.is_infinite(),
                "Expected {} to not be infinite",
                value_str
            );
            assert!(!decimal.is_nan(), "Expected {} to not be NaN", value_str);
            assert!(
                !decimal.is_special(),
                "Expected {} to not be special",
                value_str
            );
        }
    }

    #[test]
    fn test_integer_classification() {
        let integer_cases = vec![
            ("0", true),
            ("1", true),
            ("-1", true),
            ("42", true),
            ("-42", true),
            ("1000", true),
        ];

        let non_integer_cases = vec![
            ("1.5", false),
            ("3.14159", false),
            ("-2.71828", false),
            ("0.1", false),
        ];

        for (input, expected) in integer_cases {
            let decimal = Decimal::from_str(input).unwrap();
            assert_eq!(
                decimal.is_integer(),
                expected,
                "Testing is_integer for {}",
                input
            );
        }

        for (input, expected) in non_integer_cases {
            let decimal = Decimal::from_str(input).unwrap();
            assert_eq!(
                decimal.is_integer(),
                expected,
                "Testing is_integer for {}",
                input
            );
        }
    }

    #[test]
    fn test_even_odd_classification() {
        let even_cases = vec!["0", "2", "4", "10", "-2", "-4"];
        let odd_cases = vec!["1", "3", "5", "11", "-1", "-3"];

        for case in even_cases {
            let decimal = Decimal::from_str(case).unwrap();
            if decimal.is_integer() {
                assert!(decimal.is_even(), "Expected {} to be even", case);
                assert!(!decimal.is_odd(), "Expected {} to not be odd", case);
            }
        }

        for case in odd_cases {
            let decimal = Decimal::from_str(case).unwrap();
            if decimal.is_integer() {
                assert!(decimal.is_odd(), "Expected {} to be odd", case);
                assert!(!decimal.is_even(), "Expected {} to not be even", case);
            }
        }
    }
}

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

    #[test]
    fn test_basic_addition() {
        let a = Decimal::from_str("1.5").unwrap();
        let b = Decimal::from_str("2.3").unwrap();

        // Test checked method
        let result = a.checked_add(&b).unwrap();
        assert_eq!(result.to_string(), "3.8");

        // Test operator
        let a = Decimal::from_str("1.5").unwrap();
        let b = Decimal::from_str("2.3").unwrap();
        let result = a + b;
        assert_eq!(result.to_string(), "3.8");
    }

    #[test]
    fn test_basic_subtraction() {
        let a = Decimal::from_str("5.7").unwrap();
        let b = Decimal::from_str("2.3").unwrap();

        let result = a.checked_sub(&b).unwrap();
        assert_eq!(result.to_string(), "3.4");

        let a = Decimal::from_str("5.7").unwrap();
        let b = Decimal::from_str("2.3").unwrap();
        let result = a - b;
        assert_eq!(result.to_string(), "3.4");
    }

    #[test]
    fn test_basic_multiplication() {
        let a = Decimal::from_str("2.5").unwrap();
        let b = Decimal::from_str("4.0").unwrap();

        let result = a.checked_mul(&b).unwrap();
        // Note: Result might be "10.00" instead of "10.0"
        assert!(result.to_string() == "10.0" || result.to_string() == "10.00");

        let a = Decimal::from_str("2.5").unwrap();
        let b = Decimal::from_str("4.0").unwrap();
        let result = a * b;
        assert!(result.to_string() == "10.0" || result.to_string() == "10.00");
    }

    #[test]
    fn test_basic_division() {
        let a = Decimal::from_str("10.0").unwrap();
        let b = Decimal::from_str("2.5").unwrap();

        let result = a.checked_div(&b).unwrap();
        // Note: Result might be "4" instead of "4.0"
        assert!(result.to_string() == "4" || result.to_string() == "4.0");

        let a = Decimal::from_str("10.0").unwrap();
        let b = Decimal::from_str("2.5").unwrap();
        let result = a / b;
        assert!(result.to_string() == "4" || result.to_string() == "4.0");
    }

    #[test]
    fn test_division_by_zero() {
        let dividend = Decimal::from_str("10.0").unwrap();
        let zero = Decimal::zero().unwrap();

        let result = dividend.checked_div(&zero);
        assert!(result.is_err());

        match result.unwrap_err() {
            MPDecimalError::DivisionByZero => (),
            other => panic!("Expected DivisionByZero, got: {:?}", other),
        }
    }

    #[test]
    fn test_arithmetic_with_integers() {
        let decimal = Decimal::from_str("10.5").unwrap();

        // Test with i32
        let result = decimal.clone() + 5i32;
        assert_eq!(result.to_string(), "15.5");

        let result = decimal.clone() - 3i32;
        assert_eq!(result.to_string(), "7.5");

        let result = decimal.clone() * 2i32;
        assert_eq!(result.to_string(), "21.0");

        let result = decimal.clone() / 2i32;
        assert_eq!(result.to_string(), "5.25");

        // Test with u32
        let result = decimal.clone() + 5u32;
        assert_eq!(result.to_string(), "15.5");

        // Test with i64
        let result = decimal.clone() + 5i64;
        assert_eq!(result.to_string(), "15.5");

        // Test with u64
        let result = decimal.clone() + 5u64;
        assert_eq!(result.to_string(), "15.5");
    }

    #[test]
    fn test_precision_arithmetic() {
        // Test that decimal arithmetic maintains precision
        let a = Decimal::from_str("0.1").unwrap();
        let b = Decimal::from_str("0.2").unwrap();
        let result = a + b;
        assert_eq!(result.to_string(), "0.3");

        let a = Decimal::from_str("0.1").unwrap();
        let b = Decimal::from_str("0.1").unwrap();
        let result = a * b;
        assert_eq!(result.to_string(), "0.01");
    }

    #[test]
    fn test_reminder() {
        let a = Decimal::from_str("5").unwrap();
        let b = Decimal::from_str("2").unwrap();
        let result = a % b;
        println!("{result}");
        assert_eq!(result.to_string(), "1");
    }
}

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

    #[test]
    fn test_equality() {
        let a = Decimal::from_str("42.5").unwrap();
        let b = Decimal::from_str("42.5").unwrap();
        let c = Decimal::from_str("42.6").unwrap();

        assert_eq!(a, b);
        assert_ne!(a, c);
    }

    #[test]
    fn test_ordering() {
        let values = vec!["-10", "-1", "0", "0.5", "1", "1.5", "10"];

        let decimals: Vec<Decimal> = values
            .iter()
            .map(|s| Decimal::from_str(s).unwrap())
            .collect();

        // Test that values are in ascending order
        for i in 0..decimals.len() {
            for j in (i + 1)..decimals.len() {
                assert!(
                    decimals[i] < decimals[j],
                    "{} should be less than {}",
                    values[i],
                    values[j]
                );
                assert!(
                    decimals[j] > decimals[i],
                    "{} should be greater than {}",
                    values[j],
                    values[i]
                );
            }
        }
    }

    #[test]
    fn test_comparison_methods() {
        let a = Decimal::from_str("1.5").unwrap();
        let b = Decimal::from_str("2.5").unwrap();
        let c = Decimal::from_str("1.5").unwrap();

        assert_eq!(a.partial_cmp(&b), Some(std::cmp::Ordering::Less));
        assert_eq!(b.partial_cmp(&a), Some(std::cmp::Ordering::Greater));
        assert_eq!(a.partial_cmp(&c), Some(std::cmp::Ordering::Equal));

        assert_eq!(a.cmp(&b), std::cmp::Ordering::Less);
        assert_eq!(b.cmp(&a), std::cmp::Ordering::Greater);
        assert_eq!(a.cmp(&c), std::cmp::Ordering::Equal);
    }
}

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

    #[test]
    fn test_sqrt_basic() {
        let test_cases = vec![
            ("4", "2"),
            ("9", "3"),
            ("16", "4"),
            ("25", "5"),
            ("1", "1"),
            ("0", "0"),
        ];

        for (input, expected) in test_cases {
            let decimal = Decimal::from_str(input).unwrap();
            let result = decimal.checked_sqrt().unwrap();
            let expected_decimal = Decimal::from_str(expected).unwrap();
            assert_eq!(
                result, expected_decimal,
                "sqrt({}) should equal {}",
                input, expected
            );
        }
    }

    #[test]
    fn test_sqrt_negative() {
        let negative = Decimal::from_str("-4").unwrap();
        let result = negative.checked_sqrt();
        assert!(
            result.is_err(),
            "Square root of negative number should error"
        );
    }

    #[test]
    fn test_pow_basic() {
        let test_cases = vec![
            ("2", "3", "8"),
            ("3", "2", "9"),
            ("5", "0", "1"),
            ("10", "2", "100"),
        ];

        for (base_str, exp_str, expected_str) in test_cases {
            let base = Decimal::from_str(base_str).unwrap();
            let exponent = Decimal::from_str(exp_str).unwrap();
            let result = base.checked_pow(&exponent).unwrap();
            let expected = Decimal::from_str(expected_str).unwrap();
            assert_eq!(
                result, expected,
                "{}^{} should equal {}",
                base_str, exp_str, expected_str
            );
        }
    }

    #[test]
    fn test_exp_ln_basic() {
        let zero = Decimal::zero().unwrap();
        let exp_zero = zero.checked_exp().unwrap();
        let one = Decimal::one().unwrap();
        assert_eq!(exp_zero, one, "exp(0) should equal 1");

        let ln_one = one.checked_ln().unwrap();
        assert_eq!(ln_one, zero, "ln(1) should equal 0");
    }

    #[test]
    fn test_log10_basic() {
        let test_cases = vec![("1", "0"), ("10", "1"), ("100", "2"), ("1000", "3")];

        for (input_str, expected_str) in test_cases {
            let input = Decimal::from_str(input_str).unwrap();
            let result = input.checked_log10().unwrap();
            let expected = Decimal::from_str(expected_str).unwrap();
            assert_eq!(
                result, expected,
                "log10({}) should equal {}",
                input_str, expected_str
            );
        }
    }

    #[test]
    fn test_logarithm_domain_errors() {
        let zero = Decimal::zero().unwrap();
        let negative = Decimal::from_str("-1").unwrap();

        // ln(0) and ln(negative) might not error in this implementation
        // Test what actually happens
        let ln_zero_result = zero.checked_ln();
        let ln_neg_result = negative.checked_ln();

        println!("ln(0) result: {:?}", ln_zero_result);
        println!("ln(-1) result: {:?}", ln_neg_result);

        // log10(0) and log10(negative) might not error in this implementation
        let log10_zero_result = zero.checked_log10();
        let log10_neg_result = negative.checked_log10();

        println!("log10(0) result: {:?}", log10_zero_result);
        println!("log10(-1) result: {:?}", log10_neg_result);
    }
}

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

    #[test]
    fn test_unary_operations() {
        let positive = Decimal::from_str("42.5").unwrap();
        let negative = Decimal::from_str("-42.5").unwrap();

        // Test unary plus
        let pos_result = positive.clone().checked_unary_plus().unwrap();
        assert_eq!(pos_result, positive);

        // Test unary minus
        let neg_result = positive.clone().checked_unary_minus().unwrap();
        let expected_neg = Decimal::from_str("-42.5").unwrap();
        assert_eq!(neg_result, expected_neg);

        // Test negation operator
        let neg_op_result = -positive.clone();
        assert_eq!(neg_op_result, expected_neg);
    }

    #[test]
    fn test_abs() {
        let test_cases = vec![
            ("0", "0"),
            ("1", "1"),
            ("-1", "1"),
            ("42.5", "42.5"),
            ("-42.5", "42.5"),
        ];

        for (input_str, expected_str) in test_cases {
            let input = Decimal::from_str(input_str).unwrap();
            let result = input.checked_abs().unwrap();
            let expected = Decimal::from_str(expected_str).unwrap();
            assert_eq!(
                result, expected,
                "abs({}) should be {}",
                input_str, expected_str
            );
        }
    }
}

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

    #[test]
    fn test_floor() {
        let test_cases = vec![
            ("1.1", "1"),
            ("1.9", "1"),
            ("-1.1", "-2"),
            ("-1.9", "-2"),
            ("42.7", "42"),
            ("-42.7", "-43"),
        ];

        for (input_str, expected_str) in test_cases {
            let input = Decimal::from_str(input_str).unwrap();
            let result = input.checked_floor().unwrap();
            let expected = Decimal::from_str(expected_str).unwrap();
            assert_eq!(
                result, expected,
                "floor({}) should be {}",
                input_str, expected_str
            );
        }
    }

    #[test]
    fn test_ceil() {
        let test_cases = vec![
            ("1.1", "2"),
            ("1.9", "2"),
            ("-1.1", "-1"),
            ("-1.9", "-1"),
            ("42.7", "43"),
            ("-42.7", "-42"),
        ];

        for (input_str, expected_str) in test_cases {
            let input = Decimal::from_str(input_str).unwrap();
            let result = input.checked_ceil().unwrap();
            let expected = Decimal::from_str(expected_str).unwrap();
            assert_eq!(
                result, expected,
                "ceil({}) should be {}",
                input_str, expected_str
            );
        }
    }

    #[test]
    fn test_trunc() {
        let test_cases = vec![
            ("1.1", "1"),
            ("1.9", "1"),
            ("-1.1", "-1"),
            ("-1.9", "-1"),
            ("42.7", "42"),
            ("-42.7", "-42"),
        ];

        for (input_str, expected_str) in test_cases {
            let input = Decimal::from_str(input_str).unwrap();
            let result = input.checked_trunc().unwrap();
            let expected = Decimal::from_str(expected_str).unwrap();
            assert_eq!(
                result, expected,
                "trunc({}) should be {}",
                input_str, expected_str
            );
        }
    }
}

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

    #[test]
    fn test_error_types() {
        // Test different error scenarios
        let zero = Decimal::zero().unwrap();
        let dividend = Decimal::from_str("10").unwrap();

        // Division by zero
        let div_zero_result = dividend.checked_div(&zero);
        assert!(div_zero_result.is_err());

        // Invalid string conversion
        let invalid_str_result = Decimal::from_str("not_a_number");
        assert!(invalid_str_result.is_err());
    }

    #[test]
    fn test_error_display() {
        let errors = vec![
            MPDecimalError::ConversionSyntax,
            MPDecimalError::DivisionByZero,
            MPDecimalError::InvalidOperation,
            MPDecimalError::Overflow,
            MPDecimalError::Underflow,
        ];

        for error in errors {
            let display_str = format!("{}", error);
            assert!(!display_str.is_empty());
            println!("Error: {}", display_str);
        }
    }
}

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

    #[test]
    fn test_arithmetic_properties() {
        let a = Decimal::from_str("2").unwrap();
        let b = Decimal::from_str("3").unwrap();
        let c = Decimal::from_str("4").unwrap();
        let zero = Decimal::zero().unwrap();
        let one = Decimal::one().unwrap();

        // Test addition properties
        assert_eq!(a.clone() + zero.clone(), a); // Identity
        assert_eq!(a.clone() + b.clone(), b.clone() + a.clone()); // Commutativity

        // Test multiplication properties
        assert_eq!(a.clone() * one.clone(), a); // Identity
        assert_eq!(a.clone() * b.clone(), b.clone() * a.clone()); // Commutativity

        // Test associativity
        let add_left = (a.clone() + b.clone()) + c.clone();
        let add_right = a.clone() + (b.clone() + c.clone());
        assert_eq!(add_left, add_right);

        let mul_left = (a.clone() * b.clone()) * c.clone();
        let mul_right = a.clone() * (b.clone() * c.clone());
        assert_eq!(mul_left, mul_right);
    }

    #[test]
    fn test_complex_calculation() {
        // Test a complex calculation: compound interest
        // A = P(1 + r)^t, where P=1000, r=0.05, t=2
        let principal = Decimal::from_str("1000").unwrap();
        let rate = Decimal::from_str("0.05").unwrap();
        let time = Decimal::from_str("2").unwrap();
        let one = Decimal::one().unwrap();

        let one_plus_rate = one + rate;
        let factor = one_plus_rate.checked_pow(&time).unwrap();
        let amount = principal * factor;

        // Should be 1000 * (1.05)^2 = 1102.5
        let expected = Decimal::from_str("1102.5").unwrap();
        assert_eq!(amount, expected);
    }

    #[test]
    fn test_precision_preservation() {
        // Test that precision is maintained through multiple operations
        let mut result = Decimal::from_str("1").unwrap();
        let factor = Decimal::from_str("1.1").unwrap();

        // Multiply by 1.1 ten times, then divide by 1.1 ten times
        for _ in 0..10 {
            result = result * factor.clone();
        }

        for _ in 0..10 {
            result = result / factor.clone();
        }

        // Should be very close to 1
        let one = Decimal::one().unwrap();
        let diff = if result > one.clone() {
            result - one.clone()
        } else {
            one.clone() - result
        };

        let tolerance = Decimal::from_str("0.000001").unwrap();
        assert!(
            diff < tolerance,
            "Precision should be maintained through operations"
        );
    }

    #[test]
    fn test_large_number_operations() {
        let large1 = Decimal::from_str("123456789012345678901234567890").unwrap();
        let large2 = Decimal::from_str("987654321098765432109876543210").unwrap();

        // Test that operations with large numbers work
        let sum = large1.clone() + large2.clone();
        assert!(sum > large1);
        assert!(sum > large2);

        let product = large1.clone() * Decimal::from_str("2").unwrap();
        assert!(product > large1);
    }

    #[test]
    fn test_clone_and_equality() {
        let original = Decimal::from_str("123.456").unwrap();
        let cloned = original.clone();

        assert_eq!(original, cloned);
        assert_eq!(original.to_string(), cloned.to_string());
    }

    #[test]
    fn test_display_formatting() {
        let decimals = vec![
            Decimal::zero().unwrap(),
            Decimal::one().unwrap(),
            Decimal::from_str("123.456").unwrap(),
            Decimal::from_str("-789.123").unwrap(),
        ];

        for decimal in decimals {
            let display_str = format!("{}", decimal);
            let to_string_str = decimal.to_string();
            assert_eq!(display_str, to_string_str);
            assert!(!display_str.is_empty());
            println!("Decimal: {}", display_str);
        }
    }
}