C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

NaN and inf literal constants g++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Jon Wilson
Guest





PostPosted: Fri Dec 31, 2004 2:41 am    Post subject: NaN and inf literal constants g++ Reply with quote



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
Back to top
Victor Bazarov
Guest





PostPosted: Fri Dec 31, 2004 4:02 am    Post subject: Re: NaN and inf literal constants g++ Reply with quote



"Jon Wilson" <jsw (AT) fnal (DOT) gov> wrote...
Quote:
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



Back to top
Walter
Guest





PostPosted: Fri Dec 31, 2004 7:11 am    Post subject: Re: NaN and inf literal constants g++ Reply with quote




"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote

Quote:
"Jon Wilson" <jsw (AT) fnal (DOT) gov> wrote...
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.

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"



Back to top
Jon Wilson
Guest





PostPosted: Fri Dec 31, 2004 3:56 pm    Post subject: Re: NaN and inf literal constants g++ Reply with quote

Victor Bazarov wrote:
Quote:
"Jon Wilson" <jsw (AT) fnal (DOT) gov> wrote...

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



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!

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.