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
//! Approximate equality property tests for the Number type.
//!
//! Tests fundamental approximate equality properties:
//! - Reflexive: a.approx_eq(a, epsilon, 0.0) for any epsilon
//! - Symmetric: if a.approx_eq(b, epsilon, 0.0), then b.approx_eq(a, epsilon,
//!   0.0)
//! - Epsilon behavior: scaling with tolerance values
//! - Special value handling: NaN, infinity, zero
//! - Cross-type consistency

use uninum::{Number, num};

/// Generate a comprehensive set of test numbers covering all variants and edge
/// cases
fn generate_test_numbers() -> Vec<Number> {
    let numbers = vec![
        // Basic integers
        Number::from(0_u64),
        Number::from(65536_u64),
        Number::from(4294967295_u64),
        Number::from(-2147483648_i64),
        Number::from(0_i64),
        Number::from(2147483647_i64),
        Number::from(0_u64),
        Number::from(4294967296_u64),
        Number::from(u64::MAX),
        Number::from(i64::MIN),
        Number::from(0_i64),
        Number::from(i64::MAX),
        // Floats
        num!(0.0f64),
        num!(-0.0f64),
        num!(1.0f64),
        num!(-1.0f64),
        Number::from(std::f64::consts::PI),
        Number::from(-std::f64::consts::PI),
        num!(f64::INFINITY),
        num!(f64::NEG_INFINITY),
        num!(f64::NAN),
        num!(0.0f64),
        num!(-0.0f64),
        num!(1.0f64),
        num!(-1.0f64),
        Number::from(std::f64::consts::PI),
        Number::from(-std::f64::consts::PI),
        num!(f64::INFINITY),
        num!(f64::NEG_INFINITY),
        num!(f64::NAN),
        // Cross-type equivalents
        Number::from(42_u64),
        Number::from(42_i64),
        num!(42.0f64),
        num!(42.0f64),
    ];

    #[cfg(feature = "decimal")]
    let numbers = {
        use rust_decimal::Decimal;
        let mut numbers = numbers;
        numbers.extend([
            Number::from(Decimal::new(0, 0)),
            Number::from(Decimal::new(1, 0)),
            Number::from(Decimal::new(42, 0)),
            Number::from(Decimal::new(314159, 5)),  // 3.14159
            Number::from(Decimal::new(-314159, 5)), // -3.14159
        ]);
        numbers
    };

    numbers
}

#[test]
fn test_approx_eq_reflexive_property() {
    let numbers = generate_test_numbers();
    let epsilon = 1e-10;

    // Test reflexive property: a.approx_eq(a, epsilon, 0.0) should always be true
    for a in &numbers {
        assert!(
            a.approx_eq(a, epsilon, 0.0),
            "Approximate equality reflexive property violated for {a:?}"
        );
    }
}

#[test]
fn test_approx_eq_symmetric_property() {
    let numbers = generate_test_numbers();
    let epsilon = 1e-10;

    // Test symmetric property: if a.approx_eq(b, epsilon, 0.0), then b.approx_eq(a,
    // epsilon, 0.0)
    for a in &numbers {
        for b in &numbers {
            if a.approx_eq(b, epsilon, 0.0) {
                assert!(
                    b.approx_eq(a, epsilon, 0.0),
                    "Approximate equality symmetric property violated: {a:?}.approx_eq({b:?}, \
                     {epsilon}, 0.0) is true but {b:?}.approx_eq({a:?}, {epsilon}, 0.0) is false"
                );
            }
        }
    }
}

#[test]
fn test_approx_eq_with_zero_epsilon() {
    let numbers = generate_test_numbers();

    // Test that approx_eq with epsilon=0 behaves like exact equality
    for a in &numbers {
        for b in &numbers {
            let exact_eq = a == b;
            let approx_eq = a.approx_eq(b, 0.0, 0.0);
            assert_eq!(
                exact_eq, approx_eq,
                "approx_eq with epsilon=0 should match exact equality for {a:?} and {b:?}"
            );
        }
    }
}

