Can a double represent all values a float can represent?
There are certain int values that a float can not represent.
However, can a double represent all values a float can represent?
My intuition says yes, 开发者_如何学运维since double has more fractional bits & more exponent bits, but there might be some silly gotchas that I'm missing.
Yes.
It would probably help to know how floats and doubles work.
Without going too much into details...
Take the number 152853.5047
( the revolution period of Jupiter's moon Io in seconds )
In scientific notation, this number is 0.1528535047 × 10^6
Since computers only understand 1 and 0, there is way to define .
The mantissa (1528535047) and the exponent (6) are stored within 32-bits... if I remember correctly, only 24-bits are for the mantissa, so floating point is usually more about precision than size. The larger the number, the less precise it can be.
1528535047 = 1011011000110111001100000000111
so you can only store the first 24-bits... the last three 1's are lopped off.
Since Integers are 32-bits, you're right, a floating point can't accurately contain it. less significant digits get lopped off the end.
Any integer with an absolute value of less than 2^24 ( 24-bits )can be stored without losing precision. (16,777,216)
This is how the bits are stored in a floating point number:
How floats are stores diagram http://phimuemue.wordpress.com/files/2009/06/576px-ieee-754-single-svg1.png
source One bit for the sign, 8-bits for the exponent and 23-bits for the mantissa. Therefore, to answer your question, since only 23-bits are reserved for the mantissa, a 32-bit integer can't be showed with precision. It will quickly start lopping off numbers ( from the right ) as there are more digits needed to display.
For a double, you're merely increasing the number of bits that it can store... in fact, it's called double precision so any number that can be shown as a float is capable of being shown as a double. Extra 0's are merely added to the mantissa.
For this reason, since a double takes up 64-bits, most people will use a double when converting from a 32-bit int to a double. A float would be good for converting a 16-bit short.
6.2.5/10 in n1256:
There are three real floating types, designated as float, double, and long double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double.
(emphasis mine).
Whether the implementation uses IEEE754 or not is irrelevant, the C99 standard guarantees what you want.
Yes, a double can represent all values a float can.
Here is why:
Both numbers are represented as the sign, the exponent and the mantissa. The difference between float and double is, that there is more space for the exponent and the mantissa.
For the exponent the wider range is no problem. You can represent all byte-values with an int, and the same is true for the exponent. The mantissa is a bit different, but if you fill the extra bits of the double mantissa with zeros you will get exactly the same value as the float.
It may be easier to understand in decimal: Say you have a number in decimal like this:
1.99234
This number has 5 decimal places behind the decimal point. What would you do if you had to extend the same number to 10 decimal places? Easy: Add zeros:
1.9923400000
It is exactly the same number, just represented with more accuracy in the mantissa.
Any float
number can be represented as a double
. I'm not sure about NaNs. (I'm sure you can represent a float
NaN as double
by tacking extra zeros onto the significand, but I'm not sure if software or hardware that reacts to NaNs will react to such a NaN in exactly the same way.)
In practice, yes. However, I doubt that it is required by standard. If a platform with 64-bit int
s (AFAIK on current 64-bit platforms int
is actually 32-bit, but long
is 64) appears and it has double
that's also 64-bit, then some int
values would be not representable as double
values.
Read through http://docs.sun.com/source/806-3568/ncg_goldberg.html
精彩评论