Decimal arithmatic, was Re: Python GUI app to impress the boss?
Paul Rubin
phr-n2002b at NOSPAMnightsong.com
Mon Sep 30 00:42:21 EDT 2002
"Chris Gonnerman" <chris.gonnerman at newcenturycomputers.net> writes:
> > What do you mean by "right"? What do you mean by "all
> > cases"?
>
> The original subject is *business* arithmetic. There is no
> choice but to use decimal arithmetic if you want your math
> results to match good-old-fashioned pen and paper math.
No, I'm still not convinced. Is business arithmetic well-defined
enough that you can never do the same calculation two ways and get
different results?
Example: bunches are three for a dollar. How much do you pay if you
buy nine bananas?
Answer #1:
price_per_banana = 1.00 / 3
number_of_bananas = 9
total_price = number_of_bananas * price_per_banana
print total_price
Answer #2:
price_per_bunch = 1.00 # three bananas
number_of_bunches = 3
total_price = number_of_bunches * price_per_banana
print total_price
With floating point and rounding on output, you get the same answer
both ways.
With decimal arithmetic, you get $2.97 the first way and $3.00 the
second way.
For "business arithmetic" to always give the same answer, it has to
forbid at least one of those methods of doing the problem.
More information about the Python-list
mailing list