Skip to content

Commit 1bedc42

Browse files
committed
Use approximate comparisons for floats
1 parent 676ebef commit 1bedc42

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

examples/stencil/src/lib.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,24 @@ fn assert_data_eq(a: &Data, b: &Data) {
133133
for x in 0..a.n.0 {
134134
let idx = (x + y * a.n.1 + z * a.n.1 * a.n.0) as usize;
135135

136-
assert_eq!(
137-
a.vsq[idx], b.vsq[idx],
136+
const EPSILON: f32 = 1E-4;
137+
138+
assert!(
139+
(a.vsq[idx] - b.vsq[idx]).abs() < EPSILON,
138140
"vsq diff at idx = {} ({}, {}, {})",
139-
idx, x, y, z
141+
idx, x, y, z,
140142
);
141143

142-
assert_eq!(
143-
a.a.0[idx], b.a.0[idx],
144+
assert!(
145+
(a.a.0[idx] - b.a.0[idx]).abs() < EPSILON,
144146
"a.0 diff at idx = {} ({}, {}, {})",
145-
idx, x, y, z
147+
idx, x, y, z,
146148
);
147149

148-
assert_eq!(
149-
a.a.1[idx], b.a.1[idx],
150+
assert!(
151+
(a.a.1[idx] - b.a.1[idx]).abs() < EPSILON,
150152
"a.1 diff at idx = {} ({}, {}, {})",
151-
idx, x, y, z
153+
idx, x, y, z,
152154
);
153155
}
154156
}

examples/stencil/src/scalar.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ pub fn scalar(
6363
#[cfg(all(test, feature = "ispc"))]
6464
mod tests {
6565
use super::scalar;
66-
use {ispc_loops::serial, Data};
66+
use ispc_loops::serial;
67+
use {assert_data_eq, Data};
6768

6869
#[test]
6970

@@ -74,6 +75,6 @@ mod tests {
7475
let mut data_ispc = Data::default();
7576
data_ispc.exec(serial);
7677

77-
assert_eq!(data_scalar, data_ispc);
78+
assert_data_eq(&data_scalar, &data_ispc);
7879
}
7980
}

0 commit comments

Comments
 (0)