问题描述
我有一些代码可以计算(a)sin/(a)cos值,这些值预计非常接近0,但是当我说
I have some code that computes (a)sin/(a)cos values which are expected to be very close to 0, but when I say
EXPECT_FLOAT_EQ(my_computed_var, 0);
我收到如下错误:
/path/to/my/test.cpp:148: Failure
Expected: my_computed_var
Which is: 9.9920072e-16
To be equal to: 0
我应该怎么做?对于其他数字,我也遇到这种类型的错误":-4.3711388e-08
How should I do this? I get this type of "error" for other numbers as well: -4.3711388e-08
寻找有关处理此类浮点错误的具体建议和一般信息.
Looking for both specific suggestions and general information about dealing with floating point error like this.
推荐答案
使用 EXPECT_NEAR
或 FloatEq
匹配器.
Use EXPECT_NEAR
or the FloatEq
matcher instead.
浮点宏表示EXPECT_FLOAT_EQ
EXPECT_DOUBLE_EQ
ASSERT_FLOAT_EQ
和ASSERT_DOUBLE_EQ
将进行验证
Floating-Point Macros said that EXPECT_FLOAT_EQ
EXPECT_DOUBLE_EQ
ASSERT_FLOAT_EQ
and ASSERT_DOUBLE_EQ
will verify
和:
ULP代表最后一个单元
这篇关于在GTest中,如何检查非常接近0的数字? EXPECT_FLOAT_EQ不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!