num-valid 0.3.3

A robust numerical library providing validated types for real and complex numbers to prevent common floating-point errors like NaN propagation. Features a generic, layered architecture with support for native f64 and optional arbitrary-precision arithmetic.
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
//! Property-based tests using proptest for num-valid.
//!
//! These tests verify mathematical properties that should hold for all valid inputs,
//! not just specific example values. proptest generates hundreds of random inputs
//! and automatically shrinks failing cases to minimal reproducible examples.

use num::{One, Zero};
use num_valid::{
    RealScalar,
    backends::native64::validated::{ComplexNative64StrictFinite, RealNative64StrictFinite},
    functions::{
        ACos, ASin, ATan, Abs, ComplexScalarConstructors, ComplexScalarGetParts, Conjugate, Cos,
        CosH, Exp, Ln, Pow, Reciprocal, Sin, SinH, Sqrt, Tan, TanH,
    },
};
use proptest::prelude::*;

// =============================================================================
// STRATEGY DEFINITIONS
// =============================================================================
// Strategies define how to generate random values for testing.
// We exclude edge cases that would fail validation (NaN, Inf, subnormal).

/// Strategy for generating valid f64 values (finite, non-subnormal).
/// Excludes the "danger zone" near zero where subnormals live.
fn valid_f64() -> impl Strategy<Value = f64> {
    prop_oneof![
        // Normal positive range (avoiding subnormals)
        (f64::MIN_POSITIVE..1e100_f64),
        // Normal negative range
        (-1e100_f64..(-f64::MIN_POSITIVE)),
        // Zero is valid
        Just(0.0),
    ]
}

/// Strategy for positive f64 values (for sqrt, ln, etc.)
fn positive_f64() -> impl Strategy<Value = f64> {
    f64::MIN_POSITIVE..1e100_f64
}

/// Strategy for values in [-1, 1] (for asin, acos)
fn unit_interval() -> impl Strategy<Value = f64> {
    -1.0..=1.0_f64
}

/// Strategy for small positive values (for testing precision)
fn small_positive_f64() -> impl Strategy<Value = f64> {
    1e-10_f64..1e10_f64
}

/// Strategy for angles in a reasonable range (avoiding precision loss at extremes)
fn angle_radians() -> impl Strategy<Value = f64> {
    -100.0..100.0_f64
}

/// Strategy for angles in [-π/2 + ε, π/2 - ε] (for tan domain)
fn tan_safe_angle() -> impl Strategy<Value = f64> {
    let half_pi = std::f64::consts::FRAC_PI_2;
    let epsilon = 0.01; // Stay away from asymptotes
    (-half_pi + epsilon)..(half_pi - epsilon)
}

/// Strategy for generating validated RealNative64StrictFinite
fn validated_real() -> impl Strategy<Value = RealNative64StrictFinite> {
    valid_f64().prop_filter_map("must be valid", |x| {
        RealNative64StrictFinite::try_from_f64(x).ok()
    })
}

/// Strategy for positive validated reals
fn positive_validated_real() -> impl Strategy<Value = RealNative64StrictFinite> {
    positive_f64().prop_filter_map("must be positive valid", |x| {
        RealNative64StrictFinite::try_from_f64(x).ok()
    })
}

