1
1
use std:: f32:: consts;
2
2
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
+
3
8
#[ allow( unused_macros) ]
4
9
macro_rules! assert_f32_biteq {
5
10
( $left : expr, $right : expr) => {
@@ -17,9 +22,17 @@ fn test_powf() {
17
22
let inf: f32 = f32:: INFINITY ;
18
23
let neg_inf: f32 = f32:: NEG_INFINITY ;
19
24
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
+ ) ;
21
30
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
+ ) ;
23
36
assert_approx_eq ! ( 5.9f32 . powf( -2.0 ) , 0.028727 ) ;
24
37
assert_eq ! ( 8.3f32 . powf( 0.0 ) , 1.0 ) ;
25
38
assert ! ( nan. powf( 2.0 ) . is_nan( ) ) ;
@@ -30,8 +43,12 @@ fn test_powf() {
30
43
#[ test]
31
44
fn test_exp ( ) {
32
45
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
+ ) ;
35
52
36
53
let inf: f32 = f32:: INFINITY ;
37
54
let neg_inf: f32 = f32:: NEG_INFINITY ;
@@ -43,7 +60,11 @@ fn test_exp() {
43
60
44
61
#[ test]
45
62
fn 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
+ ) ;
47
68
assert_eq ! ( 1.0 , 0.0f32 . exp2( ) ) ;
48
69
49
70
let inf: f32 = f32:: INFINITY ;
@@ -66,17 +87,21 @@ fn test_ln() {
66
87
assert ! ( ( -2.3f32 ) . ln( ) . is_nan( ) ) ;
67
88
assert_eq ! ( ( -0.0f32 ) . ln( ) , neg_inf) ;
68
89
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
+ ) ;
70
95
}
71
96
72
97
#[ test]
73
98
fn test_log ( ) {
74
99
let nan: f32 = f32:: NAN ;
75
100
let inf: f32 = f32:: INFINITY ;
76
101
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 ) ;
78
103
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 ) ;
80
105
assert ! ( 1.0f32 . log( 1.0 ) . is_nan( ) ) ;
81
106
assert ! ( 1.0f32 . log( -13.9 ) . is_nan( ) ) ;
82
107
assert ! ( nan. log( 2.3 ) . is_nan( ) ) ;
@@ -92,9 +117,17 @@ fn test_log2() {
92
117
let nan: f32 = f32:: NAN ;
93
118
let inf: f32 = f32:: INFINITY ;
94
119
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
+ ) ;
96
125
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
+ ) ;
98
131
assert ! ( nan. log2( ) . is_nan( ) ) ;
99
132
assert_eq ! ( inf. log2( ) , inf) ;
100
133
assert ! ( neg_inf. log2( ) . is_nan( ) ) ;
@@ -108,7 +141,7 @@ fn test_log10() {
108
141
let nan: f32 = f32:: NAN ;
109
142
let inf: f32 = f32:: INFINITY ;
110
143
let neg_inf: f32 = f32:: NEG_INFINITY ;
111
- assert_eq ! ( 10.0f32 . log10( ) , 1.0 ) ;
144
+ assert_approx_eq ! ( 10.0f32 . log10( ) , 1.0 ) ;
112
145
assert_approx_eq ! ( 2.3f32 . log10( ) , 0.361728 ) ;
113
146
assert_approx_eq ! ( 1.0f32 . exp( ) . log10( ) , 0.434294 ) ;
114
147
assert_eq ! ( 1.0f32 . log10( ) , 0.0 ) ;
@@ -158,7 +191,11 @@ fn test_acosh() {
158
191
assert_approx_eq ! ( 3.0f32 . acosh( ) , 1.76274717403908605046521864995958461f32 ) ;
159
192
160
193
// 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
+ ) ;
162
199
}
163
200
164
201
#[ test]
@@ -237,7 +274,11 @@ fn test_real_consts() {
237
274
let ln_10: f32 = consts:: LN_10 ;
238
275
239
276
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
+ ) ;
241
282
assert_approx_eq ! ( frac_pi_4, pi / 4f32 ) ;
242
283
assert_approx_eq ! ( frac_pi_6, pi / 6f32 ) ;
243
284
assert_approx_eq ! ( frac_pi_8, pi / 8f32 ) ;
@@ -249,5 +290,9 @@ fn test_real_consts() {
249
290
assert_approx_eq ! ( log2_e, e. log2( ) ) ;
250
291
assert_approx_eq ! ( log10_e, e. log10( ) ) ;
251
292
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
+ ) ;
253
298
}
0 commit comments