#[test]
fn test_approx_eq_epsilon_scaling() {
    let test_cases = vec![
        (num!(1.0f64), num!(1.1f64)),
        (num!(1.0f64), num!(1.01f64)),
        (num!(1.0f64), num!(1.001f64)),
        (Number::from(100_u64), Number::from(110_u64)),
        (Number::from(100_u64), Number::from(101_u64)),
        (num!(0.1f64), num!(0.11f64)),
    ];

    for (a, b) in test_cases {
        // With very small epsilon, should not be approximately equal
        assert!(
            !a.approx_eq(&b, 1e-10, 0.0),
            "{a:?} and {b:?} should not be approximately equal with epsilon 1e-10"
        );

        // With large epsilon, should be approximately equal
        // For integers with larger differences, use appropriately large epsilon
        let epsilon = match (&a, &b) {
            (a_val, b_val)
                if a_val.try_get_u64() == Some(100) && b_val.try_get_u64() == Some(110) =>
            {
                10.0
            }
            (a_val, b_val)
                if a_val.try_get_u64() == Some(100) && b_val.try_get_u64() == Some(101) =>
            {
                1.0
            }
            _ => 1.0,
        };
        assert!(
            a.approx_eq(&b, epsilon, 0.0),
            "{a:?} and {b:?} should be approximately equal with epsilon {epsilon}"
        );
    }
}

#[test]
fn test_approx_eq_special_values_properties() {
    // Test NaN behavior
    let nan_f64_2 = num!(f64::NAN);
    let nan_f64 = num!(f64::NAN);
    let normal = num!(1.0f64);

    // NaN should be approximately equal to itself and other NaN values
    assert!(
        nan_f64_2.approx_eq(&nan_f64_2, 1e-10, 0.0),
        "NaN should be approximately equal to itself"
    );
    assert!(
        nan_f64.approx_eq(&nan_f64, 1e-10, 0.0),
        "NaN should be approximately equal to itself"
    );
    assert!(
        nan_f64_2.approx_eq(&nan_f64, 1e-10, 0.0),
        "Different NaN values should be approximately equal"
    );

    // NaN should not be approximately equal to normal values
    assert!(
        !nan_f64_2.approx_eq(&normal, 1e-10, 0.0),
        "NaN should not be approximately equal to normal values"
    );
    assert!(
        !normal.approx_eq(&nan_f64_2, 1e-10, 0.0),
        "Normal values should not be approximately equal to NaN"
    );

    // Test infinity behavior
    let pos_inf = num!(f64::INFINITY);
    let neg_inf = num!(f64::NEG_INFINITY);

    // Infinity should be approximately equal to itself
    assert!(
        pos_inf.approx_eq(&pos_inf, 1e-10, 0.0),
        "Positive infinity should be approximately equal to itself"
    );
    assert!(
        neg_inf.approx_eq(&neg_inf, 1e-10, 0.0),
        "Negative infinity should be approximately equal to itself"
    );

    // Different infinities should not be approximately equal
    assert!(
        !pos_inf.approx_eq(&neg_inf, 1e-10, 0.0),
        "Positive and negative infinity should not be approximately equal"
    );

    // Infinity should not be approximately equal to normal values
    assert!(
        !pos_inf.approx_eq(&normal, 1e-10, 0.0),
        "Infinity should not be approximately equal to normal values"
    );
    assert!(
        !normal.approx_eq(&pos_inf, 1e-10, 0.0),
        "Normal values should not be approximately equal to infinity"
    );
}

