NaN and inf literal constants g++

J

Jon Wilson

How do I write Not A Number or +/- infinity (floating point values) as
literal constants (I'm using g++)?

I have a function that returns float, and I need a flag value, and for
various reasons one of NaN, +/-inf would be most convenient. But how do
I tell my function to return one of these? The best I can come up with
so far is

float foo()
{
if(bar)
return (2 + 2);
else
return (0.0 / 0.0);
}

Which works, ( (0.0 / 0.0) == NaN, or (1.0 / 0.0) == inf ), but g++
gives me a divide-by-zero warning when I compile.

Regards,
Jon
 
V

Victor Bazarov

Jon Wilson said:
How do I write Not A Number or +/- infinity (floating point values) as
literal constants (I'm using g++)?

I have a function that returns float, and I need a flag value, and for
various reasons one of NaN, +/-inf would be most convenient. But how do I
tell my function to return one of these? The best I can come up with so
far is

float foo()
{
if(bar)
return (2 + 2);
else
return (0.0 / 0.0);
}

Which works, ( (0.0 / 0.0) == NaN, or (1.0 / 0.0) == inf ), but g++ gives
me a divide-by-zero warning when I compile.

Take a look at std::numeric_limits template, there are static member
functions for NaN, infinity, and other things. You won't need to do
anything as weird as what you've shown.

V
 
W

Walter

Victor Bazarov said:
Take a look at std::numeric_limits template, there are static member
functions for NaN, infinity, and other things. You won't need to do
anything as weird as what you've shown.

Digital Mars C++ has the keyword __inf representing double infinity, and
__nan representing double NaN. (For C99 compiles, the NAN and INFINITY
macros expand to these keywords.) Float and long double versions can be
created by casting the double ones.

The D programming language is even more straightforward:
float.nan // represents floating point NaN value
float.infinity // represents floating point infinity value
double.nan
double.infinity // analogous double versions
etc.

-Walter
www.digitalmars.com free C, C++ and D compilers
"code of the nerds"
 
J

Jon Wilson

Victor said:
Take a look at std::numeric_limits template, there are static member
functions for NaN, infinity, and other things. You won't need to do
anything as weird as what you've shown.

V

Unfortunately, I don't seem to have the std::numeric_limits template...
I'm running on FermiLinux, and for some reason, the FermiLinux
maintainers are extremely late adopters... I've still got gcc 2.96. And
since I would expect any of my users to be running on the same distro, I
can't add my own little extensions. cmath includes math.h includes
bits/nan.h, but only if __ISO_C99 is defined, which it apparently is
not, so I don't have the NAN macro defined. However, I have found
HUGE_VAL to be defined, which gives me inf. Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top