11use std:: f32:: consts;
22
3+ /// Miri adds some extra errors to float functions; make sure the tests still pass.
4+ /// These values are purely used as a canary to test against and are thus not a stable guarantee Rust provides.
5+ /// They serve as a way to get an idea of the real precision of floating point operations on different platforms.
6+ const APPROX_DELTA : f32 = if cfg ! ( miri) { 1e-4 } else { 1e-6 } ;
7+
38#[ allow( unused_macros) ]
49macro_rules! assert_f32_biteq {
510 ( $left : expr, $right : expr) => {
@@ -17,9 +22,17 @@ fn test_powf() {
1722 let inf: f32 = f32:: INFINITY ;
1823 let neg_inf: f32 = f32:: NEG_INFINITY ;
1924 assert_eq ! ( 1.0f32 . powf( 1.0 ) , 1.0 ) ;
20- assert_approx_eq ! ( 3.4f32 . powf( 4.5 ) , 246.408218 ) ;
25+ assert_approx_eq ! (
26+ 3.4f32 . powf( 4.5 ) ,
27+ 246.408218 ,
28+ APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
29+ ) ;
2130 assert_approx_eq ! ( 2.7f32 . powf( -3.2 ) , 0.041652 ) ;
22- assert_approx_eq ! ( ( -3.1f32 ) . powf( 2.0 ) , 9.61 ) ;
31+ assert_approx_eq ! (
32+ ( -3.1f32 ) . powf( 2.0 ) ,
33+ 9.61 ,
34+ APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
35+ ) ;
2336 assert_approx_eq ! ( 5.9f32 . powf( -2.0 ) , 0.028727 ) ;
2437 assert_eq ! ( 8.3f32 . powf( 0.0 ) , 1.0 ) ;
2538 assert ! ( nan. powf( 2.0 ) . is_nan( ) ) ;
@@ -30,8 +43,12 @@ fn test_powf() {
3043#[ test]
3144fn test_exp ( ) {
3245 assert_eq ! ( 1.0 , 0.0f32 . exp( ) ) ;
33- assert_approx_eq ! ( 2.718282 , 1.0f32 . exp( ) ) ;
34- assert_approx_eq ! ( 148.413162 , 5.0f32 . exp( ) ) ;
46+ assert_approx_eq ! ( 2.718282 , 1.0f32 . exp( ) , APPROX_DELTA ) ;
47+ assert_approx_eq ! (
48+ 148.413162 ,
49+ 5.0f32 . exp( ) ,
50+ APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
51+ ) ;
3552
3653 let inf: f32 = f32:: INFINITY ;
3754 let neg_inf: f32 = f32:: NEG_INFINITY ;
@@ -43,7 +60,11 @@ fn test_exp() {
4360
4461#[ test]
4562fn test_exp2 ( ) {
46- assert_eq ! ( 32.0 , 5.0f32 . exp2( ) ) ;
63+ assert_approx_eq ! (
64+ 32.0 ,
65+ 5.0f32 . exp2( ) ,
66+ APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
67+ ) ;
4768 assert_eq ! ( 1.0 , 0.0f32 . exp2( ) ) ;
4869
4970 let inf: f32 = f32:: INFINITY ;
@@ -66,17 +87,21 @@ fn test_ln() {
6687 assert ! ( ( -2.3f32 ) . ln( ) . is_nan( ) ) ;
6788 assert_eq ! ( ( -0.0f32 ) . ln( ) , neg_inf) ;
6889 assert_eq ! ( 0.0f32 . ln( ) , neg_inf) ;
69- assert_approx_eq ! ( 4.0f32 . ln( ) , 1.386294 ) ;
90+ assert_approx_eq ! (
91+ 4.0f32 . ln( ) ,
92+ 1.386294 ,
93+ APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
94+ ) ;
7095}
7196
7297#[ test]
7398fn test_log ( ) {
7499 let nan: f32 = f32:: NAN ;
75100 let inf: f32 = f32:: INFINITY ;
76101 let neg_inf: f32 = f32:: NEG_INFINITY ;
77- assert_eq ! ( 10.0f32 . log( 10.0 ) , 1.0 ) ;
102+ assert_approx_eq ! ( 10.0f32 . log( 10.0 ) , 1.0 ) ;
78103 assert_approx_eq ! ( 2.3f32 . log( 3.5 ) , 0.664858 ) ;
79- assert_eq ! ( 1.0f32 . exp( ) . log( 1.0f32 . exp( ) ) , 1.0 ) ;
104+ assert_approx_eq ! ( 1.0f32 . exp( ) . log( 1.0f32 . exp( ) ) , 1.0 ) ;
80105 assert ! ( 1.0f32 . log( 1.0 ) . is_nan( ) ) ;
81106 assert ! ( 1.0f32 . log( -13.9 ) . is_nan( ) ) ;
82107 assert ! ( nan. log( 2.3 ) . is_nan( ) ) ;
@@ -92,9 +117,17 @@ fn test_log2() {
92117 let nan: f32 = f32:: NAN ;
93118 let inf: f32 = f32:: INFINITY ;
94119 let neg_inf: f32 = f32:: NEG_INFINITY ;
95- assert_approx_eq ! ( 10.0f32 . log2( ) , 3.321928 ) ;
120+ assert_approx_eq ! (
121+ 10.0f32 . log2( ) ,
122+ 3.321928 ,
123+ APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
124+ ) ;
96125 assert_approx_eq ! ( 2.3f32 . log2( ) , 1.201634 ) ;
97- assert_approx_eq ! ( 1.0f32 . exp( ) . log2( ) , 1.442695 ) ;
126+ assert_approx_eq ! (
127+ 1.0f32 . exp( ) . log2( ) ,
128+ 1.442695 ,
129+ APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
130+ ) ;
98131 assert ! ( nan. log2( ) . is_nan( ) ) ;
99132 assert_eq ! ( inf. log2( ) , inf) ;
100133 assert ! ( neg_inf. log2( ) . is_nan( ) ) ;
@@ -108,7 +141,7 @@ fn test_log10() {
108141 let nan: f32 = f32:: NAN ;
109142 let inf: f32 = f32:: INFINITY ;
110143 let neg_inf: f32 = f32:: NEG_INFINITY ;
111- assert_eq ! ( 10.0f32 . log10( ) , 1.0 ) ;
144+ assert_approx_eq ! ( 10.0f32 . log10( ) , 1.0 ) ;
112145 assert_approx_eq ! ( 2.3f32 . log10( ) , 0.361728 ) ;
113146 assert_approx_eq ! ( 1.0f32 . exp( ) . log10( ) , 0.434294 ) ;
114147 assert_eq ! ( 1.0f32 . log10( ) , 0.0 ) ;
@@ -158,7 +191,11 @@ fn test_acosh() {
158191 assert_approx_eq ! ( 3.0f32 . acosh( ) , 1.76274717403908605046521864995958461f32 ) ;
159192
160193 // test for low accuracy from issue 104548
161- assert_approx_eq ! ( 60.0f32 , 60.0f32 . cosh( ) . acosh( ) ) ;
194+ assert_approx_eq ! (
195+ 60.0f32 ,
196+ 60.0f32 . cosh( ) . acosh( ) ,
197+ APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
198+ ) ;
162199}
163200
164201#[ test]
@@ -237,7 +274,11 @@ fn test_real_consts() {
237274 let ln_10: f32 = consts:: LN_10 ;
238275
239276 assert_approx_eq ! ( frac_pi_2, pi / 2f32 ) ;
240- assert_approx_eq ! ( frac_pi_3, pi / 3f32 ) ;
277+ assert_approx_eq ! (
278+ frac_pi_3,
279+ pi / 3f32 ,
280+ APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
281+ ) ;
241282 assert_approx_eq ! ( frac_pi_4, pi / 4f32 ) ;
242283 assert_approx_eq ! ( frac_pi_6, pi / 6f32 ) ;
243284 assert_approx_eq ! ( frac_pi_8, pi / 8f32 ) ;
@@ -249,5 +290,9 @@ fn test_real_consts() {
249290 assert_approx_eq ! ( log2_e, e. log2( ) ) ;
250291 assert_approx_eq ! ( log10_e, e. log10( ) ) ;
251292 assert_approx_eq ! ( ln_2, 2f32 . ln( ) ) ;
252- assert_approx_eq ! ( ln_10, 10f32 . ln( ) ) ;
293+ assert_approx_eq ! (
294+ ln_10,
295+ 10f32 . ln( ) ,
296+ APPROX_DELTA /* Miri float-non-det: Make tests pass for now */
297+ ) ;
253298}
0 commit comments