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

# 0.1 + 0.2 is not equal 0.3

Qusay Jaafar
Ranch Hand
Posts: 127

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

Horatio Westock
Ranch Hand
Posts: 221
There is a fairly good discussion of this confusion here.

Ilja Preuss
author
Sheriff
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.

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

Stan James
(instanceof Sidekick)
Ranch Hand
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.

Ilja Preuss
author
Sheriff
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
Posts: 1071
If you need accuracy use the BigDecimal class.

Stan James
(instanceof Sidekick)
Ranch Hand
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
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.

Stan James
(instanceof Sidekick)
Ranch Hand
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
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
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.