Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BOOST_CHECK_CLOSE(0, smallnumber) fails #162

Closed
WilliamTambellini opened this issue Sep 14, 2018 · 4 comments
Closed

BOOST_CHECK_CLOSE(0, smallnumber) fails #162

WilliamTambellini opened this issue Sep 14, 2018 · 4 comments
Assignees
Labels

Comments

@WilliamTambellini
Copy link

Hello
With boost 1.60
BOOST_CHECK_CLOSE(-0, -4.37113883e-08, 1)
fails with :
difference{3.40282e+38} between -0 and -4.37113883e-08 exceeds 1%
Note the difference is super big.
Should nt it pass ?
Tks

@raffienficiaud
Copy link
Member

Hi,

There were many bug fixes since 1.60 (see here for more details).

Would you please try again with the latest version of Boost?

@raffienficiaud
Copy link
Member

Sorry for the late investigation. It turns out that you cannot use BOOST_CHECK_CLOSE to compare to the value 0, as it would use the algorithm described here.

You have 2 options:

  • either use BOOST_CHECK_SMALL(-4.37113883e-08, 1.) (note . in the 1.)
  • or use BOOST_TEST(-4.37113883e-08 == 0, 1. % tt::tolerance()) (for percentage tolerance)

Hope that helps!

@WilliamTambellini
Copy link
Author

Thank you @raffienficiaud
I have retried with 1.69 and the same behavior.
Could nt find a better perf than writing my own CHECK macro doing :
CHECK_CLOSE(result, ref)
if (result == 0 || ref == 0)
BOOST_CHECK_SMALL(result - ref, 0.01f);
else
BOOST_CHECK_CLOSE(result, ref, 0.01f);

Mathematically speaking, it is legit to wonder if 0.0000001 is "close" to 0 (within a range).
Should nt it be illogic to expect BOOST_CHECK_CLOSE() to work even if left or right are 0 ?

@raffienficiaud
Copy link
Member

If you look at the equations for comparison here, then only equation (1) can work when one of the arguments u or v is 0. This is why there exists BOOST_CHECK_SMALL and BOOST_CHECK_CLOSE.

On the other hand, this is done automatically with BOOST_TEST if

  • one of the arguments is floating point (since 1.70, otherwise the two arguments should be floating points)
  • and one of the argument is zero,

so you should not be needing to write your own logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants