SlideShare a Scribd company logo
Numerical Concepts of
NaN, IND, INF and DEN
Mohammed Nisamudheen S
Project Lead
UVJ Technologies
I would like to share some concepts (some of you might know it already) that C++ offers when
dealing floating point arithmetic.
1. Concept of NaN
NaN means Not a Number.
For an average developer, when something is said to be not a number then it would be a string.
This is not the case here. When we perform extensive numerical calculations, the result will be
such that it cannot be treated as a number!
As an example, consider the below code.
double dSQRTValue = sqrt( -1.00 ); // An image processing algorithm may
invoke the sqrt() with -1 as its input .
double dResult = -dSQRTValue; // A image processing algorithm may involve
taking the negative of another value.
Here the variable dResult will contain a NaN. So a NaN represents a numeric quantity that
cannot be treated as a valid quantity.
What can be done to represent it ? Usually we designate 0 or -1 to mark an invalid entry in a
float or double variable/array. This kind of idea will not work here because -1 and 0 are valid
numbers.
A. Representation of NaN
I. Non Standard Representation
Define a long array of size 2.
const unsigned long const lnNAN[2] = {0x00000000, 0x7ff80000};
Now, cast it to a double value!
const double NOT_A_NUMBER = *( double* )lnNAN;
Now, the constant variable NOT_A_NUMBER contains a NaN.
II. Standard Representation
The <limits> header file defines the following functions for getting a NaN.
const double NOT_A_NUMBER = std::numeric_limits<double>::quiet_NaN();
B. How a NaN Looks Like?
Above is how a NaN is displayed in the debugger. We will get the same string representation
with functions such as sprint() and stream classes such as stringstream.
C. Comparison of NaN
I. Non Standard Method
bool bNaN = false;
if( 0 == memcmp( &NOT_A_NUMBER, &dQNan, sizeof(double)))
{
bNaN = true;
}
II. Standard Method
The "float.h" header file defines the function _isnan() for checking whether a number is NaN
or not.
C. Properties of NaN
I. Equality Check Returns False
A NaN has an important property that the comparison for equality will always return false. That
is
if( dResult == dResult )
{
int a = 0;
// Code inside this block will NEVER execute.
}
II. Any Calculation with a NaN Returns a NaN
dResult += 1234;
Here the variable dResult will contain a NaN.
Note:-
The Non Standard way of representation is just for your understanding on how a NaN is
represented in memory. Please note that it is NOT the only way of representing a NaN in
memory, there can be other representations. For more information, we need to refer the IEEE
floating point representation.
2. Concept of IND
IND means Indeterminate Number.
An IND number is a value that is one step down from NaN. That is, an IND is a value that is
almost equivalent to a NaN. There are situations in computation whose result cannot be
determined by the FPU (Floating Point Unit). In such cases the result will be set as an
indeterminate number.
As an example, consider the below code.
double dInfinity = <INF>; // Concept of Infinity will be
explained next.
double dIND = dInfinity / dInfinity; // Arithmetic operations may
eventually reach a point at which it divides two infinite numbers.
Here the variable dIND will contain an IND.
Another one
double dZero = 0.00; // This is defined just for demonstration.
double dIND1 = dZero / dZero; // Extensive algorithmic operations may
consequently perform 0/0.
Here the variable dIND1 will contain an IND.
Examples are given just for understanding. There can be other situations in which the result of
an expression produces an IND value.
A. Representation of IND
I. Non Standard Representation
Define a long array of size 2.
const unsigned long const lnIND[2] = {0x00000000, 0xfff80000};
Now, cast it to a double value.
const double AN_INDETERMINATE = *( double* )lnIND;
Please note that the lnIND contains a different value when compared to the corresponding NaN
representation.
II. Standard Representation
I could not find any functions that provides the standard representation of an IND number. This
may be due to the fact that C++ (Microsoft) treats an IND as a NaN. This point is evident from
the fact that the function _isnan() returns true (a non zero) when an IND is given as input.
B. How an IND Looks Like?
Above is how an IND is displayed in the debugger. We will get the same string representation
with functions such as sprint() and stream classes such as stringstream. There can be both –
VE and +VE representation of IND value. The string representation such as 1.#IND000000000000
are the Windows OS/Microsoft specific representation.
The concept and the internal representation ( i.e. IEEE Floating Point Format) will be same
across Platforms/Environment but the user level Keyword/String will be different.
C. Comparison of IND
I. Non Standard Method
bool bIND = false;
if( 0 == memcmp( &AN_INDETERMINATE, &dIND, sizeof(double)))
{
bIND = true;
}
II. Standard Method
So far, I could not find any standard functions.
One tricky solution (on Windows Platform) is to take the string representation of the double
value and then check for the presence of substring ‘#IND’.
C. Properties of IND
I. Equality Check Returns False
An IND has an important property that the comparison for equality will always return false. That
is
if( dIND == dIND )
{
int a = 0;
// Code inside this block will NEVER execute.
}
II. Any Calculation with a IND Returns an IND or NaN
dIND += 1234; // dIND will hold an IND
dIND += -dIND; // dIND will hold a NaN
Note:-
The Non Standard way of representation is just for your understanding on how an IND is
represented in memory. It is NOT the only way of representing an IND in memory, there can be
other representations. For more information, refer the IEEE 754 floating point representation.
3. Concept of INF
INF means Infinity.
An arithmetic operation results in an infinite number when the result of operation cannot be
held in the corresponding data type. Here the result is said to be overflowed. That is, the result
has overflowed the available storage space. In such cases, the result is marked as INF.
As an example, consider the below code.
double dZero = 0.00; // This is defined just for demonstration.
double dINF = 1/dZero ;
Here the variable dINF will contain an infinity.
Examples are given just for understanding. There can be other situations in which the result of
an expression produces an INF value.
A. Representation of INF
I. Non Standard Representation
Define a long array of size 2.
const unsigned long const lnINF[2] = {0x00000000, 0x7ff00000};
Now, cast it to a double value.
const double AN_INFINITY_POSITIVE = *( double* )lnINF;
II. Standard Representation
The <limits> header file defines the following function for getting an INF value .
const double AN_INFINITY_POSITIVE =
std::numeric_limits<double>::infinity();
Since there are both +VE and –VE infinity, the above function returns a +VE infinity. Negative
infinity can be obtained as below.
const double AN_INFINITY_NEGATIVE = -AN_INFINITY_POSITIVE;
B. How an INF Looks Like?
Above is how a +VE INF is displayed in the debugger. We will get the same string representation
with functions such as sprint() and stream classes such as stringstream. The string
representation such as 1.#INF000000000000 are the Windows OS/Microsoft specific
representation.
The concept and the internal representation ( i.e. IEEE Floating Point Format) will be same
across Platforms/Environment but the user level Keyword/String will be different.
C. Comparison of INF
I. Non Standard Method
bool bINF = false;
if( 0 == memcmp( &AN_INFINITY_POSITIVE, &dINF, sizeof(double)) ||
0 == memcmp( &AN_INFINITY_NEGATIVE, &dINF, sizeof(double)))
{
bINF = true;
}
II. Standard Method
The "float.h" header file defines the function _finite() for checking whether a number is INF
or not. There are other standard methods too.
C. Properties of INF
I. Equality Check Returns True
An INF has a property that the comparison for equality will always return True. That is
if( dINF == dINF )
{
int a = 0;
// Code inside this block WILL be executed.
}
if( -dINF == -dINF )
{
int a = 0;
// Code inside this block WILL be executed.
}
II. Any Calculation with a INF Returns an IND or NaN
dINF += -dINF; // dINF will hold an IND
dINF += NOT_A_NUMBER; // dINF will hold a NaN
Note:-
The Non Standard way of representation is just for your understanding on how an INF is
represented in memory. It is NOT the only way of representing an INF in memory, there can be
other representations. For more information, refer the IEEE 754 floating point representation.
4. Concept of DEN
DEN means Denormalized. It is also known as Subnormal.
All of us know that there are infinite rational numbers between 0 and 1. Have you ever thought
how much out of the infinite numbers a computer can store?
Since a computer is a finite machine, there are limitations. It has limitation in the representation
of floating numbers.
We know that the float and the
representation. This representation has two parts. One is the
the Exponent part. An example is shown below.
 Suppose an arithmetic operation results in a number that is very close to zero but NOT
