Solved

testing for infinity

Posted on 2000-03-14
3
660 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 119
IE call stack entries where function call address is not in a module 4 62
Excel/Word Add-in in what language? 5 107
Which Linux flavors will this run on? 6 76
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 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

12 Experts available now in Live!

Get 1:1 Help Now