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
//! Property-Based Testing for Backend Mathematical Correctness
//!
//! This module provides comprehensive property-based tests to verify mathematical
//! properties of backend operations. Uses proptest to generate thousands of random
//! test cases and verify that mathematical laws and properties hold.
//!
//! ## Tested Properties
//!
//! - **Algebraic Properties**: Commutativity, associativity, distributivity
//! - **Identity Properties**: Additive identity (0), multiplicative identity (1)
//! - **Inverse Properties**: a + (-a) = 0, a * (1/a) = 1
//! - **Numerical Stability**: Precision bounds, overflow handling
//! - **Monotonicity**: Operation ordering preservation
//! - **Idempotence**: Repeated operations yield same result
#[cfg(test)]
mod tests {
use proptest::prelude::*;
/// Maximum float value for testing to avoid overflow
const MAX_FLOAT: f32 = 1e6;
const MIN_FLOAT: f32 = -1e6;
/// Tolerance for floating-point comparisons
/// Note: These tolerances account for cumulative floating-point errors
/// in multi-step operations, which is a fundamental property of IEEE 754
/// The tolerance is set higher to handle edge cases like nearly-canceling operations
const EPSILON: f32 = 5e-4; // Relaxed for accumulation of rounding errors and near-cancellation
const EPSILON_F64: f64 = 1e-10; // f64 has better precision but still has limits
/// Helper to check if two floats are approximately equal
/// Uses relative error for better handling of large values
fn approx_eq(a: f32, b: f32) -> bool {
let abs_diff = (a - b).abs();
let max_val = a.abs().max(b.abs());
// Use relative error for large values, absolute for small values
if max_val > 1.0 {
abs_diff / max_val < EPSILON
} else {
abs_diff < EPSILON
}
}
fn approx_eq_f64(a: f64, b: f64) -> bool {
let abs_diff = (a - b).abs();
let max_val = a.abs().max(b.abs());
// Use relative error for large values, absolute for small values
if max_val > 1.0 {
abs_diff / max_val < EPSILON_F64
} else {
abs_diff < EPSILON_F64
}
}
// =====================================================================
// ARITHMETIC PROPERTIES
// =====================================================================
proptest! {
/// Test commutativity of addition: a + b = b + a
#[test]
fn test_addition_commutative(
a in MIN_FLOAT..MAX_FLOAT,
b in MIN_FLOAT..MAX_FLOAT
) {
let result1 = a + b;
let result2 = b + a;
prop_assert!(approx_eq(result1, result2),
"Addition not commutative: {} + {} = {} but {} + {} = {}",
a, b, result1, b, a, result2);
}
/// Test associativity of addition: (a + b) + c = a + (b + c)
///
/// Note: Floating-point addition is not perfectly associative due to rounding errors,
/// especially when there is significant cancellation (large values adding to small results).
/// We use tolerance scaled to the operand magnitudes, not the result magnitude,
/// because catastrophic cancellation can make |result| << |operands|.
#[test]
fn test_addition_associative(
a in MIN_FLOAT..MAX_FLOAT,
b in MIN_FLOAT..MAX_FLOAT,
c in MIN_FLOAT..MAX_FLOAT
) {
let result1 = (a + b) + c;
let result2 = a + (b + c);
// Scale tolerance by the magnitude of the operands, not the results.
// When large operands nearly cancel, |result| can be tiny while the
// rounding error stays proportional to the operand magnitudes.
// f32 machine epsilon ≈ 1.19e-7; two additions can each introduce
// up to 0.5 ULP error, so we allow a few ULPs of the largest operand.
let abs_diff = (result1 - result2).abs();
let operand_scale = a.abs().max(b.abs()).max(c.abs()).max(1.0);
let tolerance = operand_scale * 1e-5; // ~100 ULPs of largest operand
prop_assert!(abs_diff <= tolerance,
"Addition not associative beyond tolerance: ({} + {}) + {} = {} but {} + ({} + {}) = {}, \
diff = {}, tolerance = {}",
a, b, c, result1, a, b, c, result2, abs_diff, tolerance);
}
/// Test commutativity of multiplication: a * b = b * a
#[test]
fn test_multiplication_commutative(
a in MIN_FLOAT..MAX_FLOAT,
b in MIN_FLOAT..MAX_FLOAT
) {
let result1 = a * b;
let result2 = b * a;
prop_assert!(approx_eq(result1, result2),
"Multiplication not commutative: {} * {} = {} but {} * {} = {}",
a, b, result1, b, a, result2);
}
/// Test associativity of multiplication: (a * b) * c = a * (b * c)
#[test]
fn test_multiplication_associative(
a in -100.0f32..100.0,
b in -100.0f32..100.0,
c in -100.0f32..100.0
) {
let result1 = (a * b) * c;
let result2 = a * (b * c);
prop_assert!(approx_eq(result1, result2),
"Multiplication not associative: ({} * {}) * {} = {} but {} * ({} * {}) = {}",
a, b, c, result1, a, b, c, result2);
}
/// Test distributivity: a * (b + c) = a * b + a * c
#[test]
fn test_distributive_property(
a in -100.0f32..100.0,
b in -100.0f32..100.0,
c in -100.0f32..100.0
) {
let result1 = a * (b + c);
let result2 = a * b + a * c;
prop_assert!(approx_eq(result1, result2),
"Distributivity violated: {} * ({} + {}) = {} but {} * {} + {} * {} = {}",
a, b, c, result1, a, b, a, c, result2);
}
/// Test additive identity: a + 0 = a
#[test]
fn test_additive_identity(a in MIN_FLOAT..MAX_FLOAT) {
let result = a + 0.0;
prop_assert!(approx_eq(result, a),
"Additive identity violated: {} + 0 = {} (expected {})",
a, result, a);
}
/// Test multiplicative identity: a * 1 = a
#[test]
fn test_multiplicative_identity(a in MIN_FLOAT..MAX_FLOAT) {
let result = a * 1.0;
prop_assert!(approx_eq(result, a),
"Multiplicative identity violated: {} * 1 = {} (expected {})",
a, result, a);
}
/// Test additive inverse: a + (-a) = 0
#[test]
fn test_additive_inverse(a in MIN_FLOAT..MAX_FLOAT) {
let result = a + (-a);
prop_assert!(approx_eq(result, 0.0),
"Additive inverse violated: {} + ({}) = {} (expected 0)",
a, -a, result);
}
/// Test multiplicative inverse: a * (1/a) = 1 (for non-zero a)
#[test]
fn test_multiplicative_inverse(a in -1000.0f32..1000.0) {
prop_assume!(a.abs() > 0.001); // Avoid division by near-zero
let result = a * (1.0 / a);
prop_assert!(approx_eq(result, 1.0),
"Multiplicative inverse violated: {} * (1/{}) = {} (expected 1)",
a, a, result);
}
/// Test subtraction as inverse of addition: (a + b) - b = a
/// Note: We constrain the range to avoid floating-point precision
/// issues that occur when adding numbers with vastly different magnitudes
#[test]
fn test_subtraction_inverse(
a in -1000.0f32..1000.0,
b in -1000.0f32..1000.0
) {
let result = (a + b) - b;
prop_assert!(approx_eq(result, a),
"Subtraction inverse violated: ({} + {}) - {} = {} (expected {})",
a, b, b, result, a);
}
/// Test division as inverse of multiplication: (a * b) / b = a (for non-zero b)
#[test]
fn test_division_inverse(
a in -1000.0f32..1000.0,
b in -1000.0f32..1000.0
) {
prop_assume!(b.abs() > 0.001); // Avoid division by near-zero
let result = (a * b) / b;
prop_assert!(approx_eq(result, a),
"Division inverse violated: ({} * {}) / {} = {} (expected {})",
a, b, b, result, a);
}
}
// =====================================================================
// COMPARISON AND ORDERING PROPERTIES
// =====================================================================
proptest! {
/// Test transitivity of less-than: if a < b and b < c, then a < c
#[test]
fn test_ordering_transitivity(
a in -1000.0f32..1000.0,
b in -1000.0f32..1000.0,
c in -1000.0f32..1000.0
) {
if a < b && b < c {
prop_assert!(a < c,
"Transitivity violated: {} < {} and {} < {} but {} >= {}",
a, b, b, c, a, c);
}
}
/// Test reflexivity of equality: a == a
#[test]
fn test_equality_reflexive(a in MIN_FLOAT..MAX_FLOAT) {
prop_assert_eq!(a, a, "Reflexivity violated: {} != {}", a, a);
}
/// Test symmetry of equality: if a == b then b == a
#[test]
fn test_equality_symmetric(
a in -1000.0f32..1000.0,
b in -1000.0f32..1000.0
) {
if approx_eq(a, b) {
prop_assert!(approx_eq(b, a),
"Symmetry violated: {} == {} but {} != {}",
a, b, b, a);
}
}
/// Test transitivity of equality: if a == b and b == c then a == c
#[test]
fn test_equality_transitive(
a in -1000.0f32..1000.0,
b in -1000.0f32..1000.0,
c in -1000.0f32..1000.0
) {
if approx_eq(a, b) && approx_eq(b, c) {
prop_assert!(approx_eq(a, c),
"Transitivity of equality violated: {} == {} and {} == {} but {} != {}",
a, b, b, c, a, c);
}
}
}
// =====================================================================
// SPECIAL FUNCTION PROPERTIES
// =====================================================================
proptest! {
/// Test exp(ln(x)) = x for positive x
#[test]
fn test_exp_ln_inverse(x in 0.001f32..1000.0) {
let result = x.ln().exp();
prop_assert!(approx_eq(result, x),
"exp(ln({})) = {} (expected {})",
x, result, x);
}
/// Test ln(exp(x)) = x
#[test]
fn test_ln_exp_inverse(x in -10.0f32..10.0) {
let result = x.exp().ln();
prop_assert!(approx_eq(result, x),
"ln(exp({})) = {} (expected {})",
x, result, x);
}
/// Test sqrt(x^2) = |x|
#[test]
fn test_sqrt_square_inverse(x in -1000.0f32..1000.0) {
let result = (x * x).sqrt();
prop_assert!(approx_eq(result, x.abs()),
"sqrt({}^2) = {} (expected {})",
x, result, x.abs());
}
/// Test abs(x) >= 0
#[test]
fn test_abs_non_negative(x in MIN_FLOAT..MAX_FLOAT) {
let result = x.abs();
prop_assert!(result >= 0.0,
"abs({}) = {} (expected >= 0)",
x, result);
}
/// Test abs(-x) = abs(x)
#[test]
fn test_abs_symmetric(x in MIN_FLOAT..MAX_FLOAT) {
let result1 = x.abs();
let result2 = (-x).abs();
prop_assert!(approx_eq(result1, result2),
"abs({}) = {} but abs({}) = {}",
x, result1, -x, result2);
}
/// Test max(a, b) >= a and max(a, b) >= b
#[test]
fn test_max_upper_bound(
a in MIN_FLOAT..MAX_FLOAT,
b in MIN_FLOAT..MAX_FLOAT
) {
let result = a.max(b);
prop_assert!(result >= a && result >= b,
"max({}, {}) = {} but should be >= both",
a, b, result);
}
/// Test min(a, b) <= a and min(a, b) <= b
#[test]
fn test_min_lower_bound(
a in MIN_FLOAT..MAX_FLOAT,
b in MIN_FLOAT..MAX_FLOAT
) {
let result = a.min(b);
prop_assert!(result <= a && result <= b,
"min({}, {}) = {} but should be <= both",
a, b, result);
}
/// Test max is commutative: max(a, b) = max(b, a)
#[test]
fn test_max_commutative(
a in MIN_FLOAT..MAX_FLOAT,
b in MIN_FLOAT..MAX_FLOAT
) {
let result1 = a.max(b);
let result2 = b.max(a);
prop_assert!(approx_eq(result1, result2),
"max({}, {}) = {} but max({}, {}) = {}",
a, b, result1, b, a, result2);
}
/// Test min is commutative: min(a, b) = min(b, a)
#[test]
fn test_min_commutative(
a in MIN_FLOAT..MAX_FLOAT,
b in MIN_FLOAT..MAX_FLOAT
) {
let result1 = a.min(b);
let result2 = b.min(a);
prop_assert!(approx_eq(result1, result2),
"min({}, {}) = {} but min({}, {}) = {}",
a, b, result1, b, a, result2);
}
}
// =====================================================================
// TRIGONOMETRIC PROPERTIES
// =====================================================================
proptest! {
/// Test sin^2(x) + cos^2(x) = 1 (Pythagorean identity)
#[test]
fn test_pythagorean_identity(x in -10.0f32..10.0) {
let sin_x = x.sin();
let cos_x = x.cos();
let result = sin_x * sin_x + cos_x * cos_x;
prop_assert!(approx_eq(result, 1.0),
"sin^2({}) + cos^2({}) = {} (expected 1)",
x, x, result);
}
/// Test sin(-x) = -sin(x) (odd function)
#[test]
fn test_sin_odd_function(x in -10.0f32..10.0) {
let result1 = (-x).sin();
let result2 = -x.sin();
prop_assert!(approx_eq(result1, result2),
"sin(-{}) = {} but -sin({}) = {}",
x, result1, x, result2);
}
/// Test cos(-x) = cos(x) (even function)
#[test]
fn test_cos_even_function(x in -10.0f32..10.0) {
let result1 = (-x).cos();
let result2 = x.cos();
prop_assert!(approx_eq(result1, result2),
"cos(-{}) = {} but cos({}) = {}",
x, result1, x, result2);
}
/// Test -1 <= sin(x) <= 1
#[test]
fn test_sin_bounded(x in -100.0f32..100.0) {
let result = x.sin();
prop_assert!(result >= -1.0 && result <= 1.0,
"sin({}) = {} but should be in [-1, 1]",
x, result);
}
/// Test -1 <= cos(x) <= 1
#[test]
fn test_cos_bounded(x in -100.0f32..100.0) {
let result = x.cos();
prop_assert!(result >= -1.0 && result <= 1.0,
"cos({}) = {} but should be in [-1, 1]",
x, result);
}
/// Test tan(x) = sin(x) / cos(x) (when cos(x) != 0)
#[test]
fn test_tan_definition(x in -1.5f32..1.5) {
// Avoid points where cos(x) is near zero
prop_assume!(x.cos().abs() > 0.1);
let tan_x = x.tan();
let expected = x.sin() / x.cos();
prop_assert!(approx_eq(tan_x, expected),
"tan({}) = {} but sin({})/cos({}) = {}",
x, tan_x, x, x, expected);
}
}
// =====================================================================
// MONOTONICITY PROPERTIES
// =====================================================================
proptest! {
/// Test exp is monotonically increasing
#[test]
fn test_exp_monotonic(a in -10.0f32..10.0, b in -10.0f32..10.0) {
if a < b {
prop_assert!(a.exp() < b.exp(),
"exp not monotonic: {} < {} but exp({}) = {} >= exp({}) = {}",
a, b, a, a.exp(), b, b.exp());
}
}
/// Test ln is monotonically increasing on positive reals
#[test]
fn test_ln_monotonic(a in 0.01f32..1000.0, b in 0.01f32..1000.0) {
if a < b {
prop_assert!(a.ln() < b.ln(),
"ln not monotonic: {} < {} but ln({}) = {} >= ln({}) = {}",
a, b, a, a.ln(), b, b.ln());
}
}
/// Test sqrt is monotonically increasing on non-negative reals
#[test]
fn test_sqrt_monotonic(a in 0.0f32..1000.0, b in 0.0f32..1000.0) {
if a < b {
prop_assert!(a.sqrt() < b.sqrt(),
"sqrt not monotonic: {} < {} but sqrt({}) = {} >= sqrt({}) = {}",
a, b, a, a.sqrt(), b, b.sqrt());
}
}
}
// =====================================================================
// DOUBLE PRECISION PROPERTIES
// =====================================================================
proptest! {
/// Test f64 arithmetic maintains higher precision
#[test]
fn test_f64_precision(
a in -1000.0f64..1000.0,
b in -1000.0f64..1000.0,
c in -1000.0f64..1000.0
) {
// Test that f64 maintains associativity better than f32
let result_f64 = (a + b) + c;
let expected_f64 = a + (b + c);
prop_assert!(approx_eq_f64(result_f64, expected_f64),
"f64 addition not associative: ({} + {}) + {} = {} but {} + ({} + {}) = {}",
a, b, c, result_f64, a, b, c, expected_f64);
}
/// Test f64 distributivity
#[test]
fn test_f64_distributive(
a in -100.0f64..100.0,
b in -100.0f64..100.0,
c in -100.0f64..100.0
) {
let result = a * (b + c);
let expected = a * b + a * c;
prop_assert!(approx_eq_f64(result, expected),
"f64 distributivity violated: {} * ({} + {}) = {} but {} * {} + {} * {} = {}",
a, b, c, result, a, b, a, c, expected);
}
}
// =====================================================================
// NUMERICAL STABILITY PROPERTIES
// =====================================================================
proptest! {
/// Test that operations don't produce NaN for valid inputs
#[test]
fn test_no_unexpected_nan(
a in -1000.0f32..1000.0,
b in -1000.0f32..1000.0
) {
let sum = a + b;
let diff = a - b;
prop_assert!(!sum.is_nan(), "Addition produced NaN: {} + {} = NaN", a, b);
prop_assert!(!diff.is_nan(), "Subtraction produced NaN: {} - {} = NaN", a, b);
}
/// Test that division by non-zero doesn't produce NaN
#[test]
fn test_division_no_nan(
a in -1000.0f32..1000.0,
b in -1000.0f32..1000.0
) {
prop_assume!(b.abs() > 0.001);
let result = a / b;
prop_assert!(!result.is_nan(),
"Division produced NaN: {} / {} = NaN",
a, b);
}
/// Test that sqrt of positive numbers doesn't produce NaN
#[test]
fn test_sqrt_no_nan(x in 0.0f32..1000.0) {
let result = x.sqrt();
prop_assert!(!result.is_nan(),
"sqrt produced NaN: sqrt({}) = NaN",
x);
}
/// Test that exp doesn't overflow for reasonable inputs
#[test]
fn test_exp_no_inf(x in -10.0f32..10.0) {
let result = x.exp();
prop_assert!(!result.is_infinite(),
"exp overflowed: exp({}) = inf",
x);
}
}
// =====================================================================
// IDEMPOTENCE PROPERTIES
// =====================================================================
proptest! {
/// Test abs is idempotent: abs(abs(x)) = abs(x)
#[test]
fn test_abs_idempotent(x in MIN_FLOAT..MAX_FLOAT) {
let once = x.abs();
let twice = once.abs();
prop_assert!(approx_eq(once, twice),
"abs not idempotent: abs({}) = {} but abs(abs({})) = {}",
x, once, x, twice);
}
/// Test max(x, x) = x
#[test]
fn test_max_idempotent(x in MIN_FLOAT..MAX_FLOAT) {
let result = x.max(x);
prop_assert!(approx_eq(result, x),
"max not idempotent: max({}, {}) = {} (expected {})",
x, x, result, x);
}
/// Test min(x, x) = x
#[test]
fn test_min_idempotent(x in MIN_FLOAT..MAX_FLOAT) {
let result = x.min(x);
prop_assert!(approx_eq(result, x),
"min not idempotent: min({}, {}) = {} (expected {})",
x, x, result, x);
}
}
}