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
Printable View
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
Java floats: Wikipedia - IEEE 754-2008 single precision floating point numbers
Java doubles: Wikipedia - IEEE 754-2008 double precision floating point numbers
General IEEE 754-2008 specifications: Wikipedia - IEEE 754-2008
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
Huh? How did the links not answer that? And why do you think you need to know this?
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.
This sure does smell like a homework assignment.
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
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.
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
Google "what every computer scientist should know about floating-point arithmetic" for a popular article that explains what you aren't understanding.