// =============================================================================
// ARITHMETIC PROPERTIES
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// Addition is commutative: a + b = b + a
    #[test]
    fn prop_add_commutative(a in validated_real(), b in validated_real()) {
        let sum1 = a + b;
        let sum2 = b + a;
        prop_assert_eq!(sum1, sum2);
    }

    /// Multiplication is commutative: a * b = b * a
    #[test]
    fn prop_mul_commutative(a in validated_real(), b in validated_real()) {
        let prod1 = a * b;
        let prod2 = b * a;
        prop_assert_eq!(prod1, prod2);
    }

    /// Addition identity: a + 0 = a
    #[test]
    fn prop_add_identity(a in validated_real()) {
        let zero = RealNative64StrictFinite::zero();
        let result = a + zero;
        prop_assert_eq!(result, a);
    }

    /// Multiplication identity: a * 1 = a
    #[test]
    fn prop_mul_identity(a in validated_real()) {
        let one = RealNative64StrictFinite::one();
        let result = a * one;
        prop_assert_eq!(result, a);
    }

    /// Multiplication by zero: a * 0 = 0
    #[test]
    fn prop_mul_zero(a in validated_real()) {
        let zero = RealNative64StrictFinite::zero();
        let result = a * zero;
        prop_assert_eq!(result, zero);
    }

    /// Subtraction is inverse of addition: (a + b) - b ≈ a
    #[test]
    fn prop_sub_inverse_of_add(
        a in small_positive_f64().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok()),
        b in small_positive_f64().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())
    ) {
        let sum = a + b;
        let result = sum - b;
        let diff = (result - a).abs();
        // Allow for floating-point tolerance
        let tolerance = a.abs() * RealNative64StrictFinite::from_f64(1e-10);
        prop_assert!(diff <= tolerance, "Expected {} ≈ {}, diff = {}", result, a, diff);
    }

    /// Division is inverse of multiplication: (a * b) / b ≈ a (for b ≠ 0)
    #[test]
    fn prop_div_inverse_of_mul(
        a in small_positive_f64().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok()),
        b in (1e-5_f64..1e5_f64).prop_filter_map("valid nonzero", |x| RealNative64StrictFinite::try_from_f64(x).ok())
    ) {
        let prod = a * b;
        let result = prod / b;
        let diff = (result - a).abs();
        let tolerance = a.abs() * RealNative64StrictFinite::from_f64(1e-10);
        prop_assert!(diff <= tolerance, "Expected {} ≈ {}, diff = {}", result, a, diff);
    }

    /// Negation is self-inverse: -(-a) = a
    #[test]
    fn prop_neg_self_inverse(a in validated_real()) {
        let double_neg = -(-a);
        prop_assert_eq!(double_neg, a);
    }
}

// =============================================================================
// SQUARE ROOT PROPERTIES
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// sqrt(x)² ≈ x for positive x
    #[test]
    fn prop_sqrt_squared(x in positive_validated_real()) {
        let sqrt_x = x.sqrt();
        let squared = sqrt_x * sqrt_x;
        let diff = (squared - x).abs();
        let tolerance = x.abs() * RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance, "sqrt({})² = {}, expected {}", x, squared, x);
    }

    /// sqrt(a * b) = sqrt(a) * sqrt(b) for positive a, b
    #[test]
    fn prop_sqrt_product(
        a in positive_validated_real(),
        b in positive_validated_real()
    ) {
        // Guard against overflow
        let product_raw = *a.as_ref() * *b.as_ref();
        prop_assume!(product_raw.is_finite() && product_raw > f64::MIN_POSITIVE);

        let product = a * b;
        let sqrt_product = product.sqrt();
        let sqrt_a = a.sqrt();
        let sqrt_b = b.sqrt();
        let product_of_sqrts = sqrt_a * sqrt_b;

        let diff = (sqrt_product - product_of_sqrts).abs();
        let tolerance = product_of_sqrts.abs() * RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// sqrt(1) = 1
    #[test]
    fn prop_sqrt_one(_dummy in Just(())) {
        let one = RealNative64StrictFinite::one();
        let sqrt_one = one.sqrt();
        prop_assert_eq!(sqrt_one, one);
    }

    /// sqrt(0) = 0
    #[test]
    fn prop_sqrt_zero(_dummy in Just(())) {
        let zero = RealNative64StrictFinite::zero();
        let sqrt_zero = zero.sqrt();
        prop_assert_eq!(sqrt_zero, zero);
    }
}

