Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 927
xaxxon Avatar asked Oct 25 '16 05:10

xaxxon


1 Answers

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

like image 64
Danh Avatar answered Nov 14 '22 21:11

Danh