Using EXPECT_FLOAT when expected value is 0

70 views
Skip to first unread message

Charles Margossian

unread,
Sep 16, 2016, 10:05:26 AM9/16/16
to stan development mailing list
EXPECT_FLOAT tests relative accuracy, but what happens when the expected value is 0? EXPECT_FLOAT(0, 1e-25) for instance causes the test to fail.

I ran into this issue when testing the derivative of the matrix exponential function. With respect to certain variables, the derivative of some entries is 0. But matrix_exp() uses a numerical approximation, so the derivative ends up being 2e-10 instead of 0.

I don't think this indicates the code's failing. I edited the unit tests so that it would use EXPECT_FLOAT_EQ, unless the expected value is 0, in which case it uses EXPECT_NEAR_EQ(..., ..., 5e-10). Is this rigorous enough, and would it be worth writing a new EXPECT function?

Bob Carpenter

unread,
Sep 16, 2016, 12:21:33 PM9/16/16
to stan...@googlegroups.com
Right --- that doesn't indicate that the code's failing.
The near test is fine.

But we should keep precision in mind if we're trying to do
things like testing for zero returns in the code.

EXPECT_FLOAT_EQ is testing ULPs:

https://en.wikipedia.org/wiki/Unit_in_the_last_place

Sounds complicated. I don't know what algorithm they use
to test, but I could imagine something naive like
shifting 1e-25 up to unit scale and comparing to zero and
seeing that none of the leading digits match. That would
make it like the catastrophic cancellation case you get
with subtraction.

The other complication here is that we're taking double
values and casting them to float. I didn't realize before
that FLOAT meant the C++ float type and there's an equivalent
EXPECT_DOUBLE_EQ.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages