55use crate :: f16:: consts;
66use crate :: num:: * ;
77
8- // We run out of precision pretty quickly with f16
9-
10- /// Tolerance for results on the order of 10.0e-4;
11- const TOL_N4 : f16 = 1e-5 ;
12-
138/// Tolerance for results on the order of 10.0e-2;
149const TOL_N2 : f16 = 0.0001 ;
1510
@@ -257,16 +252,16 @@ fn test_trunc() {
257252
258253#[ test]
259254fn test_fract ( ) {
260- assert_approx_eq ! ( 1.0f16 . fract( ) , 0.0f16 , TOL_N2 ) ;
261- assert_approx_eq ! ( 1.3f16 . fract( ) , 0.3f16 , TOL_N2 ) ;
262- assert_approx_eq ! ( 1.5f16 . fract( ) , 0.5f16 , TOL_N2 ) ;
263- assert_approx_eq ! ( 1.7f16 . fract( ) , 0.7f16 , TOL_N2 ) ;
264- assert_approx_eq ! ( 0.0f16 . fract( ) , 0.0f16 , TOL_N2 ) ;
265- assert_approx_eq ! ( ( -0.0f16 ) . fract( ) , -0.0f16 , TOL_N2 ) ;
266- assert_approx_eq ! ( ( -1.0f16 ) . fract( ) , -0.0f16 , TOL_N2 ) ;
267- assert_approx_eq ! ( ( -1.3f16 ) . fract( ) , -0.3f16 , TOL_N2 ) ;
268- assert_approx_eq ! ( ( -1.5f16 ) . fract( ) , -0.5f16 , TOL_N2 ) ;
269- assert_approx_eq ! ( ( -1.7f16 ) . fract( ) , -0.7f16 , TOL_N2 ) ;
255+ assert_approx_eq ! ( 1.0f16 . fract( ) , 0.0f16 , TOL_0 ) ;
256+ assert_approx_eq ! ( 1.3f16 . fract( ) , 0.3f16 , TOL_0 ) ;
257+ assert_approx_eq ! ( 1.5f16 . fract( ) , 0.5f16 , TOL_0 ) ;
258+ assert_approx_eq ! ( 1.7f16 . fract( ) , 0.7f16 , TOL_0 ) ;
259+ assert_approx_eq ! ( 0.0f16 . fract( ) , 0.0f16 , TOL_0 ) ;
260+ assert_approx_eq ! ( ( -0.0f16 ) . fract( ) , -0.0f16 , TOL_0 ) ;
261+ assert_approx_eq ! ( ( -1.0f16 ) . fract( ) , -0.0f16 , TOL_0 ) ;
262+ assert_approx_eq ! ( ( -1.3f16 ) . fract( ) , -0.3f16 , TOL_0 ) ;
263+ assert_approx_eq ! ( ( -1.5f16 ) . fract( ) , -0.5f16 , TOL_0 ) ;
264+ assert_approx_eq ! ( ( -1.7f16 ) . fract( ) , -0.7f16 , TOL_0 ) ;
270265}
271266
272267#[ test]
@@ -406,7 +401,7 @@ fn test_powi() {
406401 let neg_inf: f16 = f16:: NEG_INFINITY ;
407402 assert_eq ! ( 1.0f16 . powi( 1 ) , 1.0 ) ;
408403 assert_approx_eq ! ( ( -3.1f16 ) . powi( 2 ) , 9.61 , TOL_0 ) ;
409- assert_approx_eq ! ( 5.9f16 . powi( -2 ) , 0.028727 , TOL_N4 ) ;
404+ assert_approx_eq ! ( 5.9f16 . powi( -2 ) , 0.028727 , TOL_N2 ) ;
410405 assert_eq ! ( 8.3f16 . powi( 0 ) , 1.0 ) ;
411406 assert ! ( nan. powi( 2 ) . is_nan( ) ) ;
412407 assert_eq ! ( inf. powi( 3 ) , inf) ;
@@ -420,9 +415,9 @@ fn test_powf() {
420415 let neg_inf: f16 = f16:: NEG_INFINITY ;
421416 assert_eq ! ( 1.0f16 . powf( 1.0 ) , 1.0 ) ;
422417 assert_approx_eq ! ( 3.4f16 . powf( 4.5 ) , 246.408183 , TOL_P2 ) ;
423- assert_approx_eq ! ( 2.7f16 . powf( -3.2 ) , 0.041652 , TOL_N4 ) ;
418+ assert_approx_eq ! ( 2.7f16 . powf( -3.2 ) , 0.041652 , TOL_N2 ) ;
424419 assert_approx_eq ! ( ( -3.1f16 ) . powf( 2.0 ) , 9.61 , TOL_P2 ) ;
425- assert_approx_eq ! ( 5.9f16 . powf( -2.0 ) , 0.028727 , TOL_N4 ) ;
420+ assert_approx_eq ! ( 5.9f16 . powf( -2.0 ) , 0.028727 , TOL_N2 ) ;
426421 assert_eq ! ( 8.3f16 . powf( 0.0 ) , 1.0 ) ;
427422 assert ! ( nan. powf( 2.0 ) . is_nan( ) ) ;
428423 assert_eq ! ( inf. powf( 2.0 ) , inf) ;
@@ -488,7 +483,7 @@ fn test_log() {
488483 let inf: f16 = f16:: INFINITY ;
489484 let neg_inf: f16 = f16:: NEG_INFINITY ;
490485 assert_eq ! ( 10.0f16 . log( 10.0 ) , 1.0 ) ;
491- assert_approx_eq ! ( 2.3f16 . log( 3.5 ) , 0.664858 , TOL_N2 ) ;
486+ assert_approx_eq ! ( 2.3f16 . log( 3.5 ) , 0.664858 , TOL_0 ) ;
492487 assert_eq ! ( 1.0f16 . exp( ) . log( 1.0f16 . exp( ) ) , 1.0 ) ;
493488 assert ! ( 1.0f16 . log( 1.0 ) . is_nan( ) ) ;
494489 assert ! ( 1.0f16 . log( -13.9 ) . is_nan( ) ) ;
@@ -522,8 +517,8 @@ fn test_log10() {
522517 let inf: f16 = f16:: INFINITY ;
523518 let neg_inf: f16 = f16:: NEG_INFINITY ;
524519 assert_eq ! ( 10.0f16 . log10( ) , 1.0 ) ;
525- assert_approx_eq ! ( 2.3f16 . log10( ) , 0.361728 , TOL_N2 ) ;
526- assert_approx_eq ! ( 1.0f16 . exp( ) . log10( ) , 0.434294 , TOL_N2 ) ;
520+ assert_approx_eq ! ( 2.3f16 . log10( ) , 0.361728 , TOL_0 ) ;
521+ assert_approx_eq ! ( 1.0f16 . exp( ) . log10( ) , 0.434294 , TOL_0 ) ;
527522 assert_eq ! ( 1.0f16 . log10( ) , 0.0 ) ;
528523 assert ! ( nan. log10( ) . is_nan( ) ) ;
529524 assert_eq ! ( inf. log10( ) , inf) ;
@@ -582,9 +577,9 @@ fn test_asinh() {
582577 assert_approx_eq ! ( ( -200.0f16 ) . asinh( ) , -5.991470797049389 , TOL_0 ) ;
583578
584579 // test for low accuracy from issue 104548
585- assert_approx_eq ! ( 60 .0f16, 60 .0f16. sinh( ) . asinh( ) , TOL_0 ) ;
580+ assert_approx_eq ! ( 10 .0f16, 10 .0f16. sinh( ) . asinh( ) , TOL_0 ) ;
586581 // mul needed for approximate comparison to be meaningful
587- assert_approx_eq ! ( 1.0f16 , 1e-15f16 . sinh( ) . asinh( ) * 1e15f16 , TOL_0 ) ;
582+ assert_approx_eq ! ( 1.0f16 , 1e-3f16 . sinh( ) . asinh( ) * 1e3f16 , TOL_0 ) ;
588583}
589584
590585#[ test]
@@ -602,7 +597,7 @@ fn test_acosh() {
602597 assert_approx_eq ! ( 3.0f16 . acosh( ) , 1.76274717403908605046521864995958461f16 , TOL_0 ) ;
603598
604599 // test for low accuracy from issue 104548
605- assert_approx_eq ! ( 60 .0f16, 60 .0f16. cosh( ) . acosh( ) ) ;
600+ assert_approx_eq ! ( 10 .0f16, 10 .0f16. cosh( ) . acosh( ) , TOL_P2 ) ;
606601}
607602
608603#[ test]
@@ -620,8 +615,8 @@ fn test_atanh() {
620615 assert ! ( inf. atanh( ) . is_nan( ) ) ;
621616 assert ! ( neg_inf. atanh( ) . is_nan( ) ) ;
622617 assert ! ( nan. atanh( ) . is_nan( ) ) ;
623- assert_approx_eq ! ( 0.5f16 . atanh( ) , 0.54930614433405484569762261846126285f16 , TOL_N2 ) ;
624- assert_approx_eq ! ( ( -0.5f16 ) . atanh( ) , -0.54930614433405484569762261846126285f16 , TOL_N2 ) ;
618+ assert_approx_eq ! ( 0.5f16 . atanh( ) , 0.54930614433405484569762261846126285f16 , TOL_0 ) ;
619+ assert_approx_eq ! ( ( -0.5f16 ) . atanh( ) , -0.54930614433405484569762261846126285f16 , TOL_0 ) ;
625620}
626621
627622#[ test]
@@ -678,20 +673,20 @@ fn test_real_consts() {
678673 let ln_2: f16 = consts:: LN_2 ;
679674 let ln_10: f16 = consts:: LN_10 ;
680675
681- assert_approx_eq ! ( frac_pi_2, pi / 2 f16, TOL_N2 ) ;
682- assert_approx_eq ! ( frac_pi_3, pi / 3 f16, TOL_N2 ) ;
683- assert_approx_eq ! ( frac_pi_4, pi / 4 f16, TOL_N2 ) ;
684- assert_approx_eq ! ( frac_pi_6, pi / 6 f16, TOL_N2 ) ;
685- assert_approx_eq ! ( frac_pi_8, pi / 8 f16, TOL_N2 ) ;
686- assert_approx_eq ! ( frac_1_pi, 1 f16 / pi, TOL_N2 ) ;
687- assert_approx_eq ! ( frac_2_pi, 2 f16 / pi, TOL_N2 ) ;
688- assert_approx_eq ! ( frac_2_sqrtpi, 2 f16 / pi. sqrt( ) , TOL_N2 ) ;
689- assert_approx_eq ! ( sqrt2, 2 f16. sqrt( ) , TOL_N2 ) ;
690- assert_approx_eq ! ( frac_1_sqrt2, 1 f16 / 2 f16. sqrt( ) , TOL_N2 ) ;
691- assert_approx_eq ! ( log2_e, e. log2( ) , TOL_N2 ) ;
692- assert_approx_eq ! ( log10_e, e. log10( ) , TOL_N2 ) ;
693- assert_approx_eq ! ( ln_2, 2 f16. ln( ) , TOL_N2 ) ;
694- assert_approx_eq ! ( ln_10, 10 f16. ln( ) , TOL_N2 ) ;
676+ assert_approx_eq ! ( frac_pi_2, pi / 2 f16, TOL_0 ) ;
677+ assert_approx_eq ! ( frac_pi_3, pi / 3 f16, TOL_0 ) ;
678+ assert_approx_eq ! ( frac_pi_4, pi / 4 f16, TOL_0 ) ;
679+ assert_approx_eq ! ( frac_pi_6, pi / 6 f16, TOL_0 ) ;
680+ assert_approx_eq ! ( frac_pi_8, pi / 8 f16, TOL_0 ) ;
681+ assert_approx_eq ! ( frac_1_pi, 1 f16 / pi, TOL_0 ) ;
682+ assert_approx_eq ! ( frac_2_pi, 2 f16 / pi, TOL_0 ) ;
683+ assert_approx_eq ! ( frac_2_sqrtpi, 2 f16 / pi. sqrt( ) , TOL_0 ) ;
684+ assert_approx_eq ! ( sqrt2, 2 f16. sqrt( ) , TOL_0 ) ;
685+ assert_approx_eq ! ( frac_1_sqrt2, 1 f16 / 2 f16. sqrt( ) , TOL_0 ) ;
686+ assert_approx_eq ! ( log2_e, e. log2( ) , TOL_0 ) ;
687+ assert_approx_eq ! ( log10_e, e. log10( ) , TOL_0 ) ;
688+ assert_approx_eq ! ( ln_2, 2 f16. ln( ) , TOL_0 ) ;
689+ assert_approx_eq ! ( ln_10, 10 f16. ln( ) , TOL_0 ) ;
695690}
696691
697692#[ test]
0 commit comments