Floating point multiplication in python
Gelonida N
gelonida at gmail.com
Wed Sep 7 10:57:53 CEST 2011
On 09/07/2011 06:51 AM, Steven D'Aprano wrote:
11258999068426240
>
> Error in float 1.1*1.1:
>
>>>> b = F(11, 10)**2
>>>> y = F.from_float(1.1**2)
>>>> f = y - b
>>>> print f
> 21/112589990684262400
>
> which is slightly more than double e above, and slightly less than our
> estimate of 2*a*e = 11/56294995342131200
>
> So we can conclude that, at least for 1.1**2, Python floats are more
> accurate than we would expect from a simple application of the binomial
> theorem. (For implementations using IEEE doubles.)
The reason why the error is different from the 2*a*e is, that we
encounter two problems.
first problem is, that x = a + e
e exists because a float does have a limited number (let's call it N) of
digits and a has an infinite amount of non zero digits in the binary format.
second problem is, that the result of the multiplication is not
(a+e) * (a+e) but a 'rounded' version of it, because the floating point
representation of the result would require about 2*N digits, whereas
only N digits will be stored in the result.
depending on the rounding which happened (up or down) the error will be
bigger or smaller than the estimated one.
More information about the Python-list
mailing list