help with NaN in C/C++ 
Author Message
 help with NaN in C/C++

Hello all,

How does one represent integers, floats, and doubles as NaN in C/C++?
Can this be done so that the resulting code is platform independent such
that it will work on intel, power pc, and sparc arcitectures?  

What I am looking for is a = (int) NaN, b = (float) NaN, c = (double) NaN.

Many thanks,

Randy
--



Mon, 12 May 2003 10:26:11 GMT  
 help with NaN in C/C++
I don't believe there is a defined cast from NaN to int. Many compilers will
DTRT with b = 0.f/0.f, c = 0./0. if you are looking for ways to set a NaN
value.  Many libraries have functions to generate NaN, but there was no
standard for this in C89.


Quote:
> Hello all,

> How does one represent integers, floats, and doubles as NaN in C/C++?
> Can this be done so that the resulting code is platform independent such
> that it will work on intel, power pc, and sparc arcitectures?

> What I am looking for is a = (int) NaN, b = (float) NaN, c = (double) NaN.

> Many thanks,

> Randy
> --


--



Tue, 13 May 2003 12:08:11 GMT  
 help with NaN in C/C++

Quote:

> Hello all,

> How does one represent integers, floats, and doubles as NaN in C/C++?
> Can this be done so that the resulting code is platform independent such
> that it will work on intel, power pc, and sparc arcitectures?

> What I am looking for is a = (int) NaN, b = (float) NaN, c = (double) NaN.

It can't be done directly with basic floating point types (float and
double).
NaN has a particular definition in a IEEE floating point format, and the
C and C++ standards do not require IEEE floating point.  Other floating
point
formats do not necessarily support the notion of NaN, +/- infinity that
are supported by IEEE.

For integer types, NaN generally has no meaning.  The results of
(say) integer overflow or division by zero are simply undefined by
the standard, and do not result in a particular value comparable to
NaN.

To achieve what you want, you will need to roll your own. Unfortunately,
the solution will probably be system dependent.
--



Tue, 13 May 2003 12:08:28 GMT  
 help with NaN in C/C++

Quote:
> How does one represent integers, floats, and doubles as NaN in C/C++?
> Can this be done so that the resulting code is platform independent such
> that it will work on intel, power pc, and sparc arcitectures?  

True platform independence cannot be had, in this respect, as not all
existing platforms have NaN, to begin with. And until C99 really
becomes widely available, there's no safe way of operating on
NaNs. Not even of determining if a system _has_ such a thing as NaN
representation, in the first place.

So this is utterly system-dependent. Even the same OS on different
hardware, or the same hardware running a different OS, or a different
set of compiler flags even, may change availability of this.

Quote:
> What I am looking for is a = (int) NaN, b = (float) NaN, c = (double) NaN.

int's cannot usually be NaN's. NaN is a floating point arithmetics
concept.

--

Even if all the snow were burnt, ashes would remain.
--



Tue, 13 May 2003 12:09:22 GMT  
 help with NaN in C/C++

Quote:


> > Hello all,

> > How does one represent integers, floats, and doubles as NaN in C/C++?
> > Can this be done so that the resulting code is platform independent such
> > that it will work on intel, power pc, and sparc arcitectures?

> > What I am looking for is a = (int) NaN, b = (float) NaN, c = (double) NaN.

> It can't be done directly with basic floating point types (float and
> double).
> NaN has a particular definition in a IEEE floating point format, and the
> C and C++ standards do not require IEEE floating point.  Other floating
> point
> formats do not necessarily support the notion of NaN, +/- infinity that
> are supported by IEEE.

Just a side note: the C++ standard does not *require* IEEE-754
(a.k.a. ISO/IEC 559), but *if* floats (and/or doubles) are implemented
according to IEEE-754 then

        std::numeric_limits<float>::is_iec559 != false

and

        std::numeric_limits<float>::infinity()
        std::numeric_limits<float>::signalling_NaN()
        std::numeric_limits<float>::quiet_NaN()

are available and return infinity, signalling NaN, and quiet NaN,
respectively.

I have no idea which C++ compilers support this though.

Best wishes,

--
Mats Kindahl, IAR Systems, Sweden

Any opinions expressed are my own, not my company's.
--



Wed, 14 May 2003 03:00:00 GMT  
 help with NaN in C/C++

Quote:


> > How does one represent integers, floats, and doubles as NaN in C/C++?
> > Can this be done so that the resulting code is platform independent such
> > that it will work on intel, power pc, and sparc arcitectures?  

> True platform independence cannot be had, in this respect, as not all
> existing platforms have NaN, to begin with. And until C99 really
> becomes widely available, there's no safe way of operating on
> NaNs. Not even of determining if a system _has_ such a thing as NaN
> representation, in the first place.
> So this is utterly system-dependent. Even the same OS on different
> hardware, or the same hardware running a different OS, or a different
> set of compiler flags even, may change availability of this.

> > What I am looking for is a = (int) NaN, b = (float) NaN, c = (double) NaN.

> int's cannot usually be NaN's. NaN is a floating point arithmetics
> concept.

> --

> Even if all the snow were burnt, ashes would remain.
> --


umm...

what wrong with

c = strtod( "nan", NULL ); ?

quite portable and system independent...

OK
--



Mon, 19 May 2003 03:00:00 GMT  
 help with NaN in C/C++

Quote:

> what wrong with

> c = strtod( "nan", NULL ); ?

It only works in C99.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield
--



Mon, 19 May 2003 03:00:00 GMT  
 help with NaN in C/C++

on comp.lang.c:

Quote:


>> > How does one represent integers, floats, and doubles as NaN in C/C++?
>> > Can this be done so that the resulting code is platform independent such
>> > that it will work on intel, power pc, and sparc arcitectures?  

>> True platform independence cannot be had, in this respect, as not all
>> existing platforms have NaN, to begin with. And until C99 really
>> becomes widely available, there's no safe way of operating on
>> NaNs. Not even of determining if a system _has_ such a thing as NaN
>> representation, in the first place.
>> So this is utterly system-dependent. Even the same OS on different
>> hardware, or the same hardware running a different OS, or a different
>> set of compiler flags even, may change availability of this.

>> > What I am looking for is a = (int) NaN, b = (float) NaN, c = (double) NaN.

>> int's cannot usually be NaN's. NaN is a floating point arithmetics
>> concept.

>> --

>> Even if all the snow were burnt, ashes would remain.
>> --

> umm...
> what wrong with
> c = strtod( "nan", NULL ); ?
> quite portable and system independent...

Isn't nan some kind of Indian corn bread?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"Products like that make me wish I could menstruate."
   - Andy Richter
--



Mon, 19 May 2003 03:00:00 GMT  
 help with NaN in C/C++

Quote:

> umm...

> what wrong with

> c = strtod( "nan", NULL ); ?

> quite portable and system independent...

Other than it's got no guarantee whatsoever of working?  I would be
surprised if there _any_ implementations in which this does what you
expect.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ To know oneself, one should assert oneself.
\__/ Albert Camus
    Physics reference / http://www.alcyone.com/max/reference/physics/
 A physics reference.
--



Mon, 19 May 2003 03:00:00 GMT  
 help with NaN in C/C++

Quote:


> > > How does one represent integers, floats, and doubles as NaN in C/C++?
> > > Can this be done so that the resulting code is platform independent
such
> > > that it will work on intel, power pc, and sparc arcitectures?

> > True platform independence cannot be had, in this respect, as not all
> > existing platforms have NaN, to begin with. And until C99 really
> > becomes widely available, there's no safe way of operating on
> > NaNs. Not even of determining if a system _has_ such a thing as NaN
> > representation, in the first place.

> > So this is utterly system-dependent. Even the same OS on different
> > hardware, or the same hardware running a different OS, or a different
> > set of compiler flags even, may change availability of this.

> > > What I am looking for is a = (int) NaN, b = (float) NaN, c = (double)
NaN.

> > int's cannot usually be NaN's. NaN is a floating point arithmetics
> > concept.

> > --

> > Even if all the snow were burnt, ashes would remain.
> > --

> umm...

> what wrong with

> c = strtod( "nan", NULL ); ?

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
 double c;
 c = strtod( "NaN", NULL );
 printf("%f\r\n", c);
 return 0;

Quote:
}

This gives 0.00000 on my machine, not the right answer.
/\/\/\/*= Martin

Quote:

> quite portable and system independent...

> OK
> --


--



Tue, 20 May 2003 03:00:00 GMT  
 help with NaN in C/C++


....

Quote:
> Isn't nan some kind of Indian corn bread?

It must be since nan2number() gives the same result for both nan and NaNs,
always returning void. :)O<

/\/\/\/*= Martin
--



Tue, 20 May 2003 03:00:00 GMT  
 help with NaN in C/C++


Quote:


[...]
>> > What I am looking for is a = (int) NaN, b = (float) NaN, c = (double) NaN.

>> int's cannot usually be NaN's. NaN is a floating point arithmetics
>> concept.

[...]

>umm...

>what wrong with

>c = strtod( "nan", NULL ); ?

>quite portable and system independent...

...way to set the variable c to zero...

Regards,

I Don't Do Windoze
--



Tue, 20 May 2003 03:00:00 GMT  
 help with NaN in C/C++

Quote:


> > what wrong with

> > c = strtod( "nan", NULL ); ?

> It only works in C99.

are you sure?

do not have C89 at hands, but it works for me in Solaris 5.6/Sun C v4
which is circa 1995-1996 time frame...

Quote:
> --
> "To get the best out of this book, I strongly recommend that you read it."
> --Richard Heathfield
> --


OK
--



Tue, 20 May 2003 03:00:00 GMT  
 help with NaN in C/C++

Quote:

> > umm...

> > what wrong with

> > c = strtod( "nan", NULL ); ?

> > quite portable and system independent...

> Other than it's got no guarantee whatsoever of working?  

                      ^^^^^^^^^^^^^^^^^^^^^^
what do you mean ?

Quote:
> I would be
> surprised if there _any_ implementations in which this does what you
> expect.

huh?

it works for me on Linux (kernel2.2, glibc 2.1) and Solaris 5.6, 5.7

OK
--



Tue, 20 May 2003 03:00:00 GMT  
 help with NaN in C/C++

Quote:

>> c = strtod( "nan", NULL ); ?
> I would be surprised if there _any_ implementations in which this
> does what you expect.

There definitely are some. GNU libc, in it's more recent versions,
IIRC.  The DJGPP (DOS gcc) libc also has support for that in the
development version, if memory serves.
--

Even if all the snow were burnt, ashes would remain.
--



Tue, 20 May 2003 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. help with NaN in C/C++

3. newbe/cs student, need help w/ code

4. Compiler error CS 1595 - Help

5. Help!!! Novice CS Advice

6. Need C++ text for non cs major course

7. This NaN == that NaN true?

8. How to show/call Form2.cs from Form1.cs ?

9. NaN comparison still broken with /Ox in VS C++ 7.0

10. NaN in C/C++ on OSF1

11. NAN and INF numbers in C, C++

12. Include code in other Cs files

 

 
Powered by phpBB® Forum Software