Reputation: 121
Can all 16-bit unsigned integer (unsigned short) values be stored EXACTLY in a 32-bit floating point value? Can all 32-bit unsigned integer (unsigned int) values be stored EXACTLY in a 32-bit floating point value? Which of the numbers below can be represented EXACTLY with a 32-bit float value? 1.648, 2.75, 2.125, 10.999, 9.351, 0.625
Upvotes: 0
Views: 652
Reputation: 80276
As a consequence all unsigned 16-bit integers can be represented exactly as floats, but not all unsigned 32-bit integers.
The explanation is that some bit patterns are used to represent fractional numbers such as 0.125 and 1.5, so fewer than 232 bit patterns are available for integers.
A simple program in your programming language of choice can tell you which of 1.648, 2.75, 2.125, 10.999, 9.351, 0.625 are exactly representable in single-precision: print, to 26 significant decimal digits (say, with the format %.25e
using a printf
-like function), the values of 1.648f
, …
When you type the floating-point constant 1.648f
in a program in most programming languages, it is translated to the single-precision float closest to 1648/1000. If this number prints as 1.64800000
, it means that the number 1.648 is exactly representable as a float. If it prints as something else, then 1.648 isn't representable (and the output tell you how far the nearest float happens to be).
Upvotes: 1