File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes dunno why this is the output..?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "dunno why this is the output..??" Watch "dunno why this is the output..??" New topic

dunno why this is the output..??

Jack Daniel
Ranch Hand

Joined: Jun 15, 2002
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

Joined: Jul 08, 2003
Posts: 24199

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.

[Jess in Action][AskingGoodQuestions]
Ilja Preuss

Joined: Jul 11, 2001
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)

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jack Daniel
Ranch Hand

Joined: Jun 15, 2002
Posts: 163
Thx a lot people, for your replies...
I agree. Here's the link:
subject: dunno why this is the output..??
It's not a secret anymore!