@@ -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 ( ) {
@@ -1276,27 +1281,27 @@ fn test_non_determinism() {
1276
1281
// seperate operations (unlikely but possible)
1277
1282
// and thus this `assert_ne!` can "sometimes" fail.
1278
1283
macro_rules! test_operation {
1279
- ( $a: expr , $b: expr , $op: path ) => {
1284
+ ( $a: expr , $b: expr , $op: path $ ( , $ulp : expr ) ? ) => {
1280
1285
// 5 times enough?
1281
1286
let results: [ _; 5 ] = :: core:: array:: from_fn ( |i| ( i, $op( $a, $b) ) ) ;
1282
1287
for ( idx1, res1) in results {
1283
1288
for ( idx2, res2) in results {
1284
1289
if idx1 == idx2 {
1285
1290
continue ;
1286
1291
}
1287
- assert_approx_eq ! ( res1, res2) ;
1292
+ assert_approx_eq ! ( res1, res2$ ( , $ulp ) ? ) ;
1288
1293
}
1289
1294
}
1290
1295
} ;
1291
- ( $a: expr, $op: path) => {
1296
+ ( $a: expr, $op: path $ ( , $ulp : expr ) ? ) => {
1292
1297
// 5 times enough?
1293
1298
let results: [ _; 5 ] = :: core:: array:: from_fn ( |i| ( i, $op( $a) ) ) ;
1294
1299
for ( idx1, res1) in results {
1295
1300
for ( idx2, res2) in results {
1296
1301
if idx1 == idx2 {
1297
1302
continue ;
1298
1303
}
1299
- assert_approx_eq ! ( res1, res2) ;
1304
+ assert_approx_eq ! ( res1, res2 $ ( , $ulp ) ? ) ;
1300
1305
}
1301
1306
}
1302
1307
} ;
@@ -1325,12 +1330,14 @@ fn test_non_determinism() {
1325
1330
}
1326
1331
pub fn test_operations_f32 ( a : f32 , b : f32 ) {
1327
1332
test_operations_f ! ( a, b) ;
1328
- test_operation ! ( a, b, f32 :: log) ;
1333
+ // custom ulp for log
1334
+ test_operation ! ( a, b, f32 :: log, 128 ) ;
1329
1335
test_operation ! ( a, f32 :: exp) ;
1330
1336
}
1331
1337
pub fn test_operations_f64 ( a : f64 , b : f64 ) {
1332
1338
test_operations_f ! ( a, b) ;
1333
- test_operation ! ( a, b, f64 :: log) ;
1339
+ // custom ulp for log
1340
+ test_operation ! ( a, b, f64 :: log, 128 ) ;
1334
1341
test_operation ! ( a, f64 :: exp) ;
1335
1342
}
1336
1343
pub fn test_operations_f128 ( a : f128 , b : f128 ) {
0 commit comments