Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the internal representation of inf and NaN?

Tags:

fortran

A friend & I were debating how Inf's and NaN's are stored during lunch today.

Take Fortran 90 for example. 4-byte reals can obtain the value of Inf or NaN. How is this stored internally? Presumably, a 4-byte real is a number represented internally by a 32 digit binary number. Are Inf's and NaN's stored as 33 bit binary numbers?

like image 690
Pete Avatar asked Mar 12 '09 19:03

Pete


People also ask

How is NaN represented?

Binary format NaNs are represented with the exponential field filled with ones (like infinity values), and some non-zero number in the significand field (to make them distinct from infinity values).

Is infinity a NaN?

nan means "not a number", a float value that you get if you perform a calculation whose result can't be expressed as a number. Any calculations you perform with NaN will also result in NaN . inf means infinity.

What does INF mean in code?

Inf means infinity, and is the result of dividing a positive number by zero -- e.g., 1/0 → Inf. or computing a number larger than 1.796E308 (the largest number that your computer can represent in 64 bits) -- e.g. 1E307 * 100 → Inf.

How is infinity represented in binary?

Positive and negative infinity are represented thus: sign = 0 for positive infinity, 1 for negative infinity. biased exponent = all 1 bits. fraction = all 0 bits.


2 Answers

Most floating point representations are based upon the IEEE standard, which has set patterns defined for Inf and NaN.

like image 44
Pesto Avatar answered Oct 22 '22 19:10

Pesto


Specifically from Pesto's link:

The IEEE single precision floating point standard representation requires a 32 bit word, which may be represented as numbered from 0 to 31, left to right. The first bit is the sign bit, S, the next eight bits are the exponent bits, 'E', and the final 23 bits are the fraction 'F':

S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF  
0 1      8 9                    31  

The value V represented by the word may be determined as follows:

  • If E=255 and F is nonzero, then V=NaN ("Not a number")
  • If E=255 and F is zero and S is 1, then V=-Infinity
  • If E=255 and F is zero and S is 0, then V=Infinity
  • If 0<E<255 then V=(-1)**S * 2 ** (E-127) * (1.F) where "1.F" is intended to represent the binary number created by prefixing F with an implicit leading 1 and a binary point.
  • If E=0 and F is nonzero, then V=(-1)**S * 2 ** (-126) * (0.F) These are "unnormalized" values.
  • If E=0 and F is zero and S is 1, then V=-0
  • If E=0 and F is zero and S is 0, then V=0
like image 88
Andy Mikula Avatar answered Oct 22 '22 18:10

Andy Mikula