Questions tagged [pi]

π (pi) is a mathematical constant whose value is the ratio of any Euclidean plane circle's circumference to its diameter; this is the same value as the ratio of a circle's area to the square of its radius. It is approximately equal to 3.14159265 in decimal notation. For Raspberry Pi, please tag with "raspberry-pi".

π (pi) is a mathematical constant whose value is the ratio of any Euclidean plane circle's circumference to its diameter; this is the same value as the ratio of a circle's area to the square of its radius. It is approximately equal to 3.14159265 in decimal notation.

For questions related to Raspberry Pi, please use the tag instead.

811 questions
792
votes
6 answers

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

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…
Ishan Sharma
  • 6,545
  • 3
  • 16
  • 21
353
votes
23 answers

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

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…
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
247
votes
3 answers

Why does Python's hash of infinity have the digits of π?

The hash of infinity in Python has digits matching pi: >>> inf = float('inf') >>> hash(inf) 314159 >>> int(math.pi*1e5) 314159 Is that just a coincidence or is it intentional?
wim
  • 338,267
  • 99
  • 616
  • 750
172
votes
3 answers

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

In a project using SciPy and NumPy, should I use scipy.pi, numpy.pi, or math.pi?
Douglas B. Staple
  • 10,510
  • 8
  • 31
  • 58
63
votes
6 answers

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

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?
ccook
  • 5,869
  • 6
  • 56
  • 81
60
votes
4 answers

How to printf long long

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 #include typedef long long num; main(){ num pi; pi=0; num e, n; scanf("%d", &n); for(e=0;…
Carlos
  • 601
  • 1
  • 5
  • 3
39
votes
11 answers

How is pi (π) calculated?

How can I write a function which will return pi (π) to a given number of decimal places? Speed is not a concern. I've been looking at http://bellard.org/pi/, but I still don't understand how to get the nth digit of pi.
jmasterx
  • 52,639
  • 96
  • 311
  • 557
38
votes
4 answers

how to use math.pi in java

I am having problems converting this formula V = 4/3 π r^3. I used Math.PI and Math.pow, but I get this error: ';' expected Also, the diameter variable doesn't work. Is there an error there? import java.util.Scanner; import…
IvanNewYork
  • 445
  • 2
  • 6
  • 10
34
votes
6 answers

Cannot import name '_gi'

I'm trying to add a repository to ppa with the add-apt-repository commands but the _gi module from Python is not found. I did this command : sudo add-apt-repository ppa:s-mankowski/ppa-kf5 Here is the traceback : Traceback (most recent call last): …
Hugo Sohm
  • 2,872
  • 4
  • 23
  • 40
34
votes
6 answers

Why do I get "OverflowError: (34, 'Result too large')" or "OverflowError: (34, 'Numerical result out of range')" from floating-point exponentiation?

I tried to use this code to calculate pi to many decimal places: 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()) Why do I get an error…
user3033766
  • 451
  • 1
  • 4
  • 7
30
votes
20 answers

How do I calculate PI in C#?

How can I calculate the value of PI using C#? I was thinking it would be through a recursive function, if so, what would it look like and are there any math equations to back it up? I'm not too fussy about performance, mainly how to go about it from…
GateKiller
  • 74,180
  • 73
  • 171
  • 204
30
votes
5 answers

Can any finite bit string be found in pi within a reasonable amount of time?

So, a while back I read a joke that went something like this: "Never compute pi in binary - because it goes on infinitely and is random, it theoretically contains every finite bit string. So, you will then possess all copyrighted material in…
Chris Staikos
  • 1,150
  • 10
  • 24
27
votes
11 answers

1000 digits of pi in Python

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,…
bobimo
  • 535
  • 2
  • 5
  • 10
25
votes
1 answer

Fast algorithm to calculate Pi in parallel

I am starting to learn CUDA and I think calculating long digits of pi would be a nice, introductory project. I have already implemented the simple Monte Carlo method which is easily parallelize-able. I simply have each thread randomly generate…
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
22
votes
2 answers

Chudnovsky binary splitting and factoring

In this article, a fast recursive formulation of the Chudnovsky pi formula using binary splitting is given. In python: C = 640320 C3_OVER_24 = C**3 // 24 def bs(a, b): if b - a == 1: if a == 0: Pab = Qab = 1 else: …
qwr
  • 9,525
  • 5
  • 58
  • 102
1
2 3
53 54