#[test]
fn test_approx_eq_cross_type_consistency() {
    // Test that approximate equality works consistently across different numeric
    // types
    let value_sets = vec![
        // Integer value 42 in different types
        vec![
            Number::from(42_u64),
            Number::from(42_i64),
            Number::from(42_u64),
            Number::from(42_i64),
            num!(42.0f64),
            num!(42.0f64),
        ],
        // Zero in different types
        vec![
            Number::from(0_u64),
            Number::from(0_i64),
            Number::from(0_u64),
            Number::from(0_i64),
            num!(0.0f64),
            num!(-0.0f64),
            num!(0.0f64),
            num!(-0.0f64),
        ],
    ];

    #[cfg(feature = "decimal")]
    let value_sets = {
        use rust_decimal::Decimal;
        let mut value_sets = value_sets;
        // Add decimal values to the sets
        value_sets[0].push(Number::from(Decimal::new(42, 0)));
        value_sets[1].push(Number::from(Decimal::new(0, 0)));
        value_sets
    };

    let epsilon = 1e-10;

    for set in value_sets {
        // All values in a set should be approximately equal to each other
        for a in &set {
            for b in &set {
                assert!(
                    a.approx_eq(b, epsilon, 0.0),
                    "Cross-type approximate equality failed: {a:?} and {b:?} should be \
                     approximately equal"
                );
            }
        }
    }
}

#[test]
fn test_approx_eq_tolerance_boundaries() {
    // Test behavior at tolerance boundaries
    let a = num!(1.0f64);
    let b = num!(1.0 + 1e-10);

    // Should not be equal with smaller epsilon
    assert!(
        !a.approx_eq(&b, 1e-11, 0.0),
        "Should not be approximately equal with epsilon smaller than difference"
    );

    // Should be equal with larger epsilon
    assert!(
        a.approx_eq(&b, 1e-9, 0.0),
        "Should be approximately equal with epsilon larger than difference"
    );

    // Should be equal with exactly matching epsilon (use slightly larger to account
    // for floating point precision)
    assert!(
        a.approx_eq(&b, 1.1e-10, 0.0),
        "Should be approximately equal with epsilon slightly larger than difference"
    );
}

#[test]
fn test_approx_eq_for_f64_precision_comparison() {
    // Test f64 precision comparison
    let f64_val = num!(std::f64::consts::PI);
    let f64_val2 = num!(std::f64::consts::PI);

    // Direct equality should work for identical values
    assert_eq!(f64_val, f64_val2);

    // And approx_eq should work with any reasonable epsilon
    let epsilon = 1e-15;
    assert!(f64_val.approx_eq(&f64_val2, epsilon, 0.0));

    // Test with slightly different precision values
    let f64_precise = Number::from(std::f64::consts::PI);
    let f64_close = Number::from(std::f64::consts::PI);

    // These should be approximately equal with reasonable epsilon
    assert!(f64_precise.approx_eq(&f64_close, 1e-15, 0.0));
}

#[test]
fn test_approx_eq_cross_type_all_combinations() {
    // Test approx_eq works for all cross-type combinations
    let int_val = Number::from(42_i64);
    let float_val = num!(42.0f64);
    let f64_val2 = num!(42.0f64);

    assert!(int_val.approx_eq(&float_val, 1e-10, 0.0));
    assert!(int_val.approx_eq(&f64_val2, 1e-6, 0.0));
    assert!(float_val.approx_eq(&f64_val2, 1e-6, 0.0));

    // Test with decimal if available
    #[cfg(feature = "decimal")]
    {
        use rust_decimal::Decimal;

        let decimal_val = Number::from(Decimal::new(42, 0));
        assert!(int_val.approx_eq(&decimal_val, 1e-10, 0.0));
        assert!(float_val.approx_eq(&decimal_val, 1e-10, 0.0));
        assert!(f64_val2.approx_eq(&decimal_val, 1e-6, 0.0));
    }
}

#[test]
fn test_approx_eq_epsilon_behavior_validation() {
    // Test that epsilon works correctly
    let a = num!(1.0f64);
    let b = num!(1.1f64);

    // Should not be equal with small epsilon
    assert!(!a.approx_eq(&b, 1e-10, 0.0));

    // Should be equal with large epsilon
    assert!(a.approx_eq(&b, 0.2, 0.0));

    // Test edge case - exactly at epsilon boundary
    let c = num!(1.0f64);
    let d = num!(1.0 + 1e-10);
    assert!(c.approx_eq(&d, 1e-9, 0.0)); // Use larger epsilon for boundary test
}