zero.
 Due to the floating point representation limit, the CPU may not be able to represent it
for further computation.
 In this case, the number is marked as a denormalized number.
As an example, consider the below code.
double dDenTest = 0.01E-
dDenTest /= 10; // This will produce a denormalized number.
. It is also known as Subnormal.
All of us know that there are infinite rational numbers between 0 and 1. Have you ever thought
out of the infinite numbers a computer can store?
Since a computer is a finite machine, there are limitations. It has limitation in the representation
and the double data types are represented by the IEEE 754
representation. This representation has two parts. One is the Mantissa part and the second is
part. An example is shown below.
Suppose an arithmetic operation results in a number that is very close to zero but NOT
Due to the floating point representation limit, the CPU may not be able to represent it
for further computation.
In this case, the number is marked as a denormalized number.
As an example, consider the below code.
-305;
// This will produce a denormalized number.
All of us know that there are infinite rational numbers between 0 and 1. Have you ever thought
Since a computer is a finite machine, there are limitations. It has limitation in the representation
data types are represented by the IEEE 754 floating point
part and the second is
Suppose an arithmetic operation results in a number that is very close to zero but NOT
Due to the floating point representation limit, the CPU may not be able to represent it
Examples are given just for demonstration. There can be other situations in which the result of
an expression produces an DEN value.
A. Representation of DEN
I. Non Standard Representation
Define a long array of size 2.
const unsigned long const lnDEN[2] = {0x00000001, 0x00000000};
Now, cast it to a double value.
const double A_DENORMAL = *( double* )lnDEN;
II. Standard Representation
The <limits> header file defines the following function for getting a DEN value .
double dDEN = std::numeric_limits<double>::denorm_min();
B. How a DEN Looks Like?
Above is how a DEN value is displayed in the debugger. We will get the same string
representation with functions such as sprint() and stream classes such as stringstream. The
string representation is the Windows OS/Microsoft specific representation.
The concept and the internal representation ( i.e. IEEE 754 Floating Point Format) will be same
across Platforms/Environment but the user level Keyword/String will be different.
C. Comparison of DEN
I. Non Standard Method
bool bDEN = false;
if( 0 == memcmp( &A_DENORMAL, &dDEN, sizeof(double)))
{
bDEN = true;
}
II. Standard Method
if ( dDEN != 0 && fabsf ( dDEN ) <= numeric_limits<double>::denorm_min())
{
// it's denormalized
bDEN = true;
}
C. Properties of DEN
I. Equality Check is Same as Numeric Comparison
Since there can be multiple way of representing a DEN, a
if( dDEN == dDEN )
{
int a = 0;
// Code inside this block WILL be executed.
}
II. Any Calculation with a DEN is Same as Normal Calculation
double dDenTest = 0.01E-305;
dDenTest /= 10; // This will produce a denormalized number.
dDenTest *= 10; // This will result in the previous normalized value.
Note:-
The Non Standard way of representation is just for your understanding on how an DEN is
represented in memory. It is NOT the only way of representing an DEN in memory, there can be
other representations. For more information, refer the IEEE 754 floating point representation.
This is the last page of this document.

