NO

Author Topic: __NAN__ and __INFINITY__  (Read 2851 times)

PaoloC13

  • Guest
__NAN__ and __INFINITY__
« on: December 21, 2014, 06:24:30 PM »
where are defined __NAN__ and __INFINITY__ special tokens?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: __NAN__ and __INFINITY__
« Reply #1 on: December 21, 2014, 06:47:41 PM »
They are compiler predefined values. More definitions are in math.h
« Last Edit: December 21, 2014, 06:53:06 PM by frankie »
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

PaoloC13

  • Guest
Re: __NAN__ and __INFINITY__
« Reply #2 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):

Code: [Select]
//////////////////////////////////
// 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:

Code: [Select]
+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.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: __NAN__ and __INFINITY__
« Reply #3 on: December 23, 2014, 12:48:13 PM »
Your answers are here
More general info here and here.
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).
« Last Edit: December 23, 2014, 01:29:25 PM by frankie »
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide