Re: [Hampshire] Python multiplicaton

Top Page
Author: Hugo Mills
Date:  
To: Hampshire LUG Discussion List
Subject: Re: [Hampshire] Python multiplicaton

Reply to this message
gpg: failed to create temporary file '/var/lib/lurker/.#lk0x586b8100.hantslug.org.uk.32061': Permission denied
gpg: keyblock resource '/var/lib/lurker/pubring.gpg': Permission denied
gpg: Signature made Sat Jan 16 13:14:24 2010 GMT
gpg: using DSA key 20ACB3BE515C238D
gpg: Can't check signature: No public key
On Sat, Jan 16, 2010 at 10:11:31AM +0000, Owain Clarke wrote:
> On Thu, Jan 14, 2010 at 11:51:35AM +0000, Owain Clarke wrote:
> >> Could anyone explain to me why 3*7.6 gives me 22.799999999999997? I have
> >> Python 2.5.2
> >>
> OK, now I understand. Thanks all - will continue to seek a workaround,
> and I'm guessing that, as the amounts are to do with pounds and pence, I
> should convert everything pence before multiplying so that the
> calculation is with integers.


I believe that this is the usual method applied with financial
values. You should also be aware of the limits of integer computation
-- with 32-bit numbers, you can only get to about 42 million pounds
before running out of integers. You can get arbitrary-sized integers
in Python by casting everything to long().

Another alternative would be to use the decimal module[1] for all
your numbers.

Hugo.

[1] http://docs.python.org/library/decimal.html

-- 
=== Hugo Mills: hugo@... carfax.org.uk | darksatanic.net | lug.org.uk ===
  PGP key: 515C238D from wwwkeys.eu.pgp.net or http://www.carfax.org.uk
  --- You can play with your friends' privates,  but you can't play ---  
        with your friends' childrens' privates. (C++ coding rule)