@@ -22,6 +22,11 @@ const NAN_MASK1: u32 = 0x002a_aaaa;
22
22
/// Second pattern over the mantissa
23
23
const NAN_MASK2 : u32 = 0x0055_5555 ;
24
24
25
+ /// Miri adds some extra errors to float functions; make sure the tests still pass.
26
+ /// These values are purely used as a canary to test against and are thus not a stable guarantee Rust provides.
27
+ /// They serve as a way to get an idea of the real precision of floating point operations on different platforms.
28
+ const APPROX_DETLA : f32 = if cfg ! ( miri) { 1e-4 } else { 1e-6 } ;
29
+
25
30
#[ allow( unused_macros) ]
26
31
macro_rules! assert_f32_biteq {
27
32
( $left : expr, $right : expr) => {
@@ -442,8 +447,12 @@ fn test_powi() {
442
447
let nan: f32 = f32:: NAN ;
443
448
let inf: f32 = f32:: INFINITY ;
444
449
let neg_inf: f32 = f32:: NEG_INFINITY ;
445
- assert_eq ! ( 1.0f32 . powi( 1 ) , 1.0 ) ;
446
- assert_approx_eq ! ( ( -3.1f32 ) . powi( 2 ) , 9.61 ) ;
450
+ assert_approx_eq ! ( 1.0f32 . powi( 1 ) , 1.0 ) ;
451
+ assert_approx_eq ! (
452
+ ( -3.1f32 ) . powi( 2 ) ,
453
+ 9.61 ,
454
+ APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
455
+ ) ;
447
456
assert_approx_eq ! ( 5.9f32 . powi( -2 ) , 0.028727 ) ;
448
457
assert_eq ! ( 8.3f32 . powi( 0 ) , 1.0 ) ;
449
458
assert ! ( nan. powi( 2 ) . is_nan( ) ) ;
@@ -457,9 +466,17 @@ fn test_powf() {
457
466
let inf: f32 = f32:: INFINITY ;
458
467
let neg_inf: f32 = f32:: NEG_INFINITY ;
459
468
assert_eq ! ( 1.0f32 . powf( 1.0 ) , 1.0 ) ;
460
- assert_approx_eq ! ( 3.4f32 . powf( 4.5 ) , 246.408218 ) ;
469
+ assert_approx_eq ! (
470
+ 3.4f32 . powf( 4.5 ) ,
471
+ 246.408218 ,
472
+ APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
473
+ ) ;
461
474
assert_approx_eq ! ( 2.7f32 . powf( -3.2 ) , 0.041652 ) ;
462
- assert_approx_eq ! ( ( -3.1f32 ) . powf( 2.0 ) , 9.61 ) ;
475
+ assert_approx_eq ! (
476
+ ( -3.1f32 ) . powf( 2.0 ) ,
477
+ 9.61 ,
478
+ APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
479
+ ) ;
463
480
assert_approx_eq ! ( 5.9f32 . powf( -2.0 ) , 0.028727 ) ;
464
481
assert_eq ! ( 8.3f32 . powf( 0.0 ) , 1.0 ) ;
465
482
assert ! ( nan. powf( 2.0 ) . is_nan( ) ) ;
@@ -482,7 +499,11 @@ fn test_sqrt_domain() {
482
499
fn test_exp ( ) {
483
500
assert_eq ! ( 1.0 , 0.0f32 . exp( ) ) ;
484
501
assert_approx_eq ! ( 2.718282 , 1.0f32 . exp( ) ) ;
485
- assert_approx_eq ! ( 148.413162 , 5.0f32 . exp( ) ) ;
502
+ assert_approx_eq ! (
503
+ 148.413162 ,
504
+ 5.0f32 . exp( ) ,
505
+ APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
506
+ ) ;
486
507
487
508
let inf: f32 = f32:: INFINITY ;
488
509
let neg_inf: f32 = f32:: NEG_INFINITY ;
@@ -494,7 +515,11 @@ fn test_exp() {
494
515
495
516
#[ test]
496
517
fn test_exp2 ( ) {
497
- assert_eq ! ( 32.0 , 5.0f32 . exp2( ) ) ;
518
+ assert_approx_eq ! (
519
+ 32.0 ,
520
+ 5.0f32 . exp2( ) ,
521
+ APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
522
+ ) ;
498
523
assert_eq ! ( 1.0 , 0.0f32 . exp2( ) ) ;
499
524
500
525
let inf: f32 = f32:: INFINITY ;
@@ -517,17 +542,21 @@ fn test_ln() {
517
542
assert ! ( ( -2.3f32 ) . ln( ) . is_nan( ) ) ;
518
543
assert_eq ! ( ( -0.0f32 ) . ln( ) , neg_inf) ;
519
544
assert_eq ! ( 0.0f32 . ln( ) , neg_inf) ;
520
- assert_approx_eq ! ( 4.0f32 . ln( ) , 1.386294 ) ;
545
+ assert_approx_eq ! (
546
+ 4.0f32 . ln( ) ,
547
+ 1.386294 ,
548
+ APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
549
+ ) ;
521
550
}
522
551
523
552
#[ test]
524
553
fn test_log ( ) {
525
554
let nan: f32 = f32:: NAN ;
526
555
let inf: f32 = f32:: INFINITY ;
527
556
let neg_inf: f32 = f32:: NEG_INFINITY ;
528
- assert_eq ! ( 10.0f32 . log( 10.0 ) , 1.0 ) ;
557
+ assert_approx_eq ! ( 10.0f32 . log( 10.0 ) , 1.0 ) ;
529
558
assert_approx_eq ! ( 2.3f32 . log( 3.5 ) , 0.664858 ) ;
530
- assert_eq ! ( 1.0f32 . exp( ) . log( 1.0f32 . exp( ) ) , 1.0 ) ;
559
+ assert_approx_eq ! ( 1.0f32 . exp( ) . log( 1.0f32 . exp( ) ) , 1.0 ) ;
531
560
assert ! ( 1.0f32 . log( 1.0 ) . is_nan( ) ) ;
532
561
assert ! ( 1.0f32 . log( -13.9 ) . is_nan( ) ) ;
533
562
assert ! ( nan. log( 2.3 ) . is_nan( ) ) ;
@@ -559,7 +588,7 @@ fn test_log10() {
559
588
let nan: f32 = f32:: NAN ;
560
589
let inf: f32 = f32:: INFINITY ;
561
590
let neg_inf: f32 = f32:: NEG_INFINITY ;
562
- assert_eq ! ( 10.0f32 . log10( ) , 1.0 ) ;
591
+ assert_approx_eq ! ( 10.0f32 . log10( ) , 1.0 ) ;
563
592
assert_approx_eq ! ( 2.3f32 . log10( ) , 0.361728 ) ;
564
593
assert_approx_eq ! ( 1.0f32 . exp( ) . log10( ) , 0.434294 ) ;
565
594
assert_eq ! ( 1.0f32 . log10( ) , 0.0 ) ;
@@ -639,7 +668,11 @@ fn test_acosh() {
639
668
assert_approx_eq ! ( 3.0f32 . acosh( ) , 1.76274717403908605046521864995958461f32 ) ;
640
669
641
670
// test for low accuracy from issue 104548
642
- assert_approx_eq ! ( 60.0f32 , 60.0f32 . cosh( ) . acosh( ) ) ;
671
+ assert_approx_eq ! (
672
+ 60.0f32 ,
673
+ 60.0f32 . cosh( ) . acosh( ) ,
674
+ APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
675
+ ) ;
643
676
}
644
677
645
678
#[ test]
@@ -718,7 +751,11 @@ fn test_real_consts() {
718
751
let ln_10: f32 = consts:: LN_10 ;
719
752
720
753
assert_approx_eq ! ( frac_pi_2, pi / 2f32 ) ;
721
- assert_approx_eq ! ( frac_pi_3, pi / 3f32 ) ;
754
+ assert_approx_eq ! (
755
+ frac_pi_3,
756
+ pi / 3f32 ,
757
+ APPROX_DETLA /* Miri float-non-det: Make tests pass for now */
758
+ ) ;
722
759
assert_approx_eq ! ( frac_pi_4, pi / 4f32 ) ;
723
760
assert_approx_eq ! ( frac_pi_6, pi / 6f32 ) ;
724
761
assert_approx_eq ! ( frac_pi_8, pi / 8f32 ) ;
0 commit comments