tags:

views:

433

answers:

6

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).

Thanks!

A: 

In practice, yes. However, I doubt that it is required by standard. If a platform with 64-bit ints (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.

doublep
It would help to decide if it is specified in the standard if you said which standard you are talking about. IEEE 754 implies that double-precision floats contain single-precision floats. C99 strongly encourages to support IEEE 754 with float for IEEE 754 single-precision and double for IEEE 754 double precision.
Pascal Cuoq
A: 

Read through http://docs.sun.com/source/806-3568/ncg_goldberg.html

Mart Oruaas
+8  A: 

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

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.

Atømix
The question is only about the ability to represent all floats as doubles. That's a very long way of saying "yes".
Pascal Cuoq
Too much information. But if you are going to give this much information, you are misleading by not also explaining that the most significant 1 bit in the significand is implicit, except for zero (which is special) and for denormalized numbers.Finally, we're not allowed to call it a "mantissa" any more; we're supposed to say "significand".
Norman Ramsey
But it's rather interesting and detailed. You can say yes in the beginning and elaborate later.
Viet
answer accepted for pretty diagram
anon
@Pascal: You are correct. I have made the change.I did get a little carried away. I just remember the revelation I had in understanding when it was first explained to me back when I was doing Comp. Sci. courses.@Norman. Thanks for the clarifications.
Atømix
+6  A: 

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.

Nils Pipenbrinck
+4  A: 

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.

Steve Jessop
Thanks for this answer.
Pascal Cuoq
+1  A: 

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.)

Norman Ramsey