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
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
posted
0
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
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.
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
posted
0
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!
"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."
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.