// =============================================================================
// EXPONENTIAL AND LOGARITHM PROPERTIES
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// ln(exp(x)) ≈ x for reasonable x values
    #[test]
    fn prop_ln_exp_inverse(x in (-50.0..50.0_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let exp_x = x.exp();
        let ln_exp_x = exp_x.ln();
        let diff = (ln_exp_x - x).abs();
        let tolerance = RealNative64StrictFinite::from_f64(1e-12);
        prop_assert!(diff <= tolerance, "ln(exp({})) = {}, expected {}", x, ln_exp_x, x);
    }

    /// exp(ln(x)) ≈ x for positive x
    #[test]
    fn prop_exp_ln_inverse(x in positive_validated_real()) {
        let ln_x = x.ln();
        let exp_ln_x = ln_x.exp();
        let diff = (exp_ln_x - x).abs();
        let tolerance = x.abs() * RealNative64StrictFinite::from_f64(1e-12);
        prop_assert!(diff <= tolerance, "exp(ln({})) = {}, expected {}", x, exp_ln_x, x);
    }

    /// exp(a + b) = exp(a) * exp(b)
    #[test]
    fn prop_exp_sum(
        a in (-20.0..20.0_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok()),
        b in (-20.0..20.0_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())
    ) {
        let sum = a + b;
        let exp_sum = sum.exp();
        let exp_a = a.exp();
        let exp_b = b.exp();
        let product = exp_a * exp_b;

        let diff = (exp_sum - product).abs();
        let tolerance = product.abs() * RealNative64StrictFinite::from_f64(1e-12);
        prop_assert!(diff <= tolerance);
    }

    /// ln(a * b) = ln(a) + ln(b) for positive a, b
    #[test]
    fn prop_ln_product(
        a in (1e-10_f64..1e10_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok()),
        b in (1e-10_f64..1e10_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())
    ) {
        let product = a * b;
        // Guard against overflow/underflow
        let raw_product = *product.as_ref();
        prop_assume!(raw_product > f64::MIN_POSITIVE && raw_product < 1e100);

        let ln_product = product.ln();
        let ln_a = a.ln();
        let ln_b = b.ln();
        let sum_of_lns = ln_a + ln_b;

        let diff = (ln_product - sum_of_lns).abs();
        let tolerance = RealNative64StrictFinite::from_f64(1e-12);
        prop_assert!(diff <= tolerance);
    }

    /// exp(0) = 1
    #[test]
    fn prop_exp_zero(_dummy in Just(())) {
        let zero = RealNative64StrictFinite::zero();
        let one = RealNative64StrictFinite::one();
        let exp_zero = zero.exp();
        prop_assert_eq!(exp_zero, one);
    }

    /// ln(1) = 0
    #[test]
    fn prop_ln_one(_dummy in Just(())) {
        let zero = RealNative64StrictFinite::zero();
        let one = RealNative64StrictFinite::one();
        let ln_one = one.ln();
        prop_assert_eq!(ln_one, zero);
    }
}