More Related Content

What's hot

What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
Rumman Ansari
 
Gr2512211225
Gr2512211225Gr2512211225
Gr2512211225
IJERA Editor
 
Generic programming
Generic programmingGeneric programming
Generic programming
Platonov Sergey
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
Abed Bukhari
 
C++ basics
C++ basicsC++ basics
C++ basics
ndargolis
 
C language
C languageC language
Generic Programming seminar
Generic Programming seminarGeneric Programming seminar
Generic Programming seminar
Gautam Roy
 
Autoencoder
AutoencoderAutoencoder
Autoencoder
HARISH R
 
Flag Waiving
Flag WaivingFlag Waiving
Flag Waiving
Kevlin Henney
 
Micro Blaze C Reference
Micro Blaze C ReferenceMicro Blaze C Reference
Micro Blaze C Reference
iuui
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
Rohit Shrivastava
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
SowmyaJyothi3
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programming
Claus Wu
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
HNDE Labuduwa Galle
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
Sherwin Banaag Sapin
 
Ch7 Basic Types
Ch7 Basic TypesCh7 Basic Types
Ch7 Basic Types
SzeChingChen
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Variables and data types IN SWIFT
 Variables and data types IN SWIFT Variables and data types IN SWIFT
Variables and data types IN SWIFT
LOVELY PROFESSIONAL UNIVERSITY
 