#[test]
fn test_approx_eq_special_float_validation() {
    // Test with special float values (validation-specific)
    let nan1 = num!(f64::NAN);
    let nan2 = num!(f64::NAN);
    let inf = num!(f64::INFINITY);
    let neg_inf = num!(f64::NEG_INFINITY);

    // NaN comparisons should work (different from IEEE 754)
    assert!(nan1.approx_eq(&nan2, 1e-10, 0.0));

    // Infinity comparisons
    assert!(inf.approx_eq(&inf, 1e-10, 0.0));
    assert!(neg_inf.approx_eq(&neg_inf, 1e-10, 0.0));
    assert!(!inf.approx_eq(&neg_inf, 1e-10, 0.0));
}

#[test]
fn test_approx_eq_with_different_number_magnitudes() {
    // Test approximate equality with numbers of different magnitudes
    let small = num!(1e-10f64);
    let large = num!(1e10f64);
    let zero = num!(0.0f64);

    // Small numbers should not be approximately equal to large numbers
    assert!(!small.approx_eq(&large, 1e-5, 0.0));

    // But with very large epsilon, they might be
    assert!(small.approx_eq(&large, 1e11, 0.0));

    // Zero should be approximately equal to very small numbers with appropriate
    // epsilon
    assert!(zero.approx_eq(&small, 1e-9, 0.0));
    assert!(!zero.approx_eq(&small, 1e-11, 0.0));
}

#[test]
fn test_approx_eq_integer_precision() {
    // Test approximate equality with integers that have precision differences when
    // converted to floats
    let int_val = Number::from(9007199254740993_u64); // 2^53 + 1, not exactly representable in f64
    let float_val = num!(9007199254740993.0f64);

    // These should not be exactly equal due to precision loss
    if int_val != float_val {
        // But they should be approximately equal with appropriate epsilon
        assert!(
            int_val.approx_eq(&float_val, 2.0, 0.0),
            "Large integer and its float representation should be approximately equal"
        );
    }
}

// ============================================================================
// New tests for relative approximation functionality
// ============================================================================

#[test]
fn test_approx_eq_rel_basic_functionality() {
    // Test basic functionality of approx_eq
    let a = num!(1.0f64);
    let b = num!(1.1f64);

    // Should not be equal with small tolerances
    assert!(!a.approx_eq(&b, 1e-10, 1e-10));

    // Should be equal with appropriate relative tolerance (10% difference)
    assert!(a.approx_eq(&b, 0.0, 0.15)); // 15% relative tolerance

    // Should be equal with appropriate absolute tolerance
    assert!(a.approx_eq(&b, 0.2, 0.0)); // 0.2 absolute tolerance

    // Should be equal with combined tolerances
    assert!(a.approx_eq(&b, 0.05, 0.05)); // Either tolerance sufficient
}

#[test]
fn test_approx_eq_rel_small_numbers() {
    // Test that both tolerances contribute additively for small numbers
    let a = num!(1e-10f64);
    let b = num!(2e-10f64);

    // 100% relative difference, but tiny absolute difference
    assert!(a.approx_eq(&b, 1.5e-10, 0.5)); // Combined tolerances sufficient
    assert!(!a.approx_eq(&b, 5e-11, 0.2)); // Combined tolerances insufficient
}

#[test]
fn test_approx_eq_rel_large_numbers() {
    // Test that relative tolerance dominates for large numbers
    let a = num!(1000000.0f64);
    let b = num!(1000001.0f64);

    // Tiny relative difference (0.0001%), but significant absolute difference
    assert!(a.approx_eq(&b, 0.0, 1e-5)); // Relative tolerance dominates
    assert!(!a.approx_eq(&b, 0.5, 0.0)); // Absolute tolerance insufficient
    assert!(a.approx_eq(&b, 0.1, 1e-5)); // Combined tolerances
}