// =============================================================================
// TRIGONOMETRIC PROPERTIES
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// sin²(x) + cos²(x) = 1 (Pythagorean identity)
    #[test]
    fn prop_pythagorean_identity(x in angle_radians().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let sin_x = x.sin();
        let cos_x = x.cos();
        let sin_sq = sin_x * sin_x;
        let cos_sq = cos_x * cos_x;
        let sum = sin_sq + cos_sq;

        let one = RealNative64StrictFinite::one();
        let diff = (sum - one).abs();
        let tolerance = RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance, "sin²({}) + cos²({}) = {}, expected 1", x, x, sum);
    }

    /// sin(-x) = -sin(x) (odd function)
    #[test]
    fn prop_sin_odd(x in angle_radians().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let sin_x = x.sin();
        let sin_neg_x = (-x).sin();
        let neg_sin_x = -sin_x;

        let diff = (sin_neg_x - neg_sin_x).abs();
        let tolerance = RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// cos(-x) = cos(x) (even function)
    #[test]
    fn prop_cos_even(x in angle_radians().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let cos_x = x.cos();
        let cos_neg_x = (-x).cos();

        let diff = (cos_x - cos_neg_x).abs();
        let tolerance = RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// tan(x) = sin(x) / cos(x)
    #[test]
    fn prop_tan_definition(x in tan_safe_angle().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let tan_x = x.tan();
        let sin_x = x.sin();
        let cos_x = x.cos();
        let ratio = sin_x / cos_x;

        let diff = (tan_x - ratio).abs();
        let tolerance = ratio.abs() * RealNative64StrictFinite::from_f64(1e-12)
            + RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// sin(0) = 0
    #[test]
    fn prop_sin_zero(_dummy in Just(())) {
        let zero = RealNative64StrictFinite::zero();
        let sin_zero = zero.sin();
        prop_assert_eq!(sin_zero, zero);
    }

    /// cos(0) = 1
    #[test]
    fn prop_cos_zero(_dummy in Just(())) {
        let zero = RealNative64StrictFinite::zero();
        let one = RealNative64StrictFinite::one();
        let cos_zero = zero.cos();
        prop_assert_eq!(cos_zero, one);
    }
}

// =============================================================================
// INVERSE TRIGONOMETRIC PROPERTIES
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// sin(asin(x)) = x for x ∈ [-1, 1]
    #[test]
    fn prop_sin_asin_inverse(x in unit_interval().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let asin_x = x.asin();
        let sin_asin_x = asin_x.sin();
        let diff = (sin_asin_x - x).abs();
        let tolerance = RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// cos(acos(x)) = x for x ∈ [-1, 1]
    #[test]
    fn prop_cos_acos_inverse(x in unit_interval().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let acos_x = x.acos();
        let cos_acos_x = acos_x.cos();
        let diff = (cos_acos_x - x).abs();
        let tolerance = RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// tan(atan(x)) = x
    /// Note: For very large x, atan(x) approaches ±π/2, making tan precision-limited
    #[test]
    fn prop_tan_atan_inverse(x in (-1e3_f64..1e3_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let atan_x = x.atan();
        let tan_atan_x = atan_x.tan();
        let diff = (tan_atan_x - x).abs();
        // Relative tolerance with absolute floor for near-zero values
        let tolerance = x.abs() * RealNative64StrictFinite::from_f64(1e-12)
            + RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }
}

// =============================================================================
// RECIPROCAL PROPERTIES
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// 1 / (1 / x) = x for x ≠ 0
    #[test]
    fn prop_reciprocal_self_inverse(
        x in (1e-100_f64..1e100_f64).prop_filter_map("valid nonzero", |x| RealNative64StrictFinite::try_from_f64(x).ok())
    ) {
        let recip = x.reciprocal();
        let double_recip = recip.reciprocal();
        let diff = (double_recip - x).abs();
        let tolerance = x.abs() * RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// x * (1/x) = 1 for x ≠ 0
    #[test]
    fn prop_reciprocal_product_one(
        x in (1e-50_f64..1e50_f64).prop_filter_map("valid nonzero", |x| RealNative64StrictFinite::try_from_f64(x).ok())
    ) {
        let recip = x.reciprocal();
        let product = x * recip;
        let one = RealNative64StrictFinite::one();
        let diff = (product - one).abs();
        let tolerance = RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }
}

// =============================================================================
// POWER PROPERTIES (using integer exponents)
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// x^1 = x (using Pow<i32>)
    #[test]
    fn prop_pow_one(x in positive_validated_real()) {
        let x_pow_1: RealNative64StrictFinite = Pow::pow(x, 1_i32);
        let diff = (x_pow_1 - x).abs();
        let tolerance = x.abs() * RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// x^0 = 1 for x > 0
    #[test]
    fn prop_pow_zero(x in positive_validated_real()) {
        let one = RealNative64StrictFinite::one();
        let x_pow_0: RealNative64StrictFinite = Pow::pow(x, 0_i32);
        prop_assert_eq!(x_pow_0, one);
    }

    /// x^2 = x * x
    #[test]
    fn prop_pow_two_equals_square(x in small_positive_f64().prop_filter_map("valid", |v| RealNative64StrictFinite::try_from_f64(v).ok())) {
        let x_squared: RealNative64StrictFinite = Pow::pow(x, 2_i32);
        let x_times_x = x * x;
        let diff = (x_squared - x_times_x).abs();
        let tolerance = x_times_x.abs() * RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// x^(-1) = 1/x
    #[test]
    fn prop_pow_neg_one_equals_reciprocal(
        x in (1e-50_f64..1e50_f64).prop_filter_map("valid nonzero", |v| RealNative64StrictFinite::try_from_f64(v).ok())
    ) {
        let x_pow_neg1: RealNative64StrictFinite = Pow::pow(x, -1_i32);
        let x_recip = x.reciprocal();
        let diff = (x_pow_neg1 - x_recip).abs();
        let tolerance = x_recip.abs() * RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// (x^a)^b = x^(a*b) for small integer exponents
    #[test]
    fn prop_pow_of_pow(
        x in (1e-2_f64..1e2_f64).prop_filter_map("valid", |v| RealNative64StrictFinite::try_from_f64(v).ok()),
        a in -2_i32..=2_i32,
        b in -2_i32..=2_i32
    ) {
        // Guard against 0^negative
        prop_assume!(*x.as_ref() > 0.0 || (a >= 0 && b >= 0));

        let x_pow_a: RealNative64StrictFinite = Pow::pow(x, a);
        // Guard against intermediate overflow/underflow
        let raw_pow_a = *x_pow_a.as_ref();
        prop_assume!(raw_pow_a.is_finite() && raw_pow_a.abs() > f64::MIN_POSITIVE);

        let left: RealNative64StrictFinite = Pow::pow(x_pow_a, b);
        let ab = a * b;
        let right: RealNative64StrictFinite = Pow::pow(x, ab);

        // Guard against result overflow/underflow
        let raw_left = *left.as_ref();
        let raw_right = *right.as_ref();
        prop_assume!(raw_left.is_finite() && raw_right.is_finite());
        prop_assume!(raw_left.abs() > f64::MIN_POSITIVE && raw_right.abs() > f64::MIN_POSITIVE);

        let diff = (left - right).abs();
        let tolerance = right.abs() * RealNative64StrictFinite::from_f64(1e-10);
        prop_assert!(diff <= tolerance);
    }
}

// =============================================================================
// COMPLEX NUMBER PROPERTIES
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// |z * conj(z)| = |z|² (magnitude property)
    #[test]
    fn prop_complex_magnitude_squared(
        re in (-100.0..100.0_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok()),
        im in (-100.0..100.0_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())
    ) {
        let z = ComplexNative64StrictFinite::new_complex(re, im);
        let conj_z = z.conjugate();
        let product = z * conj_z;

        // z * conj(z) should be a positive real number = |z|²
        let magnitude = z.abs();
        let mag_squared = magnitude * magnitude;

        // The real part of z * conj(z) should equal |z|²
        let product_re = product.real_part();
        let diff = (product_re - mag_squared).abs();
        let tolerance = mag_squared.abs() * RealNative64StrictFinite::from_f64(1e-12)
            + RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// conj(conj(z)) = z
    #[test]
    fn prop_conjugate_self_inverse(
        re in valid_f64().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok()),
        im in valid_f64().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())
    ) {
        let z = ComplexNative64StrictFinite::new_complex(re, im);
        let double_conj = z.conjugate().conjugate();
        prop_assert_eq!(double_conj, z);
    }

    /// |z| >= 0 (magnitude is non-negative)
    #[test]
    fn prop_magnitude_non_negative(
        re in valid_f64().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok()),
        im in valid_f64().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())
    ) {
        let z = ComplexNative64StrictFinite::new_complex(re, im);
        let magnitude = z.abs();
        let zero = RealNative64StrictFinite::zero();
        prop_assert!(magnitude >= zero);
    }
}

// =============================================================================
// SERIALIZATION ROUNDTRIP
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// JSON serialization roundtrip: deserialize(serialize(x)) ≈ x
    /// Note: JSON number formatting can have tiny rounding effects at extreme magnitudes
    #[test]
    fn prop_json_roundtrip(x in validated_real()) {
        let json = serde_json::to_string(&x).unwrap();
        let deserialized: RealNative64StrictFinite = serde_json::from_str(&json).unwrap();
        // Use relative tolerance for very large/small numbers
        let diff = (deserialized - x).abs();
        let tolerance = x.abs() * RealNative64StrictFinite::from_f64(1e-14)
            + RealNative64StrictFinite::from_f64(1e-300); // tiny absolute tolerance for near-zero
        prop_assert!(diff <= tolerance, "JSON roundtrip: {} != {}, diff = {}", deserialized, x, diff);
    }

    /// Conversion roundtrip: try_from_f64(*x.as_ref()) == Ok(x)
    #[test]
    fn prop_f64_conversion_roundtrip(x in validated_real()) {
        let raw = *x.as_ref();
        let back = RealNative64StrictFinite::try_from_f64(raw).unwrap();
        prop_assert_eq!(back, x);
    }
}

// =============================================================================
// ORDERING AND COMPARISON PROPERTIES
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// Transitivity: if a < b and b < c, then a < c
    #[test]
    fn prop_ordering_transitive(
        a in small_positive_f64().prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok()),
        delta1 in (1e-10_f64..1.0).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok()),
        delta2 in (1e-10_f64..1.0).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())
    ) {
        let b = a + delta1;
        let c = b + delta2;
        prop_assert!(a < b);
        prop_assert!(b < c);
        prop_assert!(a < c); // Transitivity
    }

    /// Antisymmetry: a <= b and b <= a implies a == b
    #[test]
    fn prop_ordering_antisymmetric(a in validated_real()) {
        let b = a;
        prop_assert!(a <= b);
        prop_assert!(b <= a);
        prop_assert_eq!(a, b);
    }

    /// Totality: either a <= b or b <= a (or both)
    #[test]
    fn prop_ordering_total(a in validated_real(), b in validated_real()) {
        prop_assert!(a <= b || b <= a);
    }
}