Session02 c intro
Session02 c introSession02 c intro
Session02 c intro
HarithaRanasinghe
 
(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings
Nico Ludwig
 

What's hot (20)

What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
 
Gr2512211225
Gr2512211225Gr2512211225
Gr2512211225
 
Generic programming
Generic programmingGeneric programming
Generic programming
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
C++ basics
C++ basicsC++ basics
C++ basics
 
C language
C languageC language
C language
 
Generic Programming seminar
Generic Programming seminarGeneric Programming seminar
Generic Programming seminar
 
Autoencoder
AutoencoderAutoencoder
Autoencoder
 
Flag Waiving
Flag WaivingFlag Waiving
Flag Waiving
 
Micro Blaze C Reference
Micro Blaze C ReferenceMicro Blaze C Reference
Micro Blaze C Reference
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programming
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
 
Ch7 Basic Types
Ch7 Basic TypesCh7 Basic Types
Ch7 Basic Types
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Variables and data types IN SWIFT
 Variables and data types IN SWIFT Variables and data types IN SWIFT
Variables and data types IN SWIFT
 
Session02 c intro
Session02 c introSession02 c intro
Session02 c intro
 
(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings
 

Viewers also liked

Nguy cơ tiềm ẩn từ những thực phẩm 'tốt mà không tốt'
Nguy cơ tiềm ẩn từ những thực phẩm 'tốt mà không tốt'Nguy cơ tiềm ẩn từ những thực phẩm 'tốt mà không tốt'
Nguy cơ tiềm ẩn từ những thực phẩm 'tốt mà không tốt'
dorthea606
 
Legal and Ethical Issues Ch04
Legal and Ethical Issues Ch04Legal and Ethical Issues Ch04
Legal and Ethical Issues Ch04
JLynn Jen Smith
 
Hit1614 hbhd13e ppt-ch6
Hit1614 hbhd13e ppt-ch6Hit1614 hbhd13e ppt-ch6
Hit1614 hbhd13e ppt-ch6
JLynn Jen Smith
 
HIT1443 LEIHP4e Ch09
HIT1443 LEIHP4e Ch09HIT1443 LEIHP4e Ch09
HIT1443 LEIHP4e Ch09
JLynn Jen Smith
 
Legal and Ethical Issues Ch03
Legal and Ethical Issues Ch03Legal and Ethical Issues Ch03
Legal and Ethical Issues Ch03
JLynn Jen Smith
 
HITt1443 LEIHP4e Ch07
HITt1443 LEIHP4e Ch07HITt1443 LEIHP4e Ch07
HITt1443 LEIHP4e Ch07
JLynn Jen Smith
 
HBHD13e Ch9
HBHD13e Ch9HBHD13e Ch9
HBHD13e Ch9
JLynn Jen Smith
 
HIT1443 LEIHP4e Ch05
HIT1443 LEIHP4e Ch05HIT1443 LEIHP4e Ch05
HIT1443 LEIHP4e Ch05
JLynn Jen Smith
 
HBHD 13e Ch25
HBHD 13e Ch25HBHD 13e Ch25
HBHD 13e Ch25
JLynn Jen Smith
 
HBHD 13e Ch11
HBHD 13e Ch11HBHD 13e Ch11
HBHD 13e Ch11
JLynn Jen Smith
 
HBHD13e - Ch 17
HBHD13e - Ch 17HBHD13e - Ch 17
HBHD13e - Ch 17
JLynn Jen Smith
 
Hit1614-HBHD-Ch 4
Hit1614-HBHD-Ch 4Hit1614-HBHD-Ch 4
Hit1614-HBHD-Ch 4
JLynn Jen Smith
 
HBHD 13e Ch10
HBHD 13e Ch10HBHD 13e Ch10
HBHD 13e Ch10
JLynn Jen Smith
 

Viewers also liked (13)

Nguy cơ tiềm ẩn từ những thực phẩm 'tốt mà không tốt'
Nguy cơ tiềm ẩn từ những thực phẩm 'tốt mà không tốt'Nguy cơ tiềm ẩn từ những thực phẩm 'tốt mà không tốt'
Nguy cơ tiềm ẩn từ những thực phẩm 'tốt mà không tốt'
 
Legal and Ethical Issues Ch04
Legal and Ethical Issues Ch04Legal and Ethical Issues Ch04
Legal and Ethical Issues Ch04
 
Hit1614 hbhd13e ppt-ch6
Hit1614 hbhd13e ppt-ch6Hit1614 hbhd13e ppt-ch6
Hit1614 hbhd13e ppt-ch6
 
HIT1443 LEIHP4e Ch09
HIT1443 LEIHP4e Ch09HIT1443 LEIHP4e Ch09
HIT1443 LEIHP4e Ch09
 
Legal and Ethical Issues Ch03
Legal and Ethical Issues Ch03Legal and Ethical Issues Ch03
Legal and Ethical Issues Ch03
 
HITt1443 LEIHP4e Ch07
HITt1443 LEIHP4e Ch07HITt1443 LEIHP4e Ch07
HITt1443 LEIHP4e Ch07
 
HBHD13e Ch9
HBHD13e Ch9HBHD13e Ch9
HBHD13e Ch9
 
HIT1443 LEIHP4e Ch05
HIT1443 LEIHP4e Ch05HIT1443 LEIHP4e Ch05
HIT1443 LEIHP4e Ch05
 
HBHD 13e Ch25
HBHD 13e Ch25HBHD 13e Ch25
HBHD 13e Ch25
 
HBHD 13e Ch11
HBHD 13e Ch11HBHD 13e Ch11
HBHD 13e Ch11
 
HBHD13e - Ch 17
HBHD13e - Ch 17HBHD13e - Ch 17
HBHD13e - Ch 17
 
Hit1614-HBHD-Ch 4
Hit1614-HBHD-Ch 4Hit1614-HBHD-Ch 4
Hit1614-HBHD-Ch 4
 
HBHD 13e Ch10
HBHD 13e Ch10HBHD 13e Ch10
HBHD 13e Ch10
 

Similar to Concept_of_NAN_IND_INF_DEN_Using_C++

Data representation computer architecture
Data representation  computer architectureData representation  computer architecture
Data representation computer architecture
study cse
 
02 Jo P Feb 07
02 Jo P Feb 0702 Jo P Feb 07
02 Jo P Feb 07
Ganesh Samarthyam
 
14. Recursion.pdf
14. Recursion.pdf14. Recursion.pdf
14. Recursion.pdf
VivekBhimajiyani
 
Lec 02 data representation part 2
Lec 02 data representation part 2Lec 02 data representation part 2
Lec 02 data representation part 2
Abdul Khan
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
C++ Homework Help
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
monicafrancis71118
 
13 Jo P Jan 08
13 Jo P Jan 0813 Jo P Jan 08
13 Jo P Jan 08
Ganesh Samarthyam
 
C# slid
C# slidC# slid
C# slid
pacatarpit
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
AjayBahoriya
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Chris Adamson
 
Data structures using C
Data structures using CData structures using C
Data structures using C
Pdr Patnaik
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02
Salman Qamar
 
Type Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic ExpressionsType Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic Expressions
PVS-Studio
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
oggyrao
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
Linked list
Linked listLinked list
Linked list
somuinfo123
 
07 -pointers_and_memory_alloc
07  -pointers_and_memory_alloc07  -pointers_and_memory_alloc
07 -pointers_and_memory_alloc
Hector Garzo
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
Janie Clayton
 
Accord.Net: Looking for a Bug that Could Help Machines Conquer Humankind
Accord.Net: Looking for a Bug that Could Help Machines Conquer HumankindAccord.Net: Looking for a Bug that Could Help Machines Conquer Humankind
Accord.Net: Looking for a Bug that Could Help Machines Conquer Humankind
PVS-Studio
 
Pseudocode
PseudocodePseudocode
Pseudocode
Harsha Madushanka
 

Similar to Concept_of_NAN_IND_INF_DEN_Using_C++ (20)

Data representation computer architecture
Data representation  computer architectureData representation  computer architecture
Data representation computer architecture
 
02 Jo P Feb 07
02 Jo P Feb 0702 Jo P Feb 07
02 Jo P Feb 07
 
14. Recursion.pdf
14. Recursion.pdf14. Recursion.pdf
14. Recursion.pdf
 
Lec 02 data representation part 2
Lec 02 data representation part 2Lec 02 data representation part 2
Lec 02 data representation part 2
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
 
13 Jo P Jan 08
13 Jo P Jan 0813 Jo P Jan 08
13 Jo P Jan 08
 
C# slid
C# slidC# slid
C# slid
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02
 
Type Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic ExpressionsType Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic Expressions
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Linked list
Linked listLinked list
Linked list
 
07 -pointers_and_memory_alloc
07  -pointers_and_memory_alloc07  -pointers_and_memory_alloc
07 -pointers_and_memory_alloc
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 
Accord.Net: Looking for a Bug that Could Help Machines Conquer Humankind
Accord.Net: Looking for a Bug that Could Help Machines Conquer HumankindAccord.Net: Looking for a Bug that Could Help Machines Conquer Humankind
Accord.Net: Looking for a Bug that Could Help Machines Conquer Humankind
 
Pseudocode
PseudocodePseudocode
Pseudocode
 

Concept_of_NAN_IND_INF_DEN_Using_C++

  • 1. Numerical Concepts of NaN, IND, INF and DEN Mohammed Nisamudheen S Project Lead UVJ Technologies
  • 2. I would like to share some concepts (some of you might know it already) that C++ offers when dealing floating point arithmetic. 1. Concept of NaN NaN means Not a Number. For an average developer, when something is said to be not a number then it would be a string. This is not the case here. When we perform extensive numerical calculations, the result will be such that it cannot be treated as a number! As an example, consider the below code. double dSQRTValue = sqrt( -1.00 ); // An image processing algorithm may invoke the sqrt() with -1 as its input . double dResult = -dSQRTValue; // A image processing algorithm may involve taking the negative of another value. Here the variable dResult will contain a NaN. So a NaN represents a numeric quantity that cannot be treated as a valid quantity. What can be done to represent it ? Usually we designate 0 or -1 to mark an invalid entry in a float or double variable/array. This kind of idea will not work here because -1 and 0 are valid numbers. A. Representation of NaN I. Non Standard Representation Define a long array of size 2. const unsigned long const lnNAN[2] = {0x00000000, 0x7ff80000}; Now, cast it to a double value! const double NOT_A_NUMBER = *( double* )lnNAN; Now, the constant variable NOT_A_NUMBER contains a NaN. II. Standard Representation The <limits> header file defines the following functions for getting a NaN. const double NOT_A_NUMBER = std::numeric_limits<double>::quiet_NaN();
  • 3. B. How a NaN Looks Like? Above is how a NaN is displayed in the debugger. We will get the same string representation with functions such as sprint() and stream classes such as stringstream. C. Comparison of NaN I. Non Standard Method bool bNaN = false; if( 0 == memcmp( &NOT_A_NUMBER, &dQNan, sizeof(double))) { bNaN = true; } II. Standard Method The "float.h" header file defines the function _isnan() for checking whether a number is NaN or not. C. Properties of NaN I. Equality Check Returns False A NaN has an important property that the comparison for equality will always return false. That is if( dResult == dResult ) { int a = 0; // Code inside this block will NEVER execute. } II. Any Calculation with a NaN Returns a NaN dResult += 1234; Here the variable dResult will contain a NaN.
  • 4. Note:- The Non Standard way of representation is just for your understanding on how a NaN is represented in memory. Please note that it is NOT the only way of representing a NaN in memory, there can be other representations. For more information, we need to refer the IEEE floating point representation.
  • 5. 2. Concept of IND IND means Indeterminate Number. An IND number is a value that is one step down from NaN. That is, an IND is a value that is almost equivalent to a NaN. There are situations in computation whose result cannot be determined by the FPU (Floating Point Unit). In such cases the result will be set as an indeterminate number. As an example, consider the below code. double dInfinity = <INF>; // Concept of Infinity will be explained next. double dIND = dInfinity / dInfinity; // Arithmetic operations may eventually reach a point at which it divides two infinite numbers. Here the variable dIND will contain an IND. Another one double dZero = 0.00; // This is defined just for demonstration. double dIND1 = dZero / dZero; // Extensive algorithmic operations may consequently perform 0/0. Here the variable dIND1 will contain an IND. Examples are given just for understanding. There can be other situations in which the result of an expression produces an IND value. A. Representation of IND I. Non Standard Representation Define a long array of size 2. const unsigned long const lnIND[2] = {0x00000000, 0xfff80000}; Now, cast it to a double value.
  • 6. const double AN_INDETERMINATE = *( double* )lnIND; Please note that the lnIND contains a different value when compared to the corresponding NaN representation. II. Standard Representation I could not find any functions that provides the standard representation of an IND number. This may be due to the fact that C++ (Microsoft) treats an IND as a NaN. This point is evident from the fact that the function _isnan() returns true (a non zero) when an IND is given as input. B. How an IND Looks Like? Above is how an IND is displayed in the debugger. We will get the same string representation with functions such as sprint() and stream classes such as stringstream. There can be both – VE and +VE representation of IND value. The string representation such as 1.#IND000000000000 are the Windows OS/Microsoft specific representation. The concept and the internal representation ( i.e. IEEE Floating Point Format) will be same across Platforms/Environment but the user level Keyword/String will be different. C. Comparison of IND I. Non Standard Method bool bIND = false; if( 0 == memcmp( &AN_INDETERMINATE, &dIND, sizeof(double))) { bIND = true; }
  • 7. II. Standard Method So far, I could not find any standard functions. One tricky solution (on Windows Platform) is to take the string representation of the double value and then check for the presence of substring ‘#IND’. C. Properties of IND I. Equality Check Returns False An IND has an important property that the comparison for equality will always return false. That is if( dIND == dIND ) { int a = 0; // Code inside this block will NEVER execute. } II. Any Calculation with a IND Returns an IND or NaN dIND += 1234; // dIND will hold an IND dIND += -dIND; // dIND will hold a NaN Note:- The Non Standard way of representation is just for your understanding on how an IND is represented in memory. It is NOT the only way of representing an IND in memory, there can be other representations. For more information, refer the IEEE 754 floating point representation.
  • 8. 3. Concept of INF INF means Infinity. An arithmetic operation results in an infinite number when the result of operation cannot be held in the corresponding data type. Here the result is said to be overflowed. That is, the result has overflowed the available storage space. In such cases, the result is marked as INF. As an example, consider the below code. double dZero = 0.00; // This is defined just for demonstration. double dINF = 1/dZero ; Here the variable dINF will contain an infinity. Examples are given just for understanding. There can be other situations in which the result of an expression produces an INF value. A. Representation of INF I. Non Standard Representation Define a long array of size 2. const unsigned long const lnINF[2] = {0x00000000, 0x7ff00000}; Now, cast it to a double value.
  • 9. const double AN_INFINITY_POSITIVE = *( double* )lnINF; II. Standard Representation The <limits> header file defines the following function for getting an INF value . const double AN_INFINITY_POSITIVE = std::numeric_limits<double>::infinity(); Since there are both +VE and –VE infinity, the above function returns a +VE infinity. Negative infinity can be obtained as below. const double AN_INFINITY_NEGATIVE = -AN_INFINITY_POSITIVE; B. How an INF Looks Like? Above is how a +VE INF is displayed in the debugger. We will get the same string representation with functions such as sprint() and stream classes such as stringstream. The string representation such as 1.#INF000000000000 are the Windows OS/Microsoft specific representation. The concept and the internal representation ( i.e. IEEE Floating Point Format) will be same across Platforms/Environment but the user level Keyword/String will be different.
  • 10. C. Comparison of INF I. Non Standard Method bool bINF = false; if( 0 == memcmp( &AN_INFINITY_POSITIVE, &dINF, sizeof(double)) || 0 == memcmp( &AN_INFINITY_NEGATIVE, &dINF, sizeof(double))) { bINF = true; } II. Standard Method The "float.h" header file defines the function _finite() for checking whether a number is INF or not. There are other standard methods too. C. Properties of INF I. Equality Check Returns True An INF has a property that the comparison for equality will always return True. That is if( dINF == dINF ) { int a = 0; // Code inside this block WILL be executed. }
  • 11. if( -dINF == -dINF ) { int a = 0; // Code inside this block WILL be executed. } II. Any Calculation with a INF Returns an IND or NaN dINF += -dINF; // dINF will hold an IND dINF += NOT_A_NUMBER; // dINF will hold a NaN Note:- The Non Standard way of representation is just for your understanding on how an INF is represented in memory. It is NOT the only way of representing an INF in memory, there can be other representations. For more information, refer the IEEE 754 floating point representation.
  • 12. 4. Concept of DEN DEN means Denormalized. It is also known as Subnormal. All of us know that there are infinite rational numbers between 0 and 1. Have you ever thought how much out of the infinite numbers a computer can store? Since a computer is a finite machine, there are limitations. It has limitation in the representation of floating numbers. We know that the float and the representation. This representation has two parts. One is the the Exponent part. An example is shown below.  Suppose an arithmetic operation results in a number that is very close to zero but NOT zero.  Due to the floating point representation limit, the CPU may not be able to represent it for further computation.  In this case, the number is marked as a denormalized number. As an example, consider the below code. double dDenTest = 0.01E- dDenTest /= 10; // This will produce a denormalized number. . It is also known as Subnormal. All of us know that there are infinite rational numbers between 0 and 1. Have you ever thought out of the infinite numbers a computer can store? Since a computer is a finite machine, there are limitations. It has limitation in the representation and the double data types are represented by the IEEE 754 representation. This representation has two parts. One is the Mantissa part and the second is part. An example is shown below. Suppose an arithmetic operation results in a number that is very close to zero but NOT Due to the floating point representation limit, the CPU may not be able to represent it for further computation. In this case, the number is marked as a denormalized number. As an example, consider the below code. -305; // This will produce a denormalized number. All of us know that there are infinite rational numbers between 0 and 1. Have you ever thought Since a computer is a finite machine, there are limitations. It has limitation in the representation data types are represented by the IEEE 754 floating point part and the second is Suppose an arithmetic operation results in a number that is very close to zero but NOT Due to the floating point representation limit, the CPU may not be able to represent it
  • 13. Examples are given just for demonstration. There can be other situations in which the result of an expression produces an DEN value. A. Representation of DEN I. Non Standard Representation Define a long array of size 2. const unsigned long const lnDEN[2] = {0x00000001, 0x00000000}; Now, cast it to a double value. const double A_DENORMAL = *( double* )lnDEN; II. Standard Representation The <limits> header file defines the following function for getting a DEN value . double dDEN = std::numeric_limits<double>::denorm_min(); B. How a DEN Looks Like?
  • 14. Above is how a DEN value is displayed in the debugger. We will get the same string representation with functions such as sprint() and stream classes such as stringstream. The string representation is the Windows OS/Microsoft specific representation. The concept and the internal representation ( i.e. IEEE 754 Floating Point Format) will be same across Platforms/Environment but the user level Keyword/String will be different. C. Comparison of DEN I. Non Standard Method bool bDEN = false; if( 0 == memcmp( &A_DENORMAL, &dDEN, sizeof(double))) { bDEN = true; } II. Standard Method if ( dDEN != 0 && fabsf ( dDEN ) <= numeric_limits<double>::denorm_min()) { // it's denormalized bDEN = true; } C. Properties of DEN
  • 15. I. Equality Check is Same as Numeric Comparison Since there can be multiple way of representing a DEN, a if( dDEN == dDEN ) { int a = 0; // Code inside this block WILL be executed. } II. Any Calculation with a DEN is Same as Normal Calculation double dDenTest = 0.01E-305; dDenTest /= 10; // This will produce a denormalized number. dDenTest *= 10; // This will result in the previous normalized value. Note:- The Non Standard way of representation is just for your understanding on how an DEN is represented in memory. It is NOT the only way of representing an DEN in memory, there can be other representations. For more information, refer the IEEE 754 floating point representation.
  • 16. This is the last page of this document.