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/.#lk0x5868b100.hantslug.org.uk.16699': Permission denied
gpg: keyblock resource '/var/lib/lurker/pubring.gpg': Permission denied
gpg: Signature made Thu Jan 14 22:17:40 2010 GMT
gpg: using DSA key 20ACB3BE515C238D
gpg: Can't check signature: No public key
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


Because of a rounding error in the binary representation of the
fraction. If you work it out, 7.6 in binary is 111.100110001100011...
Obviously, you can't represent *every* binary digit in that sequence
(because it's infinite). So, the maths functions chop it off after a
certain point. That's going to be fractionally short of the true
value, hence the error that you see.

Hugo.

PS. If you want the real icky detail, let's assume that they're using
IEEE 754 doubles. This means that they've got a 52 bit mantissa, and
an 11 bit exponent, so the number will be:

1111001100011000110001100011000110001100011000110001_2 * 2^-49

Multiply by 3, and you get:

1011011001010010100101001010010100101001010010100100_2 * 2^-47

or

10110.11001010010100101001010010100101001010010100100_2

which is probably (I can't be bothered to work it out on a piece of
paper) 22.799999999999997, unless I've made a mistake in all of this
somewhere:

22 + 1/2      = 22.5
... + 1/4     = 22.75
... + 1/32    = 22.78125
... + 1/128   = 22.7890625
... + 1/1024  = 22.7900390625


Keep going until you reach 1/70368744177664, and then stop...

Hugo.

-- 
=== 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
       --- A cross? Oy vey, have you picked the wrong vampire! ---