Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
math.pi
#4
(Jan-08-2018, 04:23 AM)Skaperen Wrote: ah! in C code. that's why my search didn't find it. FYI, here is how my C code defines Pi so it can work with some higher precision architectures with long double:

#define Pi (8552228672519733982877442985294966266405.0L/2722258935367507707706996859454145691648.0L)

basically that is Pi scaled up by 2**131 divided by 2**131.

Unfortunately, your fraction only provides 53 bits of accuracy, not the 64 bits required by long double.

>>> import math
>>> math.pi
3.141592653589793
>>>
>>> import gmpy2
>>> gmpy2.get_context().precision=200
>>> gmpy2.const_pi()
mpfr('3.1415926535897932384626433832795028841971693993751058209749445',200)
>>> a=gmpy2.mpfr(8552228672519733982877442985294966266405.0)
>>> b=gmpy2.mpfr(2722258935367507707706996859454145691648.0)
>>> a/b
mpfr('3.141592653589793115997963468544185161590576171875',200)
>>> # Incorrect after ^
You can check the bit patterns of the the mantissa in gmpy2.

>>> a
mpfr('8552228672519733649496873820484995645440.0',200)
>>> b
mpfr('2722258935367507707706996859454145691648.0',200)
>>> (a/b).digits(2)
('11001001000011111101101010100010001000010110100011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 2, 200)
>>> gmpy2.const_pi(precision=64).digits(2)
('1100100100001111110110101010001000100001011010001100001000110101', 2, 64)
>>> gmpy2.const_pi(precision=53).digits(2)
('11001001000011111101101010100010001000010110100011000', 2, 53)
>>> 
casevh
Reply


Messages In This Thread
math.pi - by Skaperen - Jan-08-2018, 03:17 AM
RE: math.pi - by casevh - Jan-08-2018, 04:23 AM
RE: math.pi - by Skaperen - Jan-08-2018, 04:35 AM
RE: math.pi - by casevh - Jan-08-2018, 05:08 AM
RE: math.pi - by casevh - Jan-08-2018, 06:17 AM
RE: math.pi - by wavic - Jan-08-2018, 07:06 AM
RE: math.pi - by casevh - Jan-08-2018, 07:31 AM
RE: math.pi - by Gribouillis - Jan-08-2018, 09:44 AM
RE: math.pi - by casevh - Jan-08-2018, 01:22 PM
RE: math.pi - by wavic - Jan-08-2018, 10:22 AM
RE: math.pi - by Gribouillis - Jan-08-2018, 11:29 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020