@@ -16,7 +16,7 @@ use std::{f32, f64};
16
16
const ALLOWED_ULP_DIFF : i128 = 64 ;
17
17
18
18
macro_rules! assert_approx_eq {
19
- ( $a: expr, $b: expr) => { {
19
+ ( $a: expr, $b: expr, $ulp : expr ) => { {
20
20
let ( a, b) = ( & $a, & $b) ;
21
21
let a_signum = a. signum( ) ;
22
22
let b_signum = b. signum( ) ;
@@ -27,17 +27,22 @@ macro_rules! assert_approx_eq {
27
27
}
28
28
let a_bits = a. to_bits( ) as i128 ;
29
29
let b_bits = b. to_bits( ) as i128 ;
30
+ let allowed_ulp = $ulp;
30
31
let ulp_difference = ( a_bits - b_bits) . abs( ) ;
31
32
assert!(
32
- ulp_difference <= ALLOWED_ULP_DIFF ,
33
+ ulp_difference <= allowed_ulp ,
33
34
"
34
35
{:?} is not approximately equal to {:?}
35
- ulp diff: {ulp_difference} > {ALLOWED_ULP_DIFF }
36
+ ulp diff: {ulp_difference} > {allowed_ulp }
36
37
" ,
37
38
* a,
38
39
* b
39
40
) ;
40
41
} } ;
42
+
43
+ ( $a: expr, $b: expr) => {
44
+ assert_approx_eq!( $a, $b, ALLOWED_ULP_DIFF ) ;
45
+ } ;
41
46
}
42
47
43
48
fn main ( ) {
@@ -1248,27 +1253,27 @@ fn test_non_determinism() {
1248
1253
// seperate operations (unlikely but possible)
1249
1254
// and thus this `assert_ne!` can "sometimes" fail.
1250
1255
macro_rules! test_operation {
1251
- ( $a: expr, $b: expr, $op: path) => {
1256
+ ( $a: expr, $b: expr, $op: path $ ( , $ulp : expr ) ? ) => {
1252
1257
// 5 times enough?
1253
1258
let results: [ _; 5 ] = :: core:: array:: from_fn( |i| ( i, $op( $a, $b) ) ) ;
1254
1259
for ( idx1, res1) in results {
1255
1260
for ( idx2, res2) in results {
1256
1261
if idx1 == idx2 {
1257
1262
continue ;
1258
1263
}
1259
- assert_approx_eq!( res1, res2) ;
1264
+ assert_approx_eq!( res1, res2$ ( , $ulp ) ? ) ;
1260
1265
}
1261
1266
}
1262
1267
} ;
1263
- ( $a: expr, $op: path) => {
1268
+ ( $a: expr, $op: path $ ( , $ulp : expr ) ? ) => {
1264
1269
// 5 times enough?
1265
1270
let results: [ _; 5 ] = :: core:: array:: from_fn( |i| ( i, $op( $a) ) ) ;
1266
1271
for ( idx1, res1) in results {
1267
1272
for ( idx2, res2) in results {
1268
1273
if idx1 == idx2 {
1269
1274
continue ;
1270
1275
}
1271
- assert_approx_eq!( res1, res2) ;
1276
+ assert_approx_eq!( res1, res2 $ ( , $ulp ) ? ) ;
1272
1277
}
1273
1278
}
1274
1279
} ;
@@ -1297,12 +1302,14 @@ fn test_non_determinism() {
1297
1302
}
1298
1303
pub fn test_operations_f32 ( a : f32 , b : f32 ) {
1299
1304
test_operations_f ! ( a, b) ;
1300
- test_operation ! ( a, b, f32 :: log) ;
1305
+ // custom ulp for log
1306
+ test_operation ! ( a, b, f32 :: log, 128 ) ;
1301
1307
test_operation ! ( a, f32 :: exp) ;
1302
1308
}
1303
1309
pub fn test_operations_f64 ( a : f64 , b : f64 ) {
1304
1310
test_operations_f ! ( a, b) ;
1305
- test_operation ! ( a, b, f64 :: log) ;
1311
+ // custom ulp for log
1312
+ test_operation ! ( a, b, f64 :: log, 128 ) ;
1306
1313
test_operation ! ( a, f64 :: exp) ;
1307
1314
}
1308
1315
pub fn test_operations_f128 ( a : f128 , b : f128 ) {
0 commit comments