pi

Python pi calculation?

回眸只為那壹抹淺笑 提交于 2019-11-27 01:53:37
问题 I am a python beginner and I want to calculate pi. I tried using the Chudnovsky algorithm because I heard that it is faster than other algorithms. This is my code: from math import factorial from decimal import Decimal, getcontext getcontext().prec=100 def calc(n): t= Decimal(0) pi = Decimal(0) deno= Decimal(0) k = 0 for k in range(n): t = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k) deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k)) pi += Decimal(t)/Decimal(deno) pi = pi * Decimal

OverflowError: (34, 'Result too large')

余生颓废 提交于 2019-11-26 22:42:06
I'am getting an overflow error(OverflowError: (34, 'Result too large') I want to calculate pi to 100 decimals here's my code: def pi(): pi = 0 for k in range(350): pi += (4./(8.*k+1.) - 2./(8.*k+4.) - 1./(8.*k+5.) - 1./(8.*k+6.)) / 16.**k return pi print(pi()) Python floats are neither arbitary precision nor of unlimited size. When k = 349, 16.**k is much too large - that's almost 2^1400. Fortunately, the decimal library allows arbitrary precision and can handle the size: import decimal decimal.getcontext().prec = 100 def pi(): pi = decimal.Decimal(0) for k in range(350): pi += (decimal

What is the fastest way to get the value of π?

你。 提交于 2019-11-26 21:09:29
I'm looking for the fastest way to obtain the value of π, as a personal challenge. More specifically, I'm using ways that don't involve using #define constants like M_PI , or hard-coding the number in. The program below tests the various ways I know of. The inline assembly version is, in theory, the fastest option, though clearly not portable. I've included it as a baseline to compare against the other versions. In my tests, with built-ins, the 4 * atan(1) version is fastest on GCC 4.2, because it auto-folds the atan(1) into a constant. With -fno-builtin specified, the atan2(0, -1) version is

Why define PI = 4*ATAN(1.d0)

走远了吗. 提交于 2019-11-26 19:40:06
问题 What is the motivation for defining PI as PI=4.D0*DATAN(1.D0) within Fortran 77 code? I understand how it works, but, what is the reasoning? 回答1: This style ensures that the maximum precision available on ANY architecture is used when assigning a value to PI. 回答2: Because Fortran does not have a built-in constant for PI . But rather than typing in the number manually and potentially making a mistake or not getting the maximum possible precision on the given implementation, letting the library

Should I use scipy.pi, numpy.pi, or math.pi?

让人想犯罪 __ 提交于 2019-11-26 19:21:25
问题 In a project using SciPy and NumPy, should I use scipy.pi , numpy.pi , or math.pi ? 回答1: >>> import math >>> import numpy as np >>> import scipy >>> math.pi == np.pi == scipy.pi True So it doesn't matter, they are all the same value. The only reason all three modules provide a pi value is so if you are using just one of the three modules, you can conveniently have access to pi without having to import another module. They're not providing different values for pi. 回答2: One thing to note is

How to printf long long

主宰稳场 提交于 2019-11-26 14:26:56
问题 I'm doing a program that aproximate PI and i'm trying to use long long, but it isn't working. Here is the code #include<stdio.h> #include<math.h> typedef long long num; main(){ num pi; pi=0; num e, n; scanf("%d", &n); for(e=0; 1;e++){ pi += ((pow((-1.0),e))/(2.0*e+1.0)); if(e%n==0) printf("%15lld -> %1.16lld\n",e, 4*pi); //printf("%lld\n",4*pi); } } 回答1: %lld is the standard C99 way, but that doesn't work on the compiler that I'm using (mingw32-gcc v4.6.0). The way to do it on this compiler

How do I determine whether my calculation of pi is accurate?

跟風遠走 提交于 2019-11-26 12:48:54
问题 I was trying various methods to implement a program that gives the digits of pi sequentially. I tried the Taylor series method, but it proved to converge extremely slowly (when I compared my result with the online values after some time). Anyway, I am trying better algorithms. So, while writing the program I got stuck on a problem, as with all algorithms: How do I know that the n digits that I\'ve calculated are accurate? 回答1: Since I'm the current world record holder for the most digits of

1000 digits of pi in python

∥☆過路亽.° 提交于 2019-11-26 11:10:48
问题 I have been thinking about this issue and I can\'t figure it out. Perhaps you can assist me. The problem is my code isn\'t working to output 1000 digits of pi in the python coding language. Here\'s my code: def make_pi(): q, r, t, k, m, x = 1, 0, 1, 1, 3, 3 while True: if 4 * q + r - t < m * t: yield m q, r, t, k, m, x = (10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x) else: q, r, t, k, m, x = (q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2) digits = make_pi() pi_list = [] my_array

Gauss-Legendre Algorithm in python

烂漫一生 提交于 2019-11-26 09:53:38
问题 I need some help calculating Pi. I am trying to write a python program that will calculate Pi to X digits. I have tried several from the python mailing list, and it is to slow for my use. I have read about the Gauss-Legendre Algorithm, and I have tried porting it to Python with no success. I am reading from Here, and I would appreciate any input as to where I am going wrong! It outputs: 0.163991276262 from __future__ import division import math def square(x):return x*x a = 1 b = 1/math.sqrt(2

What is the fastest way to get the value of π?

巧了我就是萌 提交于 2019-11-26 07:52:44
问题 I\'m looking for the fastest way to obtain the value of π, as a personal challenge. More specifically, I\'m using ways that don\'t involve using #define constants like M_PI , or hard-coding the number in. The program below tests the various ways I know of. The inline assembly version is, in theory, the fastest option, though clearly not portable. I\'ve included it as a baseline to compare against the other versions. In my tests, with built-ins, the 4 * atan(1) version is fastest on GCC 4.2,