Strange Google Test error with floating point numbers

I am having trouble testing floating point values ​​for equality since Google Test 1.7.0.

My statement looks like this:

ASSERT_NEAR(124691356.375f, actual, DELTA);

      

The test fails with the following error:

The difference between 124691356.375f and the actual value is 3.625, which is higher than the DELTA, where 124691356.375f is valued to 124691360 , the actual estimate is 124691356.375 and the DELTA is 0.0625.

What's happening? actual

, and the expected result is clearly within the 0.0625 allowable error. Why does gtest evaluate a floating point literal 124691356.375f

this way?

Update: DELTA

and actual

are of type double and the expected value is a floating point literal. If I change the literal to be a double, or change other arguments to be float (so that everything is of the same type), the test passes. The question still remains - what causes this behavior when the types do not match?

+3


source to share


1 answer


124691356.375f

is a single precision floating point number that has 7 to 8 significant decimal digits. Therefore, it is "rounded" to 124691360, which is not a valid error. You must use 124691356.375lf

. See also wikipedia and this question .



+2


source







All Articles