473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

NaN and inf literal constants g++

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
Jul 22 '05 #1
3 5089
"Jon Wilson" <js*@fnal.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
Jul 22 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Gb********************@comcast.com...
"Jon Wilson" <js*@fnal.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"
Jul 22 '05 #3
Victor Bazarov wrote:
"Jon Wilson" <js*@fnal.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!
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Vivek | last post by:
What is literal Pool.
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
3
by: Stephen Mayes | last post by:
Is there any way to represent a literal numerical value in binary? My wish would be something like 0b0011 instead of the hex 0x0F. ( if this is even the same. ) Sometimes I find it's easier to...
2
by: comp.lang.php | last post by:
var $filterArray = array('reverse' =IMG_FILTER_NEGATE, 'edge highlight' =IMG_FILTER_EDGEDETECT, 'emboss' =IMG_FILTER_EMBOSS, 'gaussian blur' =IMG_FILTER_GAUSSIAN_BLUR, 'blur'...
18
by: Vedo | last post by:
ref struct XXX abstract sealed { literal int A = 5; }; The definition above gives me the compiler warning "C4693: a sealed abstract class cannot have any instance members 'A'". The equivalent...
5
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to...
2
by: murali.desikan | last post by:
Hi, The definition of floating literal in the C++ (ISO/IEC 14882:2003) grammar is as follows (Note: I have replaced the "opt" subscript used in the standard with to indicate optional symbol)....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.