?
Solved

testing for infinity

Posted on 2000-03-14
3
Medium Priority
?
671 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

850 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