// =============================================================================
// ABSOLUTE VALUE PROPERTIES
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// |x| >= 0
    #[test]
    fn prop_abs_non_negative(x in validated_real()) {
        let abs_x = x.abs();
        let zero = RealNative64StrictFinite::zero();
        prop_assert!(abs_x >= zero);
    }

    /// |x| = |-x|
    #[test]
    fn prop_abs_symmetric(x in validated_real()) {
        let abs_x = x.abs();
        let abs_neg_x = (-x).abs();
        prop_assert_eq!(abs_x, abs_neg_x);
    }

    /// |x * y| = |x| * |y|
    #[test]
    fn prop_abs_multiplicative(
        x in small_positive_f64().prop_filter_map("valid", |v| RealNative64StrictFinite::try_from_f64(v).ok()),
        y in small_positive_f64().prop_filter_map("valid", |v| RealNative64StrictFinite::try_from_f64(v).ok())
    ) {
        // Use both positive and negative variants
        let x_neg = -x;
        let y_neg = -y;

        // Test all combinations
        for (a, b) in [(x, y), (x, y_neg), (x_neg, y), (x_neg, y_neg)] {
            let product = a * b;
            let abs_product = product.abs();
            let abs_a = a.abs();
            let abs_b = b.abs();
            let product_of_abs = abs_a * abs_b;

            let diff = (abs_product - product_of_abs).abs();
            let tolerance = product_of_abs.abs() * RealNative64StrictFinite::from_f64(1e-14);
            prop_assert!(diff <= tolerance);
        }
    }
}

