Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all single-precision numbers representable in the double-precision format?

Given an arbitrary number represented in the IEEE-754 single-precision format (commonly known as float in some languages/platforms) can I be certain that number can be represented exactly in the double-precision format as well?

If so, is that property valid when considering half-precision to single-precision and double-precision to quadruple-precision?

like image 860
R. Martinho Fernandes Avatar asked Oct 05 '11 10:10

R. Martinho Fernandes


2 Answers

Yes, a double can represent any number that a float can. Likewise for quad-precision, etc.

A floating-point number is represented in a form like 1.01b x 2^-1 (0.625, in this case). The significant components of the number are the significand, which is basically a binary number with a radix point usually right after the first digit, and the exponent.

The only major difference between the binary floating-point formats is the number of bits for each component. The more bits the number uses, the more bits are available for each part. So a 32-bit "float" might have 1.01000000000000000000000 for the significand, and a (64-bit) "double" would have about 50 digits after the dot. This means that any number that is exactly representable in a float is also exactly representable in a double, since you have both increased precision (read: more significant digits) and increased range. It's similar to how a 64-bit integer variable can hold any 32-bit integer; the extra bits just pretty much go unused.

Of course, any bits that got chopped off due to rounding error won't make it back into the number when you convert it to a double -- meaning the 0.3 you have in your float, being an inexact result like 0.2999999875 or something (i don't feel like calculating), isn't going to get any closer to 0.3 when you convert it to a double -- it's going to still be 0.2999999875. If you want a closer approximation, you'll need to redo the calculations with doubles from the start.

like image 69
cHao Avatar answered Sep 21 '22 15:09

cHao


Yes. In fact, you can make an even stronger statement: every product of two single-precision numbers is representable exactly in double precision (ditto for half and single or double and quad).

like image 40
Stephen Canon Avatar answered Sep 18 '22 15:09

Stephen Canon