@@ -425,14 +425,16 @@ fn test_recip() {
425425 assert_eq ! ( neg_inf. recip( ) , 0.0 ) ;
426426}
427427
428+ // Many math functions allow for less accurate results, so the next tolerance up is used
429+
428430#[ test]
429431fn test_powi ( ) {
430432 let nan: f128 = f128:: NAN ;
431433 let inf: f128 = f128:: INFINITY ;
432434 let neg_inf: f128 = f128:: NEG_INFINITY ;
433435 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 ) ;
436438 assert_eq ! ( 8.3f128 . powi( 0 ) , 1.0 ) ;
437439 assert ! ( nan. powi( 2 ) . is_nan( ) ) ;
438440 assert_eq ! ( inf. powi( 3 ) , inf) ;
@@ -445,10 +447,10 @@ fn test_powf() {
445447 let inf: f128 = f128:: INFINITY ;
446448 let neg_inf: f128 = f128:: NEG_INFINITY ;
447449 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 ) ;
452454 assert_eq ! ( 8.3f128 . powf( 0.0 ) , 1.0 ) ;
453455 assert ! ( nan. powf( 2.0 ) . is_nan( ) ) ;
454456 assert_eq ! ( inf. powf( 2.0 ) , inf) ;
@@ -470,7 +472,7 @@ fn test_sqrt_domain() {
470472fn test_exp ( ) {
471473 assert_eq ! ( 1.0 , 0.0f128 . exp( ) ) ;
472474 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 ) ;
474476
475477 let inf: f128 = f128:: INFINITY ;
476478 let neg_inf: f128 = f128:: NEG_INFINITY ;
@@ -505,7 +507,7 @@ fn test_ln() {
505507 assert ! ( ( -2.3f128 ) . ln( ) . is_nan( ) ) ;
506508 assert_eq ! ( ( -0.0f128 ) . ln( ) , neg_inf) ;
507509 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 ) ;
509511}
510512
511513#[ test]
@@ -514,7 +516,7 @@ fn test_log() {
514516 let inf: f128 = f128:: INFINITY ;
515517 let neg_inf: f128 = f128:: NEG_INFINITY ;
516518 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 ) ;
518520 assert_eq ! ( 1.0f128 . exp( ) . log( 1.0f128 . exp( ) ) , 1.0 ) ;
519521 assert ! ( 1.0f128 . log( 1.0 ) . is_nan( ) ) ;
520522 assert ! ( 1.0f128 . log( -13.9 ) . is_nan( ) ) ;
@@ -531,9 +533,9 @@ fn test_log2() {
531533 let nan: f128 = f128:: NAN ;
532534 let inf: f128 = f128:: INFINITY ;
533535 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 ) ;
537539 assert ! ( nan. log2( ) . is_nan( ) ) ;
538540 assert_eq ! ( inf. log2( ) , inf) ;
539541 assert ! ( neg_inf. log2( ) . is_nan( ) ) ;
@@ -548,8 +550,8 @@ fn test_log10() {
548550 let inf: f128 = f128:: INFINITY ;
549551 let neg_inf: f128 = f128:: NEG_INFINITY ;
550552 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 ) ;
553555 assert_eq ! ( 1.0f128 . log10( ) , 0.0 ) ;
554556 assert ! ( nan. log10( ) . is_nan( ) ) ;
555557 assert_eq ! ( inf. log10( ) , inf) ;
@@ -566,7 +568,7 @@ fn test_to_degrees() {
566568 let inf: f128 = f128:: INFINITY ;
567569 let neg_inf: f128 = f128:: NEG_INFINITY ;
568570 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 ) ;
570572 assert_approx_eq ! ( pi. to_degrees( ) , 180.0 , TOL_P2 ) ;
571573 assert ! ( nan. to_degrees( ) . is_nan( ) ) ;
572574 assert_eq ! ( inf. to_degrees( ) , inf) ;
@@ -581,8 +583,8 @@ fn test_to_radians() {
581583 let inf: f128 = f128:: INFINITY ;
582584 let neg_inf: f128 = f128:: NEG_INFINITY ;
583585 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 ) ;
586588 // check approx rather than exact because round trip for pi doesn't fall on an exactly
587589 // representable value (unlike `f32` and `f64`).
588590 assert_approx_eq ! ( 180.0f128 . to_radians( ) , pi, TOL_0 ) ;
@@ -593,6 +595,7 @@ fn test_to_radians() {
593595
594596#[ test]
595597fn test_asinh ( ) {
598+ // Lower accuracy results are allowed, use increased tolerances
596599 assert_eq ! ( 0.0f128 . asinh( ) , 0.0f128 ) ;
597600 assert_eq ! ( ( -0.0f128 ) . asinh( ) , -0.0f128 ) ;
598601
@@ -603,20 +606,21 @@ fn test_asinh() {
603606 assert_eq ! ( neg_inf. asinh( ) , neg_inf) ;
604607 assert ! ( nan. asinh( ) . is_nan( ) ) ;
605608 assert ! ( ( -0.0f128 ) . asinh( ) . is_sign_negative( ) ) ;
609+
606610 // 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 ) ;
609613 // regression test for the catastrophic cancellation fixed in 72486
610614 assert_approx_eq ! (
611615 ( -67452098.07139316f128 ) . asinh( ) ,
612616 -18.720075426274544393985484294000831757220 ,
613- TOL_0
617+ TOL_P4
614618 ) ;
615619
616620 // 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 ) ;
618622 // 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 ) ;
620624}
621625
622626#[ test]
@@ -630,11 +634,11 @@ fn test_acosh() {
630634 assert_eq ! ( inf. acosh( ) , inf) ;
631635 assert ! ( neg_inf. acosh( ) . is_nan( ) ) ;
632636 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 ) ;
635639
636640 // 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 ) ;
638642}
639643
640644#[ test]
@@ -652,20 +656,20 @@ fn test_atanh() {
652656 assert ! ( inf. atanh( ) . is_nan( ) ) ;
653657 assert ! ( neg_inf. atanh( ) . is_nan( ) ) ;
654658 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 ) ;
657661}
658662
659663#[ test]
660664fn 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 ) ;
669673 assert_eq ! ( 0.0f128 . gamma( ) , f128:: INFINITY ) ;
670674 assert_eq ! ( ( -0.0f128 ) . gamma( ) , f128:: NEG_INFINITY ) ;
671675 assert ! ( ( -1.0f128 ) . gamma( ) . is_nan( ) ) ;
@@ -678,13 +682,13 @@ fn test_gamma() {
678682
679683#[ test]
680684fn 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 ) ;
682686 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 ) ;
684688 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 ) ;
686690 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 ) ;
688692 assert_eq ! ( ( -0.5f128 ) . ln_gamma( ) . 1 , -1 ) ;
689693}
690694
0 commit comments