Solved

testing for infinity

Posted on 2000-03-14
3
657 Views
Last Modified: 2008-03-17
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
Comment
Question by:Ashurbanipal
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
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

by:Ashurbanipal
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

by:nietod
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now