#[test]
fn test_approx_eq_rel_financial_examples() {
    // Test financial calculations with appropriate tolerances
    let price1 = num!(19.99f64);
    let price2 = num!(19.991f64);

    // 1 cent absolute tolerance, 0.1% relative tolerance
    assert!(price1.approx_eq(&price2, 0.01, 0.001));
    assert!(!price1.approx_eq(&price2, 0.0001, 0.00001)); // Combined tolerances insufficient

    // Currency rounding scenarios
    let amount1 = num!(1234.567f64);
    let amount2 = num!(1234.569f64);
    assert!(amount1.approx_eq(&amount2, 0.01, 0.0)); // 1 cent tolerance

    // Interest rate calculations (basis points)
    let rate1 = num!(0.0525f64); // 5.25%
    let rate2 = num!(0.052501f64); // 5.2501%
    assert!(rate1.approx_eq(&rate2, 0.0001, 0.001)); // 1 basis point absolute,
    // 0.1% relative
}

#[test]
fn test_approx_eq_rel_scientific_calculations() {
    // Test scientific calculations with pure relative tolerance
    let measurement1 = num!(1.23456789e10f64);
    let measurement2 = num!(1.23456790e10f64);

    // Pure relative comparison (typical for scientific measurements)
    assert!(measurement1.approx_eq(&measurement2, 0.0, 1e-8));
    assert!(!measurement1.approx_eq(&measurement2, 0.0, 1e-9));

    // Very large numbers
    let big1 = num!(6.022e23f64); // Avogadro's number
    let big2 = num!(6.023e23f64);
    assert!(big1.approx_eq(&big2, 0.0, 0.002)); // 0.2% relative tolerance
    assert!(!big1.approx_eq(&big2, 0.0, 0.0001)); // 0.01% relative tolerance
}

#[test]
fn test_approx_eq_rel_special_values() {
    let normal = num!(1.0f64);
    let nan = num!(f64::NAN);
    let pos_inf = num!(f64::INFINITY);
    let neg_inf = num!(f64::NEG_INFINITY);

    // NaN behavior
    assert!(nan.approx_eq(&nan, 1e-10, 1e-10));
    assert!(!nan.approx_eq(&normal, 1e-10, 1e-10));
    assert!(!normal.approx_eq(&nan, 1e-10, 1e-10));

    // Infinity behavior
    assert!(pos_inf.approx_eq(&pos_inf, 1e-10, 1e-10));
    assert!(neg_inf.approx_eq(&neg_inf, 1e-10, 1e-10));
    assert!(!pos_inf.approx_eq(&neg_inf, 1e-10, 1e-10));

    // Mixed infinity and finite
    assert!(!pos_inf.approx_eq(&normal, 1e-10, 1e-10));
    assert!(!normal.approx_eq(&pos_inf, 1e-10, 1e-10));
    assert!(!neg_inf.approx_eq(&normal, 1e-10, 1e-10));
    assert!(!normal.approx_eq(&neg_inf, 1e-10, 1e-10));
}

#[test]
fn test_approx_eq_rel_zero_handling() {
    let zero = num!(0.0f64);
    let small_pos = num!(1e-10f64);
    let small_neg = num!(-1e-10f64);

    // Zero with small numbers - both tolerances contribute additively
    assert!(zero.approx_eq(&small_pos, 2e-10, 1.0)); // Large relative tolerance, sufficient absolute
    assert!(!zero.approx_eq(&small_pos, 5e-11, 0.3)); // Both tolerances insufficient combined

    // Negative and positive zero
    let neg_zero = num!(-0.0f64);
    assert!(zero.approx_eq(&neg_zero, 0.0, 0.0));

    // Small numbers near zero
    assert!(small_pos.approx_eq(&small_neg, 3e-10, 0.0));
    assert!(!small_pos.approx_eq(&small_neg, 1e-10, 0.0));
}

#[test]
fn test_approx_eq_rel_pure_absolute_mode() {
    // Test pure absolute mode (rel_tolerance = 0.0)
    let a = num!(1000.0f64);
    let b = num!(1001.0f64);

    assert!(a.approx_eq(&b, 2.0, 0.0)); // Sufficient absolute tolerance
    assert!(!a.approx_eq(&b, 0.5, 0.0)); // Insufficient absolute tolerance

    // Should behave exactly like original approx_eq
    let c = num!(1.0f64);
    let d = num!(1.1f64);
    assert_eq!(c.approx_eq(&d, 0.2, 0.0), c.approx_eq(&d, 0.2, 0.0));
    assert_eq!(c.approx_eq(&d, 0.05, 0.0), c.approx_eq(&d, 0.05, 0.0));
}

#[test]
fn test_approx_eq_rel_pure_relative_mode() {
    // Test pure relative mode (abs_tolerance = 0.0)
    let a = num!(1000.0f64);
    let b = num!(1010.0f64);

    // 1% relative difference
    assert!(a.approx_eq(&b, 0.0, 0.02)); // 2% relative tolerance
    assert!(!a.approx_eq(&b, 0.0, 0.005)); // 0.5% relative tolerance

    // Test with different magnitude numbers
    let c = num!(10.0f64);
    let d = num!(10.1f64);
    // Same 1% relative difference
    assert!(c.approx_eq(&d, 0.0, 0.02)); // 2% relative tolerance
    assert!(!c.approx_eq(&d, 0.0, 0.005)); // 0.5% relative tolerance
}

#[test]
fn test_approx_eq_rel_symmetry_property() {
    // Test that approx_eq is symmetric: a.approx_eq(b) == b.approx_eq(a)
    let test_pairs = vec![
        (num!(1.0f64), num!(1.1f64)),
        (num!(1000.0f64), num!(1001.0f64)),
        (num!(1e-10f64), num!(2e-10f64)),
        (Number::from(42_u64), num!(42.1f64)),
        (Number::from(-100_i64), num!(-100.01f64)),
    ];

    let abs_tol = 0.1;
    let rel_tol = 0.01;

    for (a, b) in test_pairs {
        let a_to_b = a.approx_eq(&b, abs_tol, rel_tol);
        let b_to_a = b.approx_eq(&a, abs_tol, rel_tol);
        assert_eq!(
            a_to_b, b_to_a,
            "Symmetry property violated for {a:?} and {b:?}"
        );
    }
}

#[test]
fn test_approx_eq_rel_reflexivity_property() {
    // Test that a.approx_eq(a) is always true for any reasonable tolerances
    let numbers = generate_test_numbers();

    for num in numbers {
        assert!(
            num.approx_eq(&num, 1e-10, 1e-10),
            "Reflexivity property violated for {num:?}"
        );
        assert!(
            num.approx_eq(&num, 0.0, 1e-10),
            "Reflexivity property violated for {num:?} (pure relative)"
        );
        assert!(
            num.approx_eq(&num, 1e-10, 0.0),
            "Reflexivity property violated for {num:?} (pure absolute)"
        );
    }
}

#[test]
fn test_approx_eq_rel_cross_type_consistency() {
    // Test cross-type consistency with relative tolerance
    let int_val = Number::from(1000_u64);
    let float_val = num!(1000.0f64);
    let close_float = num!(1001.0f64);

    // Same value, different types - should be equal with any reasonable tolerance
    assert!(int_val.approx_eq(&float_val, 1e-10, 1e-10));

    // Close values across types
    assert!(int_val.approx_eq(&close_float, 2.0, 0.002)); // Combined tolerances sufficient
    assert!(!int_val.approx_eq(&close_float, 0.5, 0.0003)); // Combined tolerances insufficient

    #[cfg(feature = "decimal")]
    {
        use rust_decimal::Decimal;

        let decimal_val = Number::from(Decimal::new(1000, 0));
        let close_decimal = Number::from(Decimal::new(10001, 1)); // 1000.1

        assert!(int_val.approx_eq(&decimal_val, 1e-10, 1e-10));
        assert!(float_val.approx_eq(&decimal_val, 1e-10, 1e-10));
        assert!(int_val.approx_eq(&close_decimal, 0.2, 0.001));
    }
}

