3

I'm having some trouble testing floating point values for equality with Google Test 1.7.0.

My assertion looks like this:

ASSERT_NEAR(124691356.375f, actual, DELTA);

The test fails with the following error:

The difference between 124691356.375f and actual is 3.625, which exceeds DELTA, where 124691356.375f evaluates to 124691360, actual evaluates to 124691356.375, and DELTA evaluates to 0.0625.

What's going on? actual and the expected result are clearly within the allowable error of 0.0625. Why is gtest evaluating the floating point literal 124691356.375f this way?

Update: DELTA and actual are of type double, and the expected value is a float literal. If I change the literal to be a double, or change the other arguments to be floats (so that everything is of the same type) the test passes. The question still stands - what is causing this behavior when the types don't match?

zmb
  • 7,605
  • 4
  • 40
  • 55

1 Answers1

2

124691356.375f is a single precision floating point number, which has 7-8 significant decimal digits. So it is "rounded" to 124691360, which is not within the allowable error. You should problably use 124691356.375lf. See also wikipedia and this question.

Community
  • 1
  • 1
wimh
  • 15,072
  • 6
  • 47
  • 98
  • Thanks! The real kicker was the fact that the calculation of `actual` stored the result in a double, which has enough precision not to perform the same "rounding." – zmb Dec 15 '14 at 21:03
  • or just omit the type specifier entirely for the literal - then it will be a `double` – Red Alert Dec 15 '14 at 21:42