Skip to content

Commit e32f545

Browse files
authored
Use approximate comparisons for pow tests (#7646)
1 parent 8d6cd76 commit e32f545

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

arrow-array/src/arithmetic.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,24 @@ native_type_float_op!(
433433
mod tests {
434434
use super::*;
435435

436+
macro_rules! assert_approx_eq {
437+
( $x: expr, $y: expr ) => {{
438+
assert_approx_eq!($x, $y, 1.0e-4)
439+
}};
440+
( $x: expr, $y: expr, $tol: expr ) => {{
441+
let x_val = $x;
442+
let y_val = $y;
443+
let diff = f64::from((x_val - y_val).abs());
444+
assert!(
445+
diff <= $tol,
446+
"{} != {} (with tolerance = {})",
447+
x_val,
448+
y_val,
449+
$tol
450+
);
451+
}};
452+
}
453+
436454
#[test]
437455
fn test_native_type_is_zero() {
438456
assert!(0_i8.is_zero());
@@ -803,9 +821,9 @@ mod tests {
803821
assert_eq!(8_u16.pow_wrapping(2_u32), 64_u16);
804822
assert_eq!(8_u32.pow_wrapping(2_u32), 64_u32);
805823
assert_eq!(8_u64.pow_wrapping(2_u32), 64_u64);
806-
assert_eq!(f16::from_f32(8.0).pow_wrapping(2_u32), f16::from_f32(64.0));
807-
assert_eq!(8.0_f32.pow_wrapping(2_u32), 64_f32);
808-
assert_eq!(8.0_f64.pow_wrapping(2_u32), 64_f64);
824+
assert_approx_eq!(f16::from_f32(8.0).pow_wrapping(2_u32), f16::from_f32(64.0));
825+
assert_approx_eq!(8.0_f32.pow_wrapping(2_u32), 64_f32);
826+
assert_approx_eq!(8.0_f64.pow_wrapping(2_u32), 64_f64);
809827

810828
// pow_checked
811829
assert_eq!(8_i8.pow_checked(2_u32).unwrap(), 64_i8);
@@ -821,12 +839,12 @@ mod tests {
821839
assert_eq!(8_u16.pow_checked(2_u32).unwrap(), 64_u16);
822840
assert_eq!(8_u32.pow_checked(2_u32).unwrap(), 64_u32);
823841
assert_eq!(8_u64.pow_checked(2_u32).unwrap(), 64_u64);
824-
assert_eq!(
842+
assert_approx_eq!(
825843
f16::from_f32(8.0).pow_checked(2_u32).unwrap(),
826844
f16::from_f32(64.0)
827845
);
828-
assert_eq!(8.0_f32.pow_checked(2_u32).unwrap(), 64_f32);
829-
assert_eq!(8.0_f64.pow_checked(2_u32).unwrap(), 64_f64);
846+
assert_approx_eq!(8.0_f32.pow_checked(2_u32).unwrap(), 64_f32);
847+
assert_approx_eq!(8.0_f64.pow_checked(2_u32).unwrap(), 64_f64);
830848
}
831849

832850
#[test]

0 commit comments

Comments
 (0)