Robert,

Advanced googletest Topics

Floating-Point Comparison

Comparing floating-point numbers is tricky. Due to round-off errors, it is 
very unlikely that two floating-points will match exactly. Therefore, 
ASSERT_EQ 's naive comparison usually doesn't work. And since 
floating-points can have a wide value range, no single fixed error bound 
works. It's better to compare by a fixed relative error bound, except for 
values close to 0 due to the loss of precision there.

In general, for floating-point comparison to make sense, the user needs to 
carefully choose the error bound. If they don't want or care to, comparing 
in terms of Units in the Last Place (ULPs) is a good default, and 
googletest provides assertions to do this. 

ASSERT_FLOAT_EQ(val1, val2); EXPECT_FLOAT_EQ(val1, val2);   the two float 
values 
are almost equal
ASSERT_DOUBLE_EQ(val1, val2); EXPECT_DOUBLE_EQ(val1, val2);    the two 
double values are almost equal

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

What does testify do?

Peter



On Monday, February 1, 2021 at 12:14:35 AM UTC-5 Robert M. Münch wrote:

> I have a C library with some test-cases (using googletest) that all pass. 
> I created Go bindings for the C lib and converted the tests to testify 
> format.
>
> Some Go test fails because a return value from the C function (type float) 
> is different than expected. For example:
>
> ASSERT_FLOAT_EQ(10, GetHeight(root)); // C code works
>
> assert.EqualValues(t, 10, root.Height()) // Go code fails
> The returned value in Go is: float32(1e+21) instead of float32(10). I'm 
> really wondering how this can happen. It looks like somewhere in the FFI, 
> the value changes. But what puzzles me, I'm using the same Go tests over 
> and over in other tests, and things are working.
>
> Does anyone have an idea what could be the problem?
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/cc7cbf40-55c4-4949-92f3-925577ebff4cn%40googlegroups.com.

Reply via email to