• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

0.1 + 0.2 is not equal 0.3

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the result will be:
z: 0.30000000000000004
This result happens with the double only, why?
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a fairly good discussion of this confusion here.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
https://coderanch.com/t/375505/java/java/Java-Subtraction-Incorrect
...and the linked topics within that thread.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need accuracy use the BigDecimal class.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic