doctest icon indicating copy to clipboard operation
doctest copied to clipboard

Feature request: Compare floats and have NaN be true

Open r-barnes opened this issue 1 year ago • 1 comments

Description

When I take

CHECK(floating_value1 == floating_value2);

it would be nice to have a way to say "have the check pass if the values are equal or the floats are both NaN".

r-barnes avatar May 15 '24 15:05 r-barnes

If you're using C++11 or higher, you can use std::isnan() defined in the standard cmath header, like so:

CHECK((v1 == v2) || (std::isnan(v1) && std::isnan(v2));

dargueta avatar May 17 '24 13:05 dargueta

I would do as suggested above.

You can also implement a custom wrapper type around float that have the desired non-standard behavior for equals.

BUT, I am very curious why one would even do this to begin with. Having NaN == NaN means that two completely different computations will compare equal. This implies that whenever you compare the result from two computations that fail they will pass your tests... this is usually not what you want.

Closing due to existing workaround.

cdeln avatar Aug 15 '24 10:08 cdeln