// =============================================================================
// HYPERBOLIC FUNCTION PROPERTIES
// =============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    /// cosh²(x) - sinh²(x) = 1 (hyperbolic identity)
    /// Note: For larger |x|, cosh and sinh grow exponentially, so relative error accumulates
    #[test]
    fn prop_hyperbolic_identity(x in (-4.0..4.0_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let sinh_x = x.sinh();
        let cosh_x = x.cosh();
        let sinh_sq = sinh_x * sinh_x;
        let cosh_sq = cosh_x * cosh_x;
        let diff_sq = cosh_sq - sinh_sq;

        let one = RealNative64StrictFinite::one();
        let diff = (diff_sq - one).abs();
        // At x=4, cosh(4)≈27.3, cosh²≈745, typical f64 epsilon is ~2e-16, so error ~1e-13
        let tolerance = RealNative64StrictFinite::from_f64(1e-12);
        prop_assert!(diff <= tolerance, "cosh²({}) - sinh²({}) = {}, expected 1", x, x, diff_sq);
    }

    /// sinh(-x) = -sinh(x) (odd function)
    #[test]
    fn prop_sinh_odd(x in (-10.0..10.0_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let sinh_x = x.sinh();
        let sinh_neg_x = (-x).sinh();
        let neg_sinh_x = -sinh_x;

        let diff = (sinh_neg_x - neg_sinh_x).abs();
        let tolerance = RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// cosh(-x) = cosh(x) (even function)
    #[test]
    fn prop_cosh_even(x in (-10.0..10.0_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let cosh_x = x.cosh();
        let cosh_neg_x = (-x).cosh();

        let diff = (cosh_x - cosh_neg_x).abs();
        let tolerance = RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }

    /// tanh(x) = sinh(x) / cosh(x)
    #[test]
    fn prop_tanh_definition(x in (-10.0..10.0_f64).prop_filter_map("valid", |x| RealNative64StrictFinite::try_from_f64(x).ok())) {
        let tanh_x = x.tanh();
        let sinh_x = x.sinh();
        let cosh_x = x.cosh();
        let ratio = sinh_x / cosh_x;

        let diff = (tanh_x - ratio).abs();
        let tolerance = RealNative64StrictFinite::from_f64(1e-14);
        prop_assert!(diff <= tolerance);
    }
}