In GTest, how to check for number very close to 0? EXPECT_FLOAT_EQ not working

廉价感情. 提交于 2020-01-30 05:49:08

问题


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);

I get errors like:

/path/to/my/test.cpp:148: Failure
      Expected: my_computed_var
      Which is: 9.9920072e-16
      To be equal to: 0

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.


回答1:


Use EXPECT_NEAR or the FloatEq matcher instead.

Floating-Point Macros said that EXPECT_FLOAT_EQ EXPECT_DOUBLE_EQ ASSERT_FLOAT_EQ and ASSERT_DOUBLE_EQ will verify

the two float/double values are almost equal

and:

By "almost equal", we mean the two values are within 4 ULP's from each other.

with ULP is stand for Unit in the last places



来源:https://stackoverflow.com/questions/40232221/in-gtest-how-to-check-for-number-very-close-to-0-expect-float-eq-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!