Math errors in python
Bengt Richter
bokr at oz.net
Mon Sep 20 01:42:10 EDT 2004
On Mon, 20 Sep 2004 15:16:07 +1200, Paul Foley <see at below.invalid> wrote:
>On 19 Sep 2004 15:24:31 -0700, Dan Bishop wrote:
>
>> There are, of course, reasonably accurate rational approximations of
>> pi. For example, 355/113 (accurate to 6 decimal places), 312689/99532
>> (9 decimal places), or 3126535/995207 (11 decimal places). Also, the
>> IEEE 754 double-precision representation of pi is equal to the
>> rational number 4503599627370496/281474976710656.
>
>I hope not! That's equal to 16. (The double float closest to) pi is
>884279719003555/281474976710656
>
Amazingly, that is _exactly_ equal to math.pi
>>> from ut.exactdec import ED
>>> import math
>>> ED('884279719003555/281474976710656')
ED('3.141592653589793115997963468544185161590576171875')
>>> ED(math.pi,'all')
ED('3.141592653589793115997963468544185161590576171875')
>>> ED('884279719003555/281474976710656') == ED(math.pi,'all')
True
>>> ED('884279719003555/281474976710656').astuple()
(3141592653589793115997963468544185161590576171875L, 1L, -48)
>>> ED(math.pi,'all').astuple()
(3141592653589793115997963468544185161590576171875L, 1L, -48)
So it's also equal to the rational number
3141592653589793115997963468544185161590576171875 / 10**48
>>> ED('3141592653589793115997963468544185161590576171875'
... '/1000000000000000000000000000000000000000000000000')
ED('3.141592653589793115997963468544185161590576171875')
or
>>> ED('3141592653589793115997963468544185161590576171875') / ED(10**48)
ED('3.141592653589793115997963468544185161590576171875')
Regards,
Bengt Richter
More information about the Python-list
mailing list