5
5
use crate :: f16:: consts;
6
6
use crate :: num:: * ;
7
7
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
-
13
8
/// Tolerance for results on the order of 10.0e-2;
14
9
const TOL_N2 : f16 = 0.0001 ;
15
10
@@ -257,16 +252,16 @@ fn test_trunc() {
257
252
258
253
#[ test]
259
254
fn 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 ) ;
270
265
}
271
266
272
267
#[ test]
@@ -406,7 +401,7 @@ fn test_powi() {
406
401
let neg_inf: f16 = f16:: NEG_INFINITY ;
407
402
assert_eq ! ( 1.0f16 . powi( 1 ) , 1.0 ) ;
408
403
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 ) ;
410
405
assert_eq ! ( 8.3f16 . powi( 0 ) , 1.0 ) ;
411
406
assert ! ( nan. powi( 2 ) . is_nan( ) ) ;
412
407
assert_eq ! ( inf. powi( 3 ) , inf) ;
@@ -420,9 +415,9 @@ fn test_powf() {
420
415
let neg_inf: f16 = f16:: NEG_INFINITY ;
421
416
assert_eq ! ( 1.0f16 . powf( 1.0 ) , 1.0 ) ;
422
417
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 ) ;
424
419
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 ) ;
426
421
assert_eq ! ( 8.3f16 . powf( 0.0 ) , 1.0 ) ;
427
422
assert ! ( nan. powf( 2.0 ) . is_nan( ) ) ;
428
423
assert_eq ! ( inf. powf( 2.0 ) , inf) ;
@@ -488,7 +483,7 @@ fn test_log() {
488
483
let inf: f16 = f16:: INFINITY ;
489
484
let neg_inf: f16 = f16:: NEG_INFINITY ;
490
485
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 ) ;
492
487
assert_eq ! ( 1.0f16 . exp( ) . log( 1.0f16 . exp( ) ) , 1.0 ) ;
493
488
assert ! ( 1.0f16 . log( 1.0 ) . is_nan( ) ) ;
494
489
assert ! ( 1.0f16 . log( -13.9 ) . is_nan( ) ) ;
@@ -522,8 +517,8 @@ fn test_log10() {
522
517
let inf: f16 = f16:: INFINITY ;
523
518
let neg_inf: f16 = f16:: NEG_INFINITY ;
524
519
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 ) ;
527
522
assert_eq ! ( 1.0f16 . log10( ) , 0.0 ) ;
528
523
assert ! ( nan. log10( ) . is_nan( ) ) ;
529
524
assert_eq ! ( inf. log10( ) , inf) ;
@@ -582,9 +577,9 @@ fn test_asinh() {
582
577
assert_approx_eq ! ( ( -200.0f16 ) . asinh( ) , -5.991470797049389 , TOL_0 ) ;
583
578
584
579
// 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 ) ;
586
581
// 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 ) ;
588
583
}
589
584
590
585
#[ test]
@@ -602,7 +597,7 @@ fn test_acosh() {
602
597
assert_approx_eq ! ( 3.0f16 . acosh( ) , 1.76274717403908605046521864995958461f16 , TOL_0 ) ;
603
598
604
599
// 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 ) ;
606
601
}
607
602
608
603
#[ test]
@@ -620,8 +615,8 @@ fn test_atanh() {
620
615
assert ! ( inf. atanh( ) . is_nan( ) ) ;
621
616
assert ! ( neg_inf. atanh( ) . is_nan( ) ) ;
622
617
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 ) ;
625
620
}
626
621
627
622
#[ test]
@@ -678,20 +673,20 @@ fn test_real_consts() {
678
673
let ln_2: f16 = consts:: LN_2 ;
679
674
let ln_10: f16 = consts:: LN_10 ;
680
675
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 ) ;
695
690
}
696
691
697
692
#[ test]
0 commit comments