# Double & Float primitive types

• September 8th, 2011, 10:44 PM
Zeek
Double & Float primitive types
Hi guys,

I have a quick question: What is the range the variable type "double" and "float" can hold, in terms of 2?, that is 2 tothepower of "x". Also the cardinality would be greatly appreciated.

Thanks
• September 8th, 2011, 10:58 PM
helloworld922
Re: Double & Float primitive types
• September 9th, 2011, 12:03 PM
KevinWorkman
Re: Double & Float primitive types
• September 9th, 2011, 04:20 PM
Zeek
Re: Double & Float primitive types
This are all good links, but they don't answer my question. I need to know the binary representation of a double type number inside the memory. For example an integer value takes space like this inside the 4 bits from the last to the first filling the 1 and 0 until the number is described. But what happens to float and double numbers? This question goes hand in hand with its range in terms of [ -2^x , 2^x + Y] or something similar
• September 9th, 2011, 04:27 PM
KevinWorkman
Re: Double & Float primitive types
Huh? How did the links not answer that? And why do you think you need to know this?
• September 9th, 2011, 04:39 PM
helloworld922
Re: Double & Float primitive types
The binary representation of Java floats (or single precision floating point numbers) and Java doubles (or double precision floating point numbers) is very clearly presented in the first two Wikipedia links I posted. The two links also provide very clear formulas/methods for converting between the computer binary representation and a human readable format.

IEEE 754-2008 - Wikipedia, the free encyclopedia provides a table of how many binary digits of precision you can expect from both of those two data types (float = binary32, double = binary64), as well as the range for the exponents.
• September 9th, 2011, 05:03 PM
KevinWorkman
Re: Double & Float primitive types
This sure does smell like a homework assignment.
• September 11th, 2011, 02:52 PM
Zeek
Re: Double & Float primitive types
Actually no is not for homework. Yes I read the links which have a very pretty diagram showing the binary representation. I just needed a layman's terms explanation of how it works but thanks for judging <3.

If you still are feeling friendly could you answer how to properly subtract doubles so that instead of 0.129999999999999 I get 0.13 when subtracting 20.37 from 20.50. Both values are stored in a double type instance variable.

-Tried BigDecimal and it just makes it worse. Result: same with more digits.

keeping it friendly,
-zeek
• September 11th, 2011, 02:58 PM
Sean4u
Re: Double & Float primitive types
You can't subtract doubles with BigDecimal (another edit: yes you can, but not the way you think you can). You can subtract BigDecimals with BigDecimals - that'll work. If you're constructing BigDecimals with a double value, your BigDecimal will already be 'wrong' - garbage in, garbage out and all that.

edit: the API doc for the BigDecimal(double) constructor explains this very clearly I've just noticed.
• September 11th, 2011, 06:48 PM
helloworld922
Re: Double & Float primitive types
0.13 cannot be represent exactly in IEEE 754-2008 binary notation (for any finite mantissa bit length). This is one of the major implications of the way floating point numbers are represented why they are not used to for exact calculations (such as calculations involving money).

For an online IEEE 754 converter:

IEEE 754 Converter
• September 12th, 2011, 07:31 AM
KevinWorkman
Re: Double & Float primitive types
Google "what every computer scientist should know about floating-point arithmetic" for a popular article that explains what you aren't understanding.