@@ -425,14 +425,16 @@ fn test_recip() {
425
425
assert_eq ! ( neg_inf. recip( ) , 0.0 ) ;
426
426
}
427
427
428
+ // Many math functions allow for less accurate results, so the next tolerance up is used
429
+
428
430
#[ test]
429
431
fn test_powi ( ) {
430
432
let nan: f128 = f128:: NAN ;
431
433
let inf: f128 = f128:: INFINITY ;
432
434
let neg_inf: f128 = f128:: NEG_INFINITY ;
433
435
assert_eq ! ( 1.0f128 . powi( 1 ) , 1.0 ) ;
434
- assert_approx_eq ! ( ( -3.1f128 ) . powi( 2 ) , 9.6100000000000005506706202140776519387 , TOL_0 ) ;
435
- assert_approx_eq ! ( 5.9f128 . powi( -2 ) , 0.028727377190462507313100483690639638451 , TOL_N2 ) ;
436
+ assert_approx_eq ! ( ( -3.1f128 ) . powi( 2 ) , 9.6100000000000005506706202140776519387 , TOL_P2 ) ;
437
+ assert_approx_eq ! ( 5.9f128 . powi( -2 ) , 0.028727377190462507313100483690639638451 , TOL_0 ) ;
436
438
assert_eq ! ( 8.3f128 . powi( 0 ) , 1.0 ) ;
437
439
assert ! ( nan. powi( 2 ) . is_nan( ) ) ;
438
440
assert_eq ! ( inf. powi( 3 ) , inf) ;
@@ -445,10 +447,10 @@ fn test_powf() {
445
447
let inf: f128 = f128:: INFINITY ;
446
448
let neg_inf: f128 = f128:: NEG_INFINITY ;
447
449
assert_eq ! ( 1.0f128 . powf( 1.0 ) , 1.0 ) ;
448
- assert_approx_eq ! ( 3.4f128 . powf( 4.5 ) , 246.40818323761892815995637964326426756 , TOL_P2 ) ;
449
- assert_approx_eq ! ( 2.7f128 . powf( -3.2 ) , 0.041652009108526178281070304373500889273 , TOL_N2 ) ;
450
- assert_approx_eq ! ( ( -3.1f128 ) . powf( 2.0 ) , 9.6100000000000005506706202140776519387 , TOL_P2 ) ;
451
- assert_approx_eq ! ( 5.9f128 . powf( -2.0 ) , 0.028727377190462507313100483690639638451 , TOL_N2 ) ;
450
+ assert_approx_eq ! ( 3.4f128 . powf( 4.5 ) , 246.40818323761892815995637964326426756 , TOL_P4 ) ;
451
+ assert_approx_eq ! ( 2.7f128 . powf( -3.2 ) , 0.041652009108526178281070304373500889273 , TOL_0 ) ;
452
+ assert_approx_eq ! ( ( -3.1f128 ) . powf( 2.0 ) , 9.6100000000000005506706202140776519387 , TOL_P4 ) ;
453
+ assert_approx_eq ! ( 5.9f128 . powf( -2.0 ) , 0.028727377190462507313100483690639638451 , TOL_0 ) ;
452
454
assert_eq ! ( 8.3f128 . powf( 0.0 ) , 1.0 ) ;
453
455
assert ! ( nan. powf( 2.0 ) . is_nan( ) ) ;
454
456
assert_eq ! ( inf. powf( 2.0 ) , inf) ;
@@ -470,7 +472,7 @@ fn test_sqrt_domain() {
470
472
fn test_exp ( ) {
471
473
assert_eq ! ( 1.0 , 0.0f128 . exp( ) ) ;
472
474
assert_approx_eq ! ( consts:: E , 1.0f128 . exp( ) , TOL_0 ) ;
473
- assert_approx_eq ! ( 148.41315910257660342111558004055227962348775 , 5.0f128 . exp( ) , TOL_0 ) ;
475
+ assert_approx_eq ! ( 148.41315910257660342111558004055227962348775 , 5.0f128 . exp( ) , TOL_P2 ) ;
474
476
475
477
let inf: f128 = f128:: INFINITY ;
476
478
let neg_inf: f128 = f128:: NEG_INFINITY ;
@@ -505,7 +507,7 @@ fn test_ln() {
505
507
assert ! ( ( -2.3f128 ) . ln( ) . is_nan( ) ) ;
506
508
assert_eq ! ( ( -0.0f128 ) . ln( ) , neg_inf) ;
507
509
assert_eq ! ( 0.0f128 . ln( ) , neg_inf) ;
508
- assert_approx_eq ! ( 4.0f128 . ln( ) , 1.3862943611198906188344642429163531366 , TOL_0 ) ;
510
+ assert_approx_eq ! ( 4.0f128 . ln( ) , 1.3862943611198906188344642429163531366 , TOL_P2 ) ;
509
511
}
510
512
511
513
#[ test]
@@ -514,7 +516,7 @@ fn test_log() {
514
516
let inf: f128 = f128:: INFINITY ;
515
517
let neg_inf: f128 = f128:: NEG_INFINITY ;
516
518
assert_eq ! ( 10.0f128 . log( 10.0 ) , 1.0 ) ;
517
- assert_approx_eq ! ( 2.3f128 . log( 3.5 ) , 0.664858 , TOL_0 ) ;
519
+ assert_approx_eq ! ( 2.3f128 . log( 3.5 ) , 0.66485771361478710036766645911922010272 , TOL_P4 ) ;
518
520
assert_eq ! ( 1.0f128 . exp( ) . log( 1.0f128 . exp( ) ) , 1.0 ) ;
519
521
assert ! ( 1.0f128 . log( 1.0 ) . is_nan( ) ) ;
520
522
assert ! ( 1.0f128 . log( -13.9 ) . is_nan( ) ) ;
@@ -531,9 +533,9 @@ fn test_log2() {
531
533
let nan: f128 = f128:: NAN ;
532
534
let inf: f128 = f128:: INFINITY ;
533
535
let neg_inf: f128 = f128:: NEG_INFINITY ;
534
- assert_approx_eq ! ( 10.0f128 . log2( ) , 3.32192809488736234787031942948939017 , TOL_0 ) ;
535
- assert_approx_eq ! ( 2.3f128 . log2( ) , 1.2016338611696504130002982471978765921 , TOL_0 ) ;
536
- assert_approx_eq ! ( 1.0f128 . exp( ) . log2( ) , 1.4426950408889634073599246810018921381 , TOL_0 ) ;
536
+ assert_approx_eq ! ( 10.0f128 . log2( ) , 3.32192809488736234787031942948939017 , TOL_P2 ) ;
537
+ assert_approx_eq ! ( 2.3f128 . log2( ) , 1.2016338611696504130002982471978765921 , TOL_P2 ) ;
538
+ assert_approx_eq ! ( 1.0f128 . exp( ) . log2( ) , 1.4426950408889634073599246810018921381 , TOL_P2 ) ;
537
539
assert ! ( nan. log2( ) . is_nan( ) ) ;
538
540
assert_eq ! ( inf. log2( ) , inf) ;
539
541
assert ! ( neg_inf. log2( ) . is_nan( ) ) ;
@@ -548,8 +550,8 @@ fn test_log10() {
548
550
let inf: f128 = f128:: INFINITY ;
549
551
let neg_inf: f128 = f128:: NEG_INFINITY ;
550
552
assert_eq ! ( 10.0f128 . log10( ) , 1.0 ) ;
551
- assert_approx_eq ! ( 2.3f128 . log10( ) , 0.36172783601759284532595218865859309898 , TOL_0 ) ;
552
- assert_approx_eq ! ( 1.0f128 . exp( ) . log10( ) , 0.43429448190325182765112891891660508222 , TOL_0 ) ;
553
+ assert_approx_eq ! ( 2.3f128 . log10( ) , 0.36172783601759284532595218865859309898 , TOL_P2 ) ;
554
+ assert_approx_eq ! ( 1.0f128 . exp( ) . log10( ) , 0.43429448190325182765112891891660508222 , TOL_P2 ) ;
553
555
assert_eq ! ( 1.0f128 . log10( ) , 0.0 ) ;
554
556
assert ! ( nan. log10( ) . is_nan( ) ) ;
555
557
assert_eq ! ( inf. log10( ) , inf) ;
@@ -566,7 +568,7 @@ fn test_to_degrees() {
566
568
let inf: f128 = f128:: INFINITY ;
567
569
let neg_inf: f128 = f128:: NEG_INFINITY ;
568
570
assert_eq ! ( 0.0f128 . to_degrees( ) , 0.0 ) ;
569
- assert_approx_eq ! ( ( -5.8f128 ) . to_degrees( ) , -332.31552117587745090765431723855668471 , TOL_P2 ) ;
571
+ assert_approx_eq ! ( ( -5.8f128 ) . to_degrees( ) , -332.31552117587745090765431723855668471 , TOL_P4 ) ;
570
572
assert_approx_eq ! ( pi. to_degrees( ) , 180.0 , TOL_P2 ) ;
571
573
assert ! ( nan. to_degrees( ) . is_nan( ) ) ;
572
574
assert_eq ! ( inf. to_degrees( ) , inf) ;
@@ -581,8 +583,8 @@ fn test_to_radians() {
581
583
let inf: f128 = f128:: INFINITY ;
582
584
let neg_inf: f128 = f128:: NEG_INFINITY ;
583
585
assert_eq ! ( 0.0f128 . to_radians( ) , 0.0 ) ;
584
- assert_approx_eq ! ( 154.6f128 . to_radians( ) , 2.6982790235832334267135442069489767804 , TOL_0 ) ;
585
- assert_approx_eq ! ( ( -332.31f128 ) . to_radians( ) , -5.7999036373023566567593094812182763013 , TOL_0 ) ;
586
+ assert_approx_eq ! ( 154.6f128 . to_radians( ) , 2.6982790235832334267135442069489767804 , TOL_P2 ) ;
587
+ assert_approx_eq ! ( ( -332.31f128 ) . to_radians( ) , -5.7999036373023566567593094812182763013 , TOL_P2 ) ;
586
588
// check approx rather than exact because round trip for pi doesn't fall on an exactly
587
589
// representable value (unlike `f32` and `f64`).
588
590
assert_approx_eq ! ( 180.0f128 . to_radians( ) , pi, TOL_0 ) ;
@@ -593,6 +595,7 @@ fn test_to_radians() {
593
595
594
596
#[ test]
595
597
fn test_asinh ( ) {
598
+ // Lower accuracy results are allowed, use increased tolerances
596
599
assert_eq ! ( 0.0f128 . asinh( ) , 0.0f128 ) ;
597
600
assert_eq ! ( ( -0.0f128 ) . asinh( ) , -0.0f128 ) ;
598
601
@@ -603,20 +606,21 @@ fn test_asinh() {
603
606
assert_eq ! ( neg_inf. asinh( ) , neg_inf) ;
604
607
assert ! ( nan. asinh( ) . is_nan( ) ) ;
605
608
assert ! ( ( -0.0f128 ) . asinh( ) . is_sign_negative( ) ) ;
609
+
606
610
// issue 63271
607
- assert_approx_eq ! ( 2.0f128 . asinh( ) , 1.443635475178810342493276740273105f128 , TOL_0 ) ;
608
- assert_approx_eq ! ( ( -2.0f128 ) . asinh( ) , -1.443635475178810342493276740273105f128 , TOL_0 ) ;
611
+ assert_approx_eq ! ( 2.0f128 . asinh( ) , 1.443635475178810342493276740273105f128 , TOL_P4 ) ;
612
+ assert_approx_eq ! ( ( -2.0f128 ) . asinh( ) , -1.443635475178810342493276740273105f128 , TOL_P4 ) ;
609
613
// regression test for the catastrophic cancellation fixed in 72486
610
614
assert_approx_eq ! (
611
615
( -67452098.07139316f128 ) . asinh( ) ,
612
616
-18.720075426274544393985484294000831757220 ,
613
- TOL_0
617
+ TOL_P4
614
618
) ;
615
619
616
620
// test for low accuracy from issue 104548
617
- assert_approx_eq ! ( 60.0f128 , 60.0f128 . sinh( ) . asinh( ) , TOL_0 ) ;
621
+ assert_approx_eq ! ( 60.0f128 , 60.0f128 . sinh( ) . asinh( ) , TOL_P4 ) ;
618
622
// mul needed for approximate comparison to be meaningful
619
- assert_approx_eq ! ( 1.0f128 , 1e-15f128 . sinh( ) . asinh( ) * 1e15f128 , TOL_0 ) ;
623
+ assert_approx_eq ! ( 1.0f128 , 1e-15f128 . sinh( ) . asinh( ) * 1e15f128 , TOL_P4 ) ;
620
624
}
621
625
622
626
#[ test]
@@ -630,11 +634,11 @@ fn test_acosh() {
630
634
assert_eq ! ( inf. acosh( ) , inf) ;
631
635
assert ! ( neg_inf. acosh( ) . is_nan( ) ) ;
632
636
assert ! ( nan. acosh( ) . is_nan( ) ) ;
633
- assert_approx_eq ! ( 2.0f128 . acosh( ) , 1.31695789692481670862504634730796844f128 , TOL_0 ) ;
634
- assert_approx_eq ! ( 3.0f128 . acosh( ) , 1.76274717403908605046521864995958461f128 , TOL_0 ) ;
637
+ assert_approx_eq ! ( 2.0f128 . acosh( ) , 1.31695789692481670862504634730796844f128 , TOL_P2 ) ;
638
+ assert_approx_eq ! ( 3.0f128 . acosh( ) , 1.76274717403908605046521864995958461f128 , TOL_P2 ) ;
635
639
636
640
// test for low accuracy from issue 104548
637
- assert_approx_eq ! ( 60.0f128 , 60.0f128 . cosh( ) . acosh( ) , TOL_P2 ) ;
641
+ assert_approx_eq ! ( 60.0f128 , 60.0f128 . cosh( ) . acosh( ) , TOL_P4 ) ;
638
642
}
639
643
640
644
#[ test]
@@ -652,20 +656,20 @@ fn test_atanh() {
652
656
assert ! ( inf. atanh( ) . is_nan( ) ) ;
653
657
assert ! ( neg_inf. atanh( ) . is_nan( ) ) ;
654
658
assert ! ( nan. atanh( ) . is_nan( ) ) ;
655
- assert_approx_eq ! ( 0.5f128 . atanh( ) , 0.54930614433405484569762261846126285f128 , TOL_0 ) ;
656
- assert_approx_eq ! ( ( -0.5f128 ) . atanh( ) , -0.54930614433405484569762261846126285f128 , TOL_0 ) ;
659
+ assert_approx_eq ! ( 0.5f128 . atanh( ) , 0.54930614433405484569762261846126285f128 , TOL_P2 ) ;
660
+ assert_approx_eq ! ( ( -0.5f128 ) . atanh( ) , -0.54930614433405484569762261846126285f128 , TOL_P2 ) ;
657
661
}
658
662
659
663
#[ test]
660
664
fn test_gamma ( ) {
661
- // precision can differ between platforms
662
- assert_approx_eq ! ( 1.0f128 . gamma( ) , 1.0f128 , TOL_0 ) ;
663
- assert_approx_eq ! ( 2.0f128 . gamma( ) , 1.0f128 , TOL_0 ) ;
664
- assert_approx_eq ! ( 3.0f128 . gamma( ) , 2.0f128 , TOL_0 ) ;
665
- assert_approx_eq ! ( 4.0f128 . gamma( ) , 6.0f128 , TOL_0 ) ;
666
- assert_approx_eq ! ( 5.0f128 . gamma( ) , 24.0f128 , TOL_0 ) ;
667
- assert_approx_eq ! ( 0.5f128 . gamma( ) , consts:: PI . sqrt( ) , TOL_0 ) ;
668
- assert_approx_eq ! ( ( -0.5f128 ) . gamma( ) , -2.0 * consts:: PI . sqrt( ) , TOL_0 ) ;
665
+ // precision can differ among platforms
666
+ assert_approx_eq ! ( 1.0f128 . gamma( ) , 1.0f128 , TOL_P2 ) ;
667
+ assert_approx_eq ! ( 2.0f128 . gamma( ) , 1.0f128 , TOL_P2 ) ;
668
+ assert_approx_eq ! ( 3.0f128 . gamma( ) , 2.0f128 , TOL_P2 ) ;
669
+ assert_approx_eq ! ( 4.0f128 . gamma( ) , 6.0f128 , TOL_P2 ) ;
670
+ assert_approx_eq ! ( 5.0f128 . gamma( ) , 24.0f128 , TOL_P2 ) ;
671
+ assert_approx_eq ! ( 0.5f128 . gamma( ) , consts:: PI . sqrt( ) , TOL_P2 ) ;
672
+ assert_approx_eq ! ( ( -0.5f128 ) . gamma( ) , -2.0 * consts:: PI . sqrt( ) , TOL_P2 ) ;
669
673
assert_eq ! ( 0.0f128 . gamma( ) , f128:: INFINITY ) ;
670
674
assert_eq ! ( ( -0.0f128 ) . gamma( ) , f128:: NEG_INFINITY ) ;
671
675
assert ! ( ( -1.0f128 ) . gamma( ) . is_nan( ) ) ;
@@ -678,13 +682,13 @@ fn test_gamma() {
678
682
679
683
#[ test]
680
684
fn test_ln_gamma ( ) {
681
- assert_approx_eq ! ( 1.0f128 . ln_gamma( ) . 0 , 0.0f128 , TOL_0 ) ;
685
+ assert_approx_eq ! ( 1.0f128 . ln_gamma( ) . 0 , 0.0f128 , TOL_P2 ) ;
682
686
assert_eq ! ( 1.0f128 . ln_gamma( ) . 1 , 1 ) ;
683
- assert_approx_eq ! ( 2.0f128 . ln_gamma( ) . 0 , 0.0f128 , TOL_0 ) ;
687
+ assert_approx_eq ! ( 2.0f128 . ln_gamma( ) . 0 , 0.0f128 , TOL_P2 ) ;
684
688
assert_eq ! ( 2.0f128 . ln_gamma( ) . 1 , 1 ) ;
685
- assert_approx_eq ! ( 3.0f128 . ln_gamma( ) . 0 , 2.0f128 . ln( ) , TOL_0 ) ;
689
+ assert_approx_eq ! ( 3.0f128 . ln_gamma( ) . 0 , 2.0f128 . ln( ) , TOL_P2 ) ;
686
690
assert_eq ! ( 3.0f128 . ln_gamma( ) . 1 , 1 ) ;
687
- assert_approx_eq ! ( ( -0.5f128 ) . ln_gamma( ) . 0 , ( 2.0 * consts:: PI . sqrt( ) ) . ln( ) , TOL_0 ) ;
691
+ assert_approx_eq ! ( ( -0.5f128 ) . ln_gamma( ) . 0 , ( 2.0 * consts:: PI . sqrt( ) ) . ln( ) , TOL_P2 ) ;
688
692
assert_eq ! ( ( -0.5f128 ) . ln_gamma( ) . 1 , -1 ) ;
689
693
}
690
694
0 commit comments