[FRPythoneers] I must be sitting too close to the monitor.

Andrew Diederich andrewdied at gmail.com
Tue Mar 15 11:31:21 MST 2005


This conversation closed up a while ago, but I've been thinking about
it, anyway.  In python 2.4 they came up with a decimal type which
fixes a lot of this.  I know you're not using 2.4 yet, though.

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 35.62
>>> b = 1.73
>>> a+b
37.349999999999994
>>> import decimal
>>> c = decimal.Decimal('35.62')
>>> d = decimal.Decimal('1.73')
>>> c+d
Decimal("37.35")

There's also probably a way to fudge this a bit with some work.  Going
back to my slide-rule days, while doing all your calculations without
any decimals at all.  It's up to the computer (the person doing the
computing) to keep track of where the decimal should go, then put it
back in at the end.

This should be pretty straight-forward for multiplication, harder for
division, I think, because of remainders.  Slide rules don't really do
addition and subtraction, so this is far from a fool-proof plan, I
think.  Fun to think about, though.

>>> e = 3562
>>> f = 173
>>> e*f
616226
>>> c*d
Decimal("61.6226")

-- 
Andrew



More information about the FRPythoneers mailing list