Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Double & Float primitive types

1. ## 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

4. ## 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

5. ## Re: Double & Float primitive types

Huh? How did the links not answer that? And why do you think you need to know this?

6. ## 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.

7. ## Re: Double & Float primitive types

This sure does smell like a homework assignment.

8. ## 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

9. ## 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.

10. ## 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

11. ## 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.