Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# dunno why this is the output..??

Jack Daniel
Ranch Hand
Posts: 163
Well, I have come across a strange problem.. in a project
I have cut and put the confusing part in another small program..

Can anybody tell me WHY itr is giving 3.7199999999999998
instead of 3.72
I would be soo thankful, if anybody can help me with this ...
Thanks e'body..

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
Computers (well, most computers) use what is called a floating point representation for real numbers. If you're familiar with scientific notation -- i.e., 1.5E-3 is 0.0015 -- then you understand how it works. There's a mantissa (the first number in scientific notation) and an exponent (the second number.) A floating point representation allocates a fixed number of bits for the mantissa, and a fixed number for the exponent.
Now, the problem is that many rational numbers -- 1/3 for example -- are infinitely repeating fractions: 1/3 is 0.333333333... (forever.) You can't represent such a number exactly in floating-point notation, because there aren't an infinite number of bits. So sometimes in computer arithmetic you see something like 0.99999999999999123 when you expected 1.0 -- this is just the way it is. If you need to display floating-point numbers, you should never just println() them; you should use Java's formatting tools (like DecimalFormat, for instance) which can round the numbers appropriately for display.
For similar reasons, it's bad idea to compare floating-point numbers using ==; 0.9999999999999999123 and 1.0 are different values as far as the computer is concerned.

Ilja Preuss
author
Sheriff
Posts: 14112
What makes the situation even more complicated is that most often (as in this case), the floating point numbers are stored in the binary system (instead of the decimal). 0.1 decimal is in fact an infinitely repeating fraction in the binary system: 0.000110001100011.... (whereas in the ternary system, 1/3 decimal would be representable without any loss of precision: 0.1)

Jack Daniel
Ranch Hand
Posts: 163
Thx a lot people, for your replies...
cheers