Concept of NaN, IND, INF and DEN

1

Came across this C++ article on the CodeProject:
https://www.codeproject.com/Articles/824516/Concept-of-NaN-IND-INF-and-DEN

that tried to explain the concept of how Infinity & NaN (and other "special" numeric types) are represented in computational arithmetic in C++.


It was clear enough to follow where it showed its "standard representation" of each number type using standard library code but where it got a bit confusing was the "non-standard representation" given out of "academic" interest.

The standard representations looked like this:

const double STD_NOT_A_NUMBERD = std::numeric_limits<double>::quiet_NaN();

while the non-standard representations looked like this:

const unsigned long lnNAN[2] = {0x00000000, 0x7ff80000}; // "Define an array of size 2 of type long."
const double NOT_A_NUMBER = *( double* )lnNAN; // "Now, cast it to a double value!"

Of course, I’ve tested all of these and both representations elude to their respective numeric form (Denormalized, Indeterminate, Infinity & NaN) but the thing is:

What is happening in these non-standard representations?

More specifically, how is an array being converted to a number here and why are the values in said array specific to each numeric form?


Here’s the code for each "special" number:

• Denormalized

/* Non-Standard Representation */
const unsigned long lnDEN[2] = {0x00000001, 0x00000000};
const double A_DENORMAL  = *( double* )lnDEN;

/* Standard Representation */
double dDEN = std::numeric_limits<double>::denorm_min();

• Indeterminate

/* Non-Standard Representation */
const unsigned long lnIND[2] = {0x00000000, 0xfff80000};
const double AN_INDETERMINATE  = *( double* )lnIND;

/* Standard Representation */
// ???

• Infinity

/* Non-Standard Representation */
const unsigned long lnINF[2] = {0x00000000, 0x7ff00000};
const double AN_INFINITY_POSITIVE  = *( double* )lnINF;

/* Standard Representation */
const double STD_AN_INFINITY_POSITIVE = std::numeric_limits<double>::infinity();

• Not-A-Number (NaN)

/* Non-Standard Representation */
const unsigned long lnNAN[2] = {0x00000000, 0x7ff80000};
const double NOT_A_NUMBER = *( double* )lnNAN;

/* Standard Representation */
const double STD_NOT_A_NUMBERD = std::numeric_limits<double>::quiet_NaN();
c++
asked on Stack Overflow Jan 20, 2020 by Lapys

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0