我有一个测试断言,如下所示
assert_eq!(-0.000031989493, res);这对测试很有效。但是当我在测试中运行Clippy时,它会报错。
error: strict comparison of `f32` or `f64`
--> src/main.rs:351:9
|
351 | assert_eq!(-0.000031989493, res);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)我已经查看了提供的链接https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp,并将其重写为以下内容。
assert_eq!(
true,
(res - -0.000031989493).abs() < f32::EPSILON
);但这会降低测试的可读性,如果测试失败,我在输出中看不到测试的结果。
我应该如何编写我的f32比较断言,以使Clippy满意,并且使测试在失败时仍在终端中输出值?
或者我不应该在我的测试中运行Clippy?
谢谢!
发布于 2021-01-25 03:03:43
好了,我已经复制并修改了assert_eq宏
macro_rules! assert_almost_eq {
($left:expr, $right:expr, $prec:expr) => {{
match (&$left, &$right) {
(left_val, right_val) => {
let diff = (left_val - right_val).abs();
if diff > $prec {
panic!(
"assertion failed: `(left == right)`\n left: `{:?}`,\n right: `{:?}`",
&*left_val, &*right_val
)
}
}
}
}};
}我并不是真的理解所有的事情,比如比赛的原因,但它似乎解决了我的问题。
https://stackoverflow.com/questions/65874156
复制相似问题