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?
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).
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.
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.
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.
Most floating point representations are based upon the IEEE standard, which has set patterns defined for Inf and NaN.
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:
E=255
and F
is nonzero, then V=NaN
("Not a number") E=255
and F
is zero and S
is 1
, then V=-Infinity
E=255
and F
is zero and S
is 0
, then V=Infinity
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. E=0
and F
is nonzero, then V=(-1)**S * 2 ** (-126) * (0.F)
These
are "unnormalized" values. E=0
and F
is zero and S
is 1
, then V=-0
E=0
and F
is zero and S
is 0
, then V=0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With