#[test]
fn test_approx_eq_rel_edge_cases() {
    // Test edge cases and boundary conditions

    // Very small relative tolerance
    let a = num!(1.0f64);
    let b = num!(1.0000001f64);
    assert!(a.approx_eq(&b, 0.0, 1e-6)); // Just above threshold
    assert!(!a.approx_eq(&b, 0.0, 1e-8)); // Just below threshold

    // Very small absolute tolerance
    let c = num!(1000.0f64);
    let d = num!(1000.0001f64);
    assert!(c.approx_eq(&d, 1e-3, 0.0)); // Just above threshold
    assert!(!c.approx_eq(&d, 1e-5, 0.0)); // Just below threshold

    // Equal tolerances, different dominating factors
    let abs_tol = 1e-6;
    let rel_tol = 1e-6;

    // For numbers around 1, both tolerances are similar
    let e = num!(1.0f64);
    let f = num!(1.0000005f64);
    assert!(e.approx_eq(&f, abs_tol, rel_tol));

    // For large numbers, relative tolerance dominates
    let g = num!(1e6f64);
    let h = num!(1e6 + 0.5);
    assert!(g.approx_eq(&h, abs_tol, rel_tol)); // Relative tolerance allows this
    assert!(!g.approx_eq(&h, abs_tol, 0.0)); // Pure absolute would not
}

#[test]
fn test_approx_eq_rel_performance_equivalence() {
    // Test that approx_eq(abs_tol, 0.0) behaves exactly like approx_eq(abs_tol)
    let test_cases = vec![
        (num!(1.0f64), num!(1.1f64), 0.2),
        (num!(1.0f64), num!(1.1f64), 0.05),
        (Number::from(100_u64), Number::from(101_u64), 2.0),
        (Number::from(100_u64), Number::from(105_u64), 3.0),
        (num!(0.0f64), num!(1e-10f64), 2e-10),
        (num!(f64::NAN), num!(1.0f64), 1e-6),
        (num!(f64::INFINITY), num!(f64::INFINITY), 1e-6),
    ];

    for (a, b, epsilon) in test_cases {
        let original = a.approx_eq(&b, epsilon, 0.0);
        let new_pure_abs = a.approx_eq(&b, epsilon, 0.0);
        assert_eq!(
            original, new_pure_abs,
            "approx_eq({epsilon}) should equal approx_eq({epsilon}, 0.0) for {a:?} and {b:?}"
        );
    }
}

#[test]
fn test_approx_eq_rel_tolerance_scaling() {
    // Test how relative tolerance scales with number magnitude
    let base_cases = vec![
        (1.0f64, 1.01f64),      // 1% difference
        (10.0f64, 10.1f64),     // 1% difference
        (100.0f64, 101.0f64),   // 1% difference
        (1000.0f64, 1010.0f64), // 1% difference
    ];

    let rel_tolerance = 0.015; // 1.5% tolerance

    for (base, modified) in base_cases {
        let a = num!(base);
        let b = num!(modified);

        // All should be approximately equal with 1.5% relative tolerance
        assert!(
            a.approx_eq(&b, 0.0, rel_tolerance),
            "1% difference should be within 1.5% tolerance for {base} and {modified}"
        );

        // All should not be approximately equal with 0.5% relative tolerance
        assert!(
            !a.approx_eq(&b, 0.0, 0.005),
            "1% difference should not be within 0.5% tolerance for {base} and {modified}"
        );
    }
}