Solved

# testing for infinity

Posted on 2000-03-14
657 Views
How do I check to see whether a number has gone out of range?  The check for positive infinity works, but the following doesn't:

float x=1.0;
if (x == numeric_limits<float>::quiet_NaN())
{
do something
}

What happens is that the comparison always comes up true.

What's the trick to doing this check?
0
Question by:Ashurbanipal
• 2

LVL 22

Accepted Solution

nietod earned 50 total points
ID: 2617422
You don't want to compare with quiet_Nan().

Use max() instead.  Positive infinity is the only number greater than max(), like

if (x == numeric_limits<float>::max())
0

Author Comment

ID: 2618308
if (x == numeric_limits<float>::infinity())

seems to work, but

if (x == numeric_limits<float>::max())

generates a compiler error that says max() needs parameters.

Is there any reason not to use infinity()?
0

LVL 22

Expert Comment

ID: 2619410
>> Is there any reason not to use infinity()?
Probably not. I simply didn't realize it was there.

>> if (x == numeric_limits<float>::max())
>> generates a compiler error that says max() needs parameters
numeric_limits::max()  does not require any parameters.  (And the code I suggested works, I did test it, because I had never tried that with floating point before.)

I suspect that the problem may be that you have the C (not C++) max() function #defined in your program so the preprocessor is converting the max() if finds to the code of the max function.
0

## Featured Post

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.