Pelles C forum

C language => Beginner questions => Topic started by: PaoloC13 on December 21, 2014, 06:24:30 PM

Title: __NAN__ and __INFINITY__
Post by: PaoloC13 on December 21, 2014, 06:24:30 PM
where are defined __NAN__ and __INFINITY__ special tokens?
Title: Re: __NAN__ and __INFINITY__
Post by: frankie on December 21, 2014, 06:47:41 PM
They are compiler predefined values. More definitions are in math.h
Title: Re: __NAN__ and __INFINITY__
Post by: PaoloC13 on December 23, 2014, 01:04:07 AM
Thanks frankie, I needed help to dispel this doubt.

However I wanted to "look inside" these values, and I did it by deriving them in this way (single precision floating point):


//////////////////////////////////
// Positive infinity value:

float x = __INFINITY__; // First way.
float x = 1.0f / 0.0f; // Second way.

//////////////////////////////////
// Negative infinity value:

float x = -__INFINITY__; // First way.
float x = -1.0f / 0.0f; // Second way.

////////////////////////////
// the NAN value:

float x = __NAN__; // First way.
float x = 0.0f / 0.0f; // Second way.
float x = nan(NULL); // Using <math.h>.


I hope it's correct.
Results are consistent, both for IA32 + Windows XP and for IA64 + Windows 8, namely:

+INFINITY 0x7f800000  or  0x0, 0xff, 0x000000
-INFINITY 0xff800000  or  0x1, 0xff, 0x000000
NAN 0xffc00000  or  0x1, 0xff, 0x400000
NAN 0x7fc00000  or  0x0, 0xff, 0x400000 // Only with nan(NULL);

FLT_MAX; 0x7f7fffff  or  0x0, 0xfe, 0x7fffff
-FLT_MAX; 0xff7fffff  or  0x1, 0xfe, 0x7fffff
FLT_MIN; 0x00800000  or  0x0, 0x01, 0x000000
-FLT_MIN; 0x80800000  or  0x1, 0x01, 0x000000


Values in exadecimal are whole Binary32, (or) sign, exponent, mantissa (exponent in raw value).
So I suppose that 0xff exponent is reserved for these special values, and we have a NAN when significand is non-zero.
Title: Re: __NAN__ and __INFINITY__
Post by: frankie on December 23, 2014, 12:48:13 PM
Your answers are here (http://en.wikipedia.org/wiki/IEEE_754-1985)
More general info here (http://en.wikipedia.org/wiki/IEEE_floating_point) and here (http://en.wikipedia.org/wiki/NaN).
In short:
- Infinity is represented as exponent 0xff, the sign bit 1 =positive infinity, sign bit 0=negative infinity, the fracion all zeros
- NaN is reperesented with exponent 0xff the sign don't care 1 or 0. The fraction is anything but not 0  (to differentiate it from infinity).