Solved

testing for infinity

Posted on 2000-03-14
3
664 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

696 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