Assigning nan to a double 
Author Message
 Assigning nan to a double

Can anyone tell me how to assign the value nan (as known on an Intel
processor) to a double

Best regards

Christian Hansen



Sat, 15 Jan 2000 03:00:00 GMT  
 Assigning nan to a double

Most C compilers are smart enough to generate NaN constants from
expressions such as ((DBL_MAX * 2) * 0)  Of course, many of you have to
write code which works even with Microsoft c++ which is its own kettle of
fish.
Tim



Sat, 15 Jan 2000 03:00:00 GMT  
 Assigning nan to a double



| Can anyone tell me how to assign the value nan (as known on an Intel
| processor) to a double

You're dealing with tricky stuff here; IEEE 754 specifies certain
ranges of bit patterns as a 'nan'. Some hardware splits up those
nans into two distinct classes: signaling nans and quiet nans.
printf() is (sometimes) capable of printing 'NaNS' or 'NaNQ' when
fed one of those nan-types.

IEEE specifies two ranges:

        0x7f00000000000001 ... 0x7fffffffffffffff and
        0xff00000000000001 ... 0xffffffffffffffff

as nans. Note that not all hardware stores the most significant
byte (the sign and exponent part) first in memory. Something like
the following should (probably) give you a nan value:

union {
        char c[sizeof(double)] = { 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f

Quote:
};
        double f;
} nan;


dirty stuff; are you really sure you need an explicit nan-value?
Have you looked at isnan()?

kind regards,




Sat, 15 Jan 2000 03:00:00 GMT  
 Assigning nan to a double

Quote:

>Can anyone tell me how to assign the value nan (as known on an
>Intel processor) to a double

Although off topic, the answer is short.  For infinity, all 11
exponent bits are 1's and all 52 significand bits are 0's.  The
sign bit differentiates positive vs negative infinity.

For NaN's, all 11 exponent bits are also 1's, but all 52
significand bits are NOT 0's.

If you want to into the real nitty gritty, pick up a book on
the 80x87.

Bill <WJMc at pobox dot com>

[posted and emailed]



Fri, 28 Jan 2000 03:00:00 GMT  
 Assigning nan to a double



Quote:

>>Can anyone tell me how to assign the value nan (as known on an
>>Intel processor) to a double
>Although off topic, the answer is short.  For infinity, all 11
>exponent bits are 1's and all 52 significand bits are 0's.  The
>sign bit differentiates positive vs negative infinity.
>For NaN's, all 11 exponent bits are also 1's, but all 52
>significand bits are NOT 0's.

That still doesn't tell him how to assign a NaN to a variable.
One could use such representation information to initialize a
union of an array of integer and a double with the integer values,
then access the double value, but that isn't strictly conforming
code.  Another approach is to cheaply generate a NaN and just
assign that.  I assume the environment supports IEEE math (since
there is little use for NaNs otherwise):
        #include <math.h>
        ...
        double d = sqrt(-1.0);
        // d now holds some representation of a NaN


Sat, 29 Jan 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Problem with Double.IsNan() and Double.Nan

2. Comparing double NAN with double INF is not IEEE compliant

3. Assigning +-Inf, NaN

4. Assigning +-Inf, NaN

5. assign NaN to a variable ?

6. warning C4756: overflow in constant arithmetic in MC++ app using Double::NaN

7. long doubles and NaN

8. Check if a float/double is NaN

9. This NaN == that NaN true?

10. double not assigning value

11. Is NaN >= NaN or NaN >= 1.0 TRUE or FALSE?

12. result of double*double is no double

 

 
Powered by phpBB® Forum Software