I need to check a double value for infinity in a C++ app on Linux. On most platforms this works by comparing with std::numeric_limits<double>::infinity()
. However, on some old platforms (RedHat 9 for example, with gcc 3.2.2) this is not available, and std::numeric_limits<double>::has_infinity
is false there.
What workaround would you recommend for those platforms?
-
For most cases, std::numeric_limits::max() can be a valid replacement for std::numeric_limits::infinity().
You would have to be more careful about using it, however. With overflows I believe you would have to manually detect the overflow and set the result to the max explicitly if an overflow is detected.
-
If you're using IEEE 754 arithmetic, as you almost certainly are, infinities are well defined values and have defined outcomes for all arithmetic operations. In particular,
infinity - infinity = NaN
Positive and negative infinity and
NaN
values are the only values for which this is true. NaNs are special "not-a-number" values used to indicate domain errors of functions, e.g.sqrt(-1)
. Also:NaN != NaN
NaN
s are the only values for which this is true.Therefore:
bool is_infinite(double x) { double y = x - x; return x == x && y != y; }
will return true if and only if
x
is either positive or negative infinity. Add a test forx > 0
if you only want to check for positive infinity.MSalters : std::numeric_limits::has_infinity==false suggests you do NOT have IEEE 754 j_random_hacker : That may be true, but in the OP's case it's likely to mean he has an incomplete C++ Standard Library implementation that **has** IEEE 754 but doesn't "realise it". Why? Because IEEE 754 is usually done in hardware, and upgrading to a newer compiler fixed the problem.j_random_hacker : But I agree that in the general case, if you do not know otherwise, then you should assume that std::numeric_limits::has_infinity==false implies no IEEE 754. -
Ok, I have now resorted to using the
INFINITY
andNAN
macros on that particular machine - seems to work fine. They come frommath.h
.
0 comments:
Post a Comment