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

close_at_tolerance always returns false for comparisons of infinity #156

Closed
Timmmm opened this Issue Aug 31, 2018 · 9 comments

Comments

Projects
None yet
2 participants
@Timmmm

Timmmm commented Aug 31, 2018

Consider this code:

#include <boost/test/tools/floating_point_comparison.hpp>
#include <iostream>
#include <limits>

namespace fpc = boost::math::fpc;
int main() {
	float a = std::numeric_limits<float>::infinity();
	float b = std::numeric_limits<float>::infinity();

	auto P = fpc::close_at_tolerance<float>(fpc::percent_tolerance<float>(0.01), fpc::FPC_WEAK);
	std::cout << P(a, b) << "\n";
}

It prints 0. You might say "well that makes sense because one infinity is not the same as another". Except that the IEEE floating point standard considers inf == inf to be true, which means that you have the strange situation where this passes:

BOOST_AUTO_TEST_CASE(foo)
) {
	float a = std::numeric_limits<float>::infinity();
	float b = a;
	BOOST_TEST(a == b);
}

But this does not:

BOOST_AUTO_TEST_CASE(foo,
	*utf::tolerance<float>(fpc::percent_tolerance<float>(0.01))
) {
	float a = std::numeric_limits<float>::infinity();
	float b = a;
	BOOST_TEST(a == b);
}

Also, I'm 90% sure the latter case did pass in the past (Boost 1.60 at least).

@Timmmm

This comment has been minimized.

Timmmm commented Aug 31, 2018

I have double checked and that test definitely passes on Boost 1.60.0 but not 1.67.0. Unfortunately brew doesn't have any versions between those so I can't easily narrow it down. :-(

@raffienficiaud

This comment has been minimized.

Member

raffienficiaud commented Aug 31, 2018

Thanks for filing this bug and checking about the regression. Not so much happened on this part since some time, and I believe the fix is quite easy. The only thing I do not know about is how other floating point types (like multiprecision) handle infinity.

@Timmmm

This comment has been minimized.

Timmmm commented Sep 1, 2018

Do you happen to know what the fix is or which commit changed the behaviour?

@raffienficiaud

This comment has been minimized.

Member

raffienficiaud commented Sep 1, 2018

I believe this is this one: https://svn.boost.org/trac10/ticket/13011
Rev bf703c2

I will check tonight.

@Timmmm

This comment has been minimized.

Timmmm commented Sep 3, 2018

Ah cool, I patched it like this and it works for us now:

inline assertion_result
compare_fpv( Lhs const& lhs, Rhs const& rhs, op::EQ<Lhs,Rhs>* )
{
    if (lhs == rhs) {
        return true;
    }
    ...
@raffienficiaud

This comment has been minimized.

Member

raffienficiaud commented Sep 29, 2018

It is fixed on the branch topic/GH-156-comparison-to-infinity. However close_at_tolerance is using a tolerance value, and does not handle 0 nor infinity. I updated the doc as well.

Let me know if the branch works for you

raffienficiaud added a commit that referenced this issue Sep 29, 2018

Merge branch 'topic/GH-156-comparison-to-infinity' into next-internal
* topic/GH-156-comparison-to-infinity:
  Change log
  Doc update
  Fix tolerance when one of the numbers is infinity

raffienficiaud added a commit that referenced this issue Oct 1, 2018

Merge branch 'topic/GH-156-comparison-to-infinity' into develop
* topic/GH-156-comparison-to-infinity:
  Change log
  Doc update
  Fix tolerance when one of the numbers is infinity
@raffienficiaud

This comment has been minimized.

Member

raffienficiaud commented Oct 1, 2018

In develop, thanks again for the report. Closing.

@Timmmm

This comment has been minimized.

Timmmm commented Oct 3, 2018

Seems like our tests have changed and I can't get them to fail now, but the fix looks good to me - thanks!

@raffienficiaud

This comment has been minimized.

Member

raffienficiaud commented Oct 3, 2018

You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment