xaxxon - 2 months ago 13

C++ Question

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.

Answer

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