uninum 0.1.1

A robust, ergonomic unified number type for Rust with automatic overflow handling, type promotion, and cross-type consistency.
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
//! Tests for TryFrom conversions between Number and primitive types
//!
//! This module tests the TryFrom trait implementations for converting
//! between Number variants and primitive types with error handling.
//!
//! Test Organization:
//! - Conversions to unsigned integer targets (u8, u16, u32, u64)
//! - Conversions to signed integer targets (i8, i16, i32, i64)
//! - Conversions to float targets (f64)
//! - Special float values handling (NaN, Infinity)
//! - Precision loss and boundary cases
//! - Decimal-specific conversions (feature-gated)

use std::convert::TryFrom;

#[cfg(feature = "decimal")]
use rust_decimal::Decimal;
#[cfg(feature = "decimal")]
use std::str::FromStr;
use uninum::{Number, TryFromNumberError, num};

#[test]
fn test_try_from_to_u8() {
    // From U64: <=255 Ok, > Out
    assert_eq!(u8::try_from(Number::from(0_u64)).unwrap(), 0);
    assert_eq!(u8::try_from(Number::from(255_u64)).unwrap(), 255);
    assert!(matches!(
        u8::try_from(Number::from(256_u64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // From I64: >=0 <=255 Ok, <0 Out, >255 Out
    assert_eq!(u8::try_from(Number::from(0_i64)).unwrap(), 0);
    assert_eq!(u8::try_from(Number::from(255_i64)).unwrap(), 255);
    assert!(matches!(
        u8::try_from(Number::from(-1_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        u8::try_from(Number::from(256_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // From U64: <=255 Ok, > Out
    assert_eq!(u8::try_from(Number::from(0_u64)).unwrap(), 0);
    assert_eq!(u8::try_from(Number::from(255_u64)).unwrap(), 255);
    assert!(matches!(
        u8::try_from(Number::from(256_u64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // From I64: >=0 <=255 Ok, <0 Out, >255 Out
    assert_eq!(u8::try_from(Number::from(0_i64)).unwrap(), 0);
    assert_eq!(u8::try_from(Number::from(255_i64)).unwrap(), 255);
    assert!(matches!(
        u8::try_from(Number::from(-1_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        u8::try_from(Number::from(256_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // From F64: similar
    assert_eq!(u8::try_from(num!(200.0f64)).unwrap(), 200);
    assert!(matches!(
        u8::try_from(num!(200.5f64)),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    assert!(matches!(
        u8::try_from(num!(-1.0f64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        u8::try_from(num!(256.0f64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        u8::try_from(num!(f64::INFINITY)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        u8::try_from(num!(f64::NEG_INFINITY)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        u8::try_from(num!(f64::NAN)),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));

    #[cfg(feature = "decimal")]
    {
        use std::str::FromStr;

        use rust_decimal::Decimal;

        // From Decimal: fract zero, to_u8 Some Ok else Out, fract Precision
        let int_dec = Number::from(Decimal::new(42, 0));
        assert_eq!(u8::try_from(int_dec).unwrap(), 42);

        let frac_dec = Number::from(Decimal::new(425, 1)); // 42.5
        assert!(matches!(
            u8::try_from(frac_dec),
            Err(TryFromNumberError::TypeMismatch { .. })
        ));

        let large_dec = Number::from(Decimal::new(256, 0));
        assert!(matches!(
            u8::try_from(large_dec),
            Err(TryFromNumberError::OutOfRange { .. })
        ));

        let neg_dec = Number::from(Decimal::new(-1, 0));
        assert!(matches!(
            u8::try_from(neg_dec),
            Err(TryFromNumberError::OutOfRange { .. })
        ));

        // Large mantissa > u8
        let large = Number::from(Decimal::from_str("256").unwrap());
        assert!(matches!(
            u8::try_from(large),
            Err(TryFromNumberError::OutOfRange { .. })
        ));
    }
}

#[test]
fn test_try_from_to_i8() {
    // For signed, min = i8::MIN = -128, max=127

    // From U64: <=127 Ok, > Out
    assert_eq!(i8::try_from(Number::from(127_u64)).unwrap(), 127);
    assert!(matches!(
        i8::try_from(Number::from(128_u64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // From I64: >= -128 <=127 Ok, else Out
    assert_eq!(i8::try_from(Number::from(-128_i64)).unwrap(), -128);
    assert_eq!(i8::try_from(Number::from(127_i64)).unwrap(), 127);
    assert!(matches!(
        i8::try_from(Number::from(-129_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        i8::try_from(Number::from(128_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // From U64: <=127 Ok, > Out
    assert_eq!(i8::try_from(Number::from(127_u64)).unwrap(), 127);
    assert!(matches!(
        i8::try_from(Number::from(128_u64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // From I64: >= -128 <=127 Ok, else Out
    assert_eq!(i8::try_from(Number::from(-128_i64)).unwrap(), -128);
    assert_eq!(i8::try_from(Number::from(127_i64)).unwrap(), 127);
    assert!(matches!(
        i8::try_from(Number::from(-129_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        i8::try_from(Number::from(128_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // From F64: similar
    assert_eq!(i8::try_from(num!(100.0f64)).unwrap(), 100);
    assert!(matches!(
        i8::try_from(num!(100.5f64)),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    assert_eq!(i8::try_from(num!(-128.0f64)).unwrap(), -128);
    assert!(matches!(
        i8::try_from(num!(127.5f64)),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    assert!(matches!(
        i8::try_from(num!(128.0f64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        i8::try_from(num!(-129.0f64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    #[cfg(feature = "decimal")]
    {
        // From Decimal: fract zero, to_i8 Some Ok else Out
        let int_dec = Number::from(Decimal::new(42, 0));
        assert_eq!(i8::try_from(int_dec).unwrap(), 42);

        let frac_dec = Number::from(Decimal::new(425, 1)); // 42.5
        assert!(matches!(
            i8::try_from(frac_dec),
            Err(TryFromNumberError::TypeMismatch { .. })
        ));

        let large_dec = Number::from(Decimal::new(128, 0));
        assert!(matches!(
            i8::try_from(large_dec),
            Err(TryFromNumberError::OutOfRange { .. })
        ));

        let neg_large_dec = Number::from(Decimal::new(-129, 0));
        assert!(matches!(
            i8::try_from(neg_large_dec),
            Err(TryFromNumberError::OutOfRange { .. })
        ));

        let min_dec = Number::from(Decimal::new(-128, 0));
        assert_eq!(i8::try_from(min_dec).unwrap(), -128);
        let max_dec = Number::from(Decimal::new(127, 0));
        assert_eq!(i8::try_from(max_dec).unwrap(), 127);
    }
}

#[test]
fn test_try_from_to_u16() {
    // Analogous to u8, but with max=65535
    // Test cases adjusted for larger range
    // For example, small integer types always fit within larger bounds
    // But for from I64: can hit >65535
    assert_eq!(u16::try_from(Number::from(65535_i64)).unwrap(), 65535);
    assert!(matches!(
        u16::try_from(Number::from(65536_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        u16::try_from(Number::from(-1_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    // From U64: <=65535 Ok, > Out
    assert_eq!(u16::try_from(Number::from(65535_u64)).unwrap(), 65535);
    assert!(matches!(
        u16::try_from(Number::from(65536_u64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    // Add more cases as needed
}

#[test]
fn test_try_from_to_i16() {
    // Similar to i8, but with min=-32768, max=32767
    assert_eq!(i16::try_from(Number::from(32767_u64)).unwrap(), 32767);
    assert!(matches!(
        i16::try_from(Number::from(32768_u64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert_eq!(i16::try_from(Number::from(-32768_i64)).unwrap(), -32768);
    assert!(matches!(
        i16::try_from(Number::from(-32769_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    // From F64: exact integer within range
    assert_eq!(i16::try_from(num!(1000.0f64)).unwrap(), 1000);
    assert!(matches!(
        i16::try_from(num!(1000.5f64)),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    assert!(matches!(
        i16::try_from(num!(32768.0f64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    // Add more cases as needed
}

#[test]
fn test_try_from_to_u32() {
    // Analogous to u8, but with max=4294967295
    assert_eq!(u32::try_from(Number::from(0_u64)).unwrap(), 0);
    assert_eq!(
        u32::try_from(Number::from(4294967295_u64)).unwrap(),
        4294967295
    );
    assert_eq!(
        u32::try_from(Number::from(2147483647_i64)).unwrap(),
        2147483647
    );
    assert!(matches!(
        u32::try_from(Number::from(-1_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        u32::try_from(Number::from(4294967296_u64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
}

#[test]
fn test_try_from_to_i32() {
    // Similar to i8, but with min=-2147483648, max=2147483647
    assert_eq!(
        i32::try_from(Number::from(2147483647_u64)).unwrap(),
        2147483647
    );
    assert!(matches!(
        i32::try_from(Number::from(2147483648_u64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert_eq!(
        i32::try_from(Number::from(-2147483648_i64)).unwrap(),
        -2147483648
    );
    assert!(matches!(
        i32::try_from(Number::from(-2147483649_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    // From F64: exact integer within range
    assert_eq!(i32::try_from(num!(1000.0f64)).unwrap(), 1000);
    assert!(matches!(
        i32::try_from(num!(1000.5f64)),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    assert!(matches!(
        i32::try_from(num!(2147483648.0f64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    // Add more cases as needed
}

#[test]
fn test_try_from_to_u64() {
    // Analogous to u8, but with max=18446744073709551615
    assert_eq!(u64::try_from(Number::from(0_u64)).unwrap(), 0);
    assert_eq!(
        u64::try_from(Number::from(18446744073709551615_u64)).unwrap(),
        18446744073709551615
    );
    assert_eq!(
        u64::try_from(Number::from(9223372036854775807_i64)).unwrap(),
        9223372036854775807
    );
    assert!(matches!(
        u64::try_from(Number::from(-1_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    // From F64: exact integer within range, considering f64 precision
    assert_eq!(u64::try_from(num!(1000.0f64)).unwrap(), 1000);
    assert!(matches!(
        u64::try_from(num!(1000.5f64)),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    // Large values where f64 loses precision
    let large_u64 = Number::from(9007199254740993_u64); // 2^53 +1
    assert!(matches!(
        f64::try_from(large_u64),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    // For u64::try_from(F64), need F64 that represents integer exactly
    let f64_large_int = num!(9007199254740992.0f64); // 2^53, exact
    assert_eq!(u64::try_from(f64_large_int).unwrap(), 9007199254740992);
    let f64_large_loss = num!(42.5f64); // Non-integer value
    assert!(matches!(
        u64::try_from(f64_large_loss),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
}

#[test]
fn test_try_from_to_i64() {
    // From small signed/unsigned: Ok always
    assert_eq!(
        i64::try_from(Number::from(i64::from(i32::MIN))).unwrap(),
        i32::MIN as i64
    );
    assert_eq!(
        i64::try_from(Number::from(u64::from(u32::MAX))).unwrap(),
        u32::MAX as i64
    );
    assert_eq!(i64::try_from(Number::from(i64::MIN)).unwrap(), i64::MIN);
    // From U64: <= i64::MAX Ok, > Out
    assert_eq!(i64::try_from(Number::from(0_u64)).unwrap(), 0);
    assert_eq!(
        i64::try_from(Number::from(i64::MAX as u64)).unwrap(),
        i64::MAX
    );
    assert!(matches!(
        i64::try_from(Number::from(i64::MAX as u64 + 1)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // From F64: validate_signed

    // Similar for F64
    let f64_large = Number::from(i64::MAX as f64 + 1.0);
    assert!(matches!(
        i64::try_from(f64_large),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    let f64_prec_loss = num!(42.5f64); // Non-integer value
    assert!(matches!(
        i64::try_from(f64_prec_loss),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));

    // For boundary where as_int == i64::MAX but f >= 2^63
    let boundary = Number::from(9223372036854775808.0f64); // 2^63
    assert!(matches!(
        i64::try_from(boundary),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    let neg_boundary = Number::from(-9223372036854775809.0f64); // rounds to -2^63
    assert_eq!(i64::try_from(neg_boundary).unwrap(), i64::MIN);

    let far_below_min = Number::from(-1e20f64);
    assert!(matches!(
        i64::try_from(far_below_min),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    #[cfg(feature = "decimal")]
    {
        let dec_int = Number::from(Decimal::new(42, 0));
        assert_eq!(i64::try_from(dec_int).unwrap(), 42);

        let dec_frac = Number::from(Decimal::new(42, 1));
        assert!(matches!(
            i64::try_from(dec_frac),
            Err(TryFromNumberError::TypeMismatch { .. })
        ));

        let dec_large = Number::from(Decimal::MAX);
        assert!(matches!(
            i64::try_from(dec_large),
            Err(TryFromNumberError::OutOfRange { .. })
        ));

        let dec_min = Number::from(Decimal::MIN);
        assert!(matches!(
            i64::try_from(dec_min),
            Err(TryFromNumberError::OutOfRange { .. })
        ));
    }
}

#[test]
fn test_try_from_to_f64() {
    // From U64/I64/U64/I64: for small exact Ok, for large check if exact

    // U64: <=2^53 Ok if exact, else Precision
    let u64_exact = Number::from(9007199254740992_u64); // 2^53
    assert_eq!(f64::try_from(u64_exact).unwrap(), 9007199254740992.0); // 2^53
    let u64_loss = Number::from(9007199254740993_u64); // 2^53 + 1
    assert!(matches!(
        f64::try_from(u64_loss),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    let u64_large_exact = Number::from(9007199254740994_u64); // 2^53 + 2
    assert_eq!(f64::try_from(u64_large_exact).unwrap(), 9007199254740994.0); // 2^53 + 2

    // I64 similar, including negative
    let i64_large = Number::from(9007199254740991_i64); // 2^53 - 1
    assert_eq!(f64::try_from(i64_large).unwrap(), 9007199254740991.0); // 2^53 - 1
    let i64_neg_loss = Number::from(-9007199254740993_i64); // -(2^53 + 1)
    assert!(matches!(
        f64::try_from(i64_neg_loss),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));

    // From F64: always Ok
    assert_eq!(
        f64::try_from(Number::from(std::f64::consts::PI)).unwrap(),
        std::f64::consts::PI
    );
    assert!(f64::try_from(num!(f64::NAN)).is_ok());
    assert!(f64::try_from(num!(f64::INFINITY)).is_ok());

    // Only F64 supported now

    #[cfg(feature = "decimal")]
    {
        // From Decimal: always Ok, since Decimal::to_f64 always succeeds
        let dec = Number::from(Decimal::from_str("1.234567890123456789").unwrap());
        assert!(f64::try_from(dec).is_ok());
    }
}

#[test]
fn test_try_from_to_decimal() {
    #[cfg(feature = "decimal")]
    {
        use std::str::FromStr;

        use rust_decimal::Decimal;

        // From integer variants: always Ok
        assert_eq!(
            Decimal::try_from(Number::from(42_u64)).unwrap(),
            Decimal::from(42)
        );
        assert_eq!(
            Decimal::try_from(Number::from(-42_i64)).unwrap(),
            Decimal::from(-42)
        );
        assert_eq!(
            Decimal::try_from(Number::from(u64::MAX)).unwrap(),
            Decimal::from(u64::MAX)
        );
        assert_eq!(
            Decimal::try_from(Number::from(i64::MIN)).unwrap(),
            Decimal::from(i64::MIN)
        );

        // From F64: finite Ok if try_from ok, inf/nan Incompatible
        let f64_ok = Number::from(std::f64::consts::E);
        assert!(Decimal::try_from(f64_ok).is_ok());

        let f64_nan = num!(f64::NAN);
        assert!(matches!(
            Decimal::try_from(f64_nan),
            Err(TryFromNumberError::TypeMismatch { .. })
        ));
        let f64_inf = num!(f64::INFINITY);
        assert!(matches!(
            Decimal::try_from(f64_inf),
            Err(TryFromNumberError::TypeMismatch { .. })
        ));
        let f64_neg_inf = num!(f64::NEG_INFINITY);
        assert!(matches!(
            Decimal::try_from(f64_neg_inf),
            Err(TryFromNumberError::TypeMismatch { .. })
        ));

        // From Decimal: always Ok
        let dec = Number::from(Decimal::from_str("1.234").unwrap());
        assert_eq!(
            Decimal::try_from(dec).unwrap(),
            Decimal::from_str("1.234").unwrap()
        );

        // Precision loss for float to decimal if not exact.
        let f64_large = num!(f64::MAX);
        assert!(matches!(
            Decimal::try_from(f64_large),
            Err(TryFromNumberError::TypeMismatch { .. })
        )); // Too large for Decimal
    }
}

#[test]
fn test_special_float_values() {
    let nan = num!(f64::NAN);
    let inf = num!(f64::INFINITY);
    let neg_inf = num!(f64::NEG_INFINITY);

    // To integer types
    assert!(matches!(
        i32::try_from(nan.clone()),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    assert!(matches!(
        u64::try_from(inf.clone()),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert!(matches!(
        i8::try_from(neg_inf.clone()),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // To f64 types
    assert!(f64::try_from(nan.clone()).is_ok());
    assert!(f64::try_from(inf.clone()).is_ok());
    assert!(f64::try_from(neg_inf.clone()).is_ok());

    #[cfg(feature = "decimal")]
    {
        assert!(matches!(
            Decimal::try_from(nan),
            Err(TryFromNumberError::TypeMismatch { .. })
        ));
        assert!(matches!(
            Decimal::try_from(inf),
            Err(TryFromNumberError::TypeMismatch { .. })
        ));
        assert!(matches!(
            Decimal::try_from(neg_inf),
            Err(TryFromNumberError::TypeMismatch { .. })
        ));
    }
}

#[test]
fn test_precision_loss_detection() {
    // Integer to f64 precision (large integers)
    let u64_precise = Number::from(9007199254740993_u64); // 2^53 + 1, cannot be exactly represented in f64
    assert!(matches!(
        f64::try_from(u64_precise.clone()),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));

    // Float to integer precision loss
    let f64_frac = num!(42.5f64);
    assert!(matches!(
        i32::try_from(f64_frac),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));

    #[cfg(feature = "decimal")]
    {
        let dec_frac = Number::from(Decimal::new(425, 1)); // 42.5
        assert!(matches!(
            u8::try_from(dec_frac),
            Err(TryFromNumberError::TypeMismatch { .. })
        ));
    }
}

#[test]
fn test_boundary_conditions() {
    // Integer boundaries
    assert_eq!(u8::try_from(Number::from(255_u64)).unwrap(), 255);
    assert!(matches!(
        u8::try_from(Number::from(256_u64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert_eq!(i8::try_from(Number::from(127_i64)).unwrap(), 127);
    assert!(matches!(
        i8::try_from(Number::from(128_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert_eq!(i8::try_from(Number::from(-128_i64)).unwrap(), -128);
    assert!(matches!(
        i8::try_from(Number::from(-129_i64)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // Float to integer boundaries
    let f64_max_i64 = num!(i64::MAX as f64);
    let result = i64::try_from(f64_max_i64);
    assert!(result.is_ok() || matches!(result, Err(TryFromNumberError::OutOfRange { .. })));

    let f64_min_i64 = num!(i64::MIN as f64);
    let result = i64::try_from(f64_min_i64);
    assert!(result.is_ok() || matches!(result, Err(TryFromNumberError::OutOfRange { .. })));

    // Clamp boundaries
    let at_clamp_boundary = Number::from(18446744073709551616.0f64); // 2^64
    assert!(matches!(
        u64::try_from(at_clamp_boundary),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    let at_neg_clamp_boundary = Number::from(-9223372036854775809.0f64); // rounds to -2^63
    assert_eq!(i64::try_from(at_neg_clamp_boundary).unwrap(), i64::MIN);

    let clearly_out_of_range = Number::from(-1e20f64);
    assert!(matches!(
        i64::try_from(clearly_out_of_range),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
}

#[test]
fn test_subnormal_and_small_values() {
    // Subnormal float values
    let subnormal_f64 = num!(f64::MIN_POSITIVE / 2.0);
    assert!(matches!(
        u8::try_from(subnormal_f64.clone()),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));

    let subnormal_f64 = num!(f64::MIN_POSITIVE / 2.0);
    assert!(matches!(
        i32::try_from(subnormal_f64),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));

    // Very small numbers near zero
    let tiny_values = [
        f64::MIN_POSITIVE,
        f64::MIN_POSITIVE * 2.0,
        f64::MIN_POSITIVE / 2.0, // Subnormal
        1e-300,
        1e-310,
    ];

    for &val in &tiny_values {
        let f64_num = Number::from(val);
        // Test that small values are handled correctly
        let f64_result = f64::try_from(f64_num);
        assert!(f64_result.is_ok());
        if let Ok(f64_val) = f64_result {
            assert!(f64_val >= 0.0);
        }
    }
}

#[test]
fn test_negative_zero_handling() {
    let neg_zero_f64 = num!(-0.0f64);
    assert_eq!(i32::try_from(neg_zero_f64.clone()).unwrap(), 0i32);
    assert_eq!(u32::try_from(neg_zero_f64).unwrap(), 0u32);
}

#[test]
fn test_cross_type_conversions() {
    // Float to unsigned
    let f64_val = Number::from(std::f64::consts::PI);
    assert!(matches!(
        u8::try_from(f64_val.clone()),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    assert!(matches!(
        i32::try_from(f64_val),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));

    // Negative to unsigned
    let neg_i32 = Number::from(-1000_i64);
    assert!(matches!(
        u32::try_from(neg_i32),
        Err(TryFromNumberError::OutOfRange { .. })
    ));

    // Unsigned to signed
    assert_eq!(
        i8::try_from(Number::from(u64::from(i8::MAX as u32))).unwrap(),
        i8::MAX
    );
    assert!(matches!(
        i8::try_from(Number::from(u64::from(i8::MAX as u32 + 1))),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert_eq!(
        i16::try_from(Number::from(u64::from(i16::MAX as u32))).unwrap(),
        i16::MAX
    );
    assert!(matches!(
        i16::try_from(Number::from(u64::from(i16::MAX as u32 + 1))),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert_eq!(
        i32::try_from(Number::from(u64::from(i32::MAX as u32))).unwrap(),
        i32::MAX
    );
    assert!(matches!(
        i32::try_from(Number::from(u64::from(i32::MAX as u32 + 1))),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
    assert_eq!(
        i64::try_from(Number::from(i64::MAX as u64)).unwrap(),
        i64::MAX
    );
    assert!(matches!(
        i64::try_from(Number::from(i64::MAX as u64 + 1)),
        Err(TryFromNumberError::OutOfRange { .. })
    ));
}

#[cfg(feature = "decimal")]
#[test]
fn test_decimal_edge_cases() {
    use std::str::FromStr;

    use rust_decimal::Decimal;

    // Decimal zero with different scales
    let zero_scale_0 = Number::from(Decimal::new(0, 0));
    let zero_scale_10 = Number::from(Decimal::new(0, 10));
    let zero_scale_28 = Number::from(Decimal::new(0, 28));
    assert_eq!(u8::try_from(zero_scale_0).unwrap(), 0);
    assert_eq!(u8::try_from(zero_scale_10).unwrap(), 0);
    assert_eq!(u8::try_from(zero_scale_28).unwrap(), 0);

    // Decimal one with different scales
    let one_scale_0 = Number::from(Decimal::new(1, 0)); // 1
    let one_scale_1 = Number::from(Decimal::new(1, 1)); // 0.1
    let one_scale_2 = Number::from(Decimal::new(1, 2)); // 0.01
    assert_eq!(u8::try_from(one_scale_0).unwrap(), 1);
    assert!(matches!(
        u8::try_from(one_scale_1),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    assert!(matches!(
        u8::try_from(one_scale_2),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));

    // Decimal parsing from floats with edge values
    let float_max = num!(f64::MAX);
    assert!(matches!(
        Decimal::try_from(float_max),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));

    let float_min = num!(f64::MIN);
    assert!(matches!(
        Decimal::try_from(float_min),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));

    // Test Decimal values that require exact representation
    let exact_tenth = Number::from(Decimal::from_str("0.1").unwrap());
    let f64_tenth = f64::try_from(exact_tenth).unwrap();
    assert!((f64_tenth - 0.1f64).abs() < f64::EPSILON * 10.0);

    // Test conversion accuracy for financial values
    let financial = Number::from(Decimal::from_str("123.45").unwrap());
    assert!(matches!(
        u8::try_from(financial.clone()),
        Err(TryFromNumberError::TypeMismatch { .. })
    ));
    let f64_financial = f64::try_from(financial).unwrap();
    assert_eq!(f64_financial, 123.45);
}