GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes 0.1 + 0.2 is not equal 0.3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "0.1 + 0.2 is not equal 0.3" Watch "0.1 + 0.2 is not equal 0.3" New topic
Author

0.1 + 0.2 is not equal 0.3

Qusay Jaafar
Ranch Hand

Joined: May 06, 2002
Posts: 127

the result will be:
z: 0.30000000000000004
This result happens with the double only, why?


Qusay
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
There is a fairly good discussion of this confusion here.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In short, 0.1 decimal doesn't have a finite representation in binary.

That's very similar to 1/3 (= 0.1 in ternary) doesn't have a finite representation in decimal.


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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See the topic, "Some things you should know about floating point arithmetic" in the following page:
http://java.sun.com/developer/JDCTechTips/2003/tt0204.html

Also see the following JavaRanch thread:
http://www.coderanch.com/t/375505/java/java/Java-Subtraction-Incorrect
...and the linked topics within that thread.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's kinda surprising given the hype about computers that they really are not very good at math. Any middle-schooler knows that:

( 1 / 3 * 3 ) = ( 1 * 3 / 3 ) = 1

but it's a very rare computer language that gets that right. You have to be very careful with any floating arithmetic, and even more careful with decimal money. Use COBOL for that

When comparing floats you might want to make a comparison method with a fuzz factor: if ( abs( a - b ) < fuzz factor ) return true.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
it's a very rare computer language that gets that right. You have to be very careful with any floating arithmetic, and even more careful with decimal money. Use COBOL for that


I'd rather go with Smalltalk...
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
If you need accuracy use the BigDecimal class.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Ilja, I only used Smalltalk for a month or two a decade ago ... does it have a good decimal or money class?

I'm not sure whether BigDecimal is more accurate or more predictably wrong. 1 / 3 * 3 still doesn't equal 1, but it's probably exactly what you want for money. I remember a brilliant math major and instructor wondering why in COBOL 1 / 3 * 3 did not equal 1 * 3 / 3. The math is willing but the bits are weak.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
Yes, when we develop an application for the bank transaction, it will become some tips for us..... I wonder why that situation did not happen in our real live.


Francis Siu
SCJP, MCDBA
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It happens every day. We're used to rounding money when computing sales tax or discounts or loan interest. My paycheck goes up or down a penney now & then which I assume is for rounding of some kind. The movie "Office Space" revived an old urban legend: programmers changed every transaction to round down to the nearest penney instead of up or down as you'd expect and pocketed the difference. It will be important for your banking app to behave as customers expect!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Well, BigDecimal's precision is "arbitrary."

"When a MathContext object is supplied with a precision setting of 0..., arithmetic operations are exact... [But] in the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3. If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown."

Ref: http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html
[ April 06, 2005: Message edited by: marc weber ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
Ilja, I only used Smalltalk for a month or two a decade ago ... does it have a good decimal or money class?


I've never used Smalltalk, but from what I hear, the number classes are quite good. It has a Fraction class, so 1 / 3 * 3 actually is *exactly* 1. And it works with arbitrarily large numbers, transparently.
 
GeeCON Prague 2014
 
subject: 0.1 + 0.2 is not equal 0.3