Skip to content

Commit a64db7e

Browse files
accept custom ULP for approx check
1 parent 8547902 commit a64db7e

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

tests/pass/float.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{f32, f64};
1616
const ALLOWED_ULP_DIFF: i128 = 64;
1717

1818
macro_rules! assert_approx_eq {
19-
($a:expr, $b:expr) => {{
19+
($a:expr, $b:expr, $ulp:expr) => {{
2020
let (a, b) = (&$a, &$b);
2121
let a_signum = a.signum();
2222
let b_signum = b.signum();
@@ -27,17 +27,22 @@ macro_rules! assert_approx_eq {
2727
}
2828
let a_bits = a.to_bits() as i128;
2929
let b_bits = b.to_bits() as i128;
30+
let allowed_ulp = $ulp;
3031
let ulp_difference = (a_bits - b_bits).abs();
3132
assert!(
32-
ulp_difference <= ALLOWED_ULP_DIFF,
33+
ulp_difference <= allowed_ulp,
3334
"
3435
{:?} is not approximately equal to {:?}
35-
ulp diff: {ulp_difference} > {ALLOWED_ULP_DIFF}
36+
ulp diff: {ulp_difference} > {allowed_ulp}
3637
",
3738
*a,
3839
*b
3940
);
4041
}};
42+
43+
($a:expr, $b: expr) => {
44+
assert_approx_eq!($a, $b, ALLOWED_ULP_DIFF);
45+
};
4146
}
4247

4348
fn main() {
@@ -1248,27 +1253,27 @@ fn test_non_determinism() {
12481253
// seperate operations (unlikely but possible)
12491254
// and thus this `assert_ne!` can "sometimes" fail.
12501255
macro_rules! test_operation {
1251-
($a:expr, $b:expr, $op:path) => {
1256+
($a:expr, $b:expr, $op:path $(, $ulp:expr)?) => {
12521257
// 5 times enough?
12531258
let results: [_; 5] = ::core::array::from_fn(|i| (i, $op($a, $b)));
12541259
for (idx1, res1) in results {
12551260
for (idx2, res2) in results {
12561261
if idx1 == idx2 {
12571262
continue;
12581263
}
1259-
assert_approx_eq!(res1, res2);
1264+
assert_approx_eq!(res1, res2$(, $ulp)?);
12601265
}
12611266
}
12621267
};
1263-
($a:expr, $op:path) => {
1268+
($a:expr, $op:path $(, $ulp:expr)?) => {
12641269
// 5 times enough?
12651270
let results: [_; 5] = ::core::array::from_fn(|i| (i, $op($a)));
12661271
for (idx1, res1) in results {
12671272
for (idx2, res2) in results {
12681273
if idx1 == idx2 {
12691274
continue;
12701275
}
1271-
assert_approx_eq!(res1, res2);
1276+
assert_approx_eq!(res1, res2 $(, $ulp)?);
12721277
}
12731278
}
12741279
};
@@ -1297,12 +1302,14 @@ fn test_non_determinism() {
12971302
}
12981303
pub fn test_operations_f32(a: f32, b: f32) {
12991304
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);
13011307
test_operation!(a, f32::exp);
13021308
}
13031309
pub fn test_operations_f64(a: f64, b: f64) {
13041310
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);
13061313
test_operation!(a, f64::exp);
13071314
}
13081315
pub fn test_operations_f128(a: f128, b: f128) {

0 commit comments

Comments
 (0)