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".
823
questions
792
votes
6
answers
125k
views
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 ...
352
votes
23
answers
70k
views
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.
...
248
votes
3
answers
30k
views
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 ...
171
votes
3
answers
234k
views
Is there a difference between scipy.pi, numpy.pi, or math.pi?
In a project using SciPy and NumPy, when should one use scipy.pi vs numpy.pi vs just math.pi? Is there a difference between these values?
64
votes
6
answers
75k
views
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?
61
votes
4
answers
176k
views
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<stdio.h>
#include<math.h>
typedef long long num;
main(){
num pi;...
40
votes
11
answers
44k
views
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 ...
39
votes
4
answers
564k
views
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?
...
36
votes
6
answers
50k
views
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 ...
36
votes
6
answers
112k
views
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
...
31
votes
5
answers
4k
views
Can any finite bit string be found in pi within a reasonable amount of time? [closed]
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 ...
30
votes
20
answers
84k
views
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 ...
27
votes
11
answers
74k
views
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 ...
25
votes
1
answer
16k
views
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. ...
23
votes
2
answers
10k
views
PI constant is ambiguous
Consider following code:
fn main() {
let i = f32::consts::PI;
}
With following error:
$ rustc --version
rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
$ rustc -
<anon>:2:13: 2:28 ...
22
votes
2
answers
1k
views
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 ...
20
votes
3
answers
53k
views
Python pi calculation?
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 ...
20
votes
2
answers
2k
views
why in matlab sin(pi) is not exact but sin(pi/2) is exact?
I have a problem in calculation with matlab.
I know that "pi" is a floating number and is not exact. So, in matlab sin(pi) is not exactly zero.
My question is if "pi" is not exact then why sin(pi/2) ...
18
votes
3
answers
10k
views
Gauss-Legendre Algorithm in python
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 ...
17
votes
3
answers
58k
views
How to calculate wind direction from U and V wind components in R
I have U and V wind component data and I would like to calculate wind direction from these values in R.
I would like to end up with wind direction data on a scale of 0-360 degrees, with 0° or 360° ...
17
votes
7
answers
12k
views
How to print 1000 decimals places of pi value?
I would like to print e.g. 1000 or 2000 or 15000 decimals of pi value using R?
Now I get only six
> pi
[1] 3.141593
How to achieve this?
15
votes
1
answer
53k
views
What's the difference between "pi" and "M_PI" in objc
Including some math in my code I stumbled over the constant "PI".
At least in my Xcode version 4.6 I could use either one. But what is the difference between pi and M_PI? The documentation is little ...
15
votes
1
answer
8k
views
Why is there no definition of the constant pi in the C++11 standard? [closed]
I find it quiet annoying that I have to use the macro _USE_MATH_DEFINES in order to get the value of pi into my program. Or I need to define it myself in one of my own headers. Or I have to use boost ...
14
votes
1
answer
1k
views
What is the best rational approximation to pi using a 64-bit numerator and denominator?
What's the most accurate rational pair for Pi representable with two 64 bit integers? Feel free to include other int types if you'd like.
Here's what I came up with, but I'm sure it can get more ...
14
votes
3
answers
26k
views
How to get PI in tensorflow?
I could not find any information about mathemtical constants in the Tensorflow API
neither in Basic Math functions nor in Math Ops.
I was able to get it by
import math as m
pi = tf.constant(m.pi)
...
13
votes
2
answers
7k
views
Best platform independent pi constant?
I know that you can use:
#define _USE_MATH_DEFINES
and then:
M_PI
to get the constant pi. However, if I remember correctly (comments welcome) this is compiler/platform dependent. So, what would be ...
13
votes
1
answer
1k
views
Python large iterations number fail
I wrote simple monte-carlo π calculation program in Python, using multiprocessing module.
It works just fine, but when I pass 1E+10 iterations for each worker, some problem occur, and the result is ...
12
votes
2
answers
30k
views
python overwrite previous line
how do you overwrite the previous print in python 2.7?
I am making a simple program to calculate pi. here is the code:
o = 0
hpi = 1.0
i = 1
print "pi calculator"
acc= int(raw_input("enter accuracy:")...
11
votes
9
answers
54k
views
Print pi to a number of decimal places
One of the challenges on w3resources is to print pi to 'n' decimal places. Here is my code:
from math import pi
fraser = str(pi)
length_of_pi = []
number_of_places = raw_input("Enter the number of ...
11
votes
1
answer
8k
views
Calculating the nth digit of pi using the Bailey–Borwein–Plouffe (BBP) formula
How do I calculate the nth binary (or hexadecimal) digit of pi using the Bailey–Borwein–Plouffe formula? I have been thoroughly searching the Internet and this site for an answer, but I've still yet ...
10
votes
3
answers
5k
views
C# Math vs. XNA MathHelper
Ever since I needed to work with PI (3.1415...) in C# I have used Math.PI to get the value. Usually I would just use values like Math.PI/2.0 or 2.0*Math.PI, but now I have just noticed that XNA ...
10
votes
1
answer
1k
views
Are there number limitations in python?
I have made a Python 3 program to calculate pi for a school project, but it always stops at 16 decimal places. Is there a limit to the length of numbers in python? If so is there a language that I ...
10
votes
2
answers
4k
views
Calculating Pi in JavaScript using Gregory-Leibniz Series
I have to calculate value of Pi using Gregory-Leibniz series:
pi = 4 * ((1/1 - 1/3) + (1/5 - 1/7) + (1/9 - 1/11) + ...)
I want to write a function in JavaScript that would take the number of ...
9
votes
3
answers
18k
views
generating pi to nth digit java
I wanted to know how I can generate pi to the nth digit. I have a couple of basic ideas.
Use Math.PI and increase the precision (if that's possible)
Use Euler's formula to generate pi but even here, ...
8
votes
10
answers
2k
views
Pi/Infinite Numbers
I'm curious about infinite numbers in computing, in particular pi.
For a computer to render a circle it would have to understand pi. But how can it if it is infinite?
Am I looking too much into ...
8
votes
4
answers
19k
views
Javascript: PI (π) Calculator
Is there a way to calculate pi in Javascript? I know there you can use Math.PI to find pie like this:
var pie = Math.PI;
alert(pie); // output "3.141592653589793"
but this is not accurate. What I ...
8
votes
1
answer
8k
views
Why do M_PI_2, M_PI_4, M_1_PI, and M_2_PI exist? [closed]
I do understand why we have this in standard headers:
#define M_PI 3.14159265358979323846 // pi
However, I don't see much benefit in having these:
#define M_PI_2 1.57079632679489661923 ...
8
votes
1
answer
2k
views
pi deprecated on OS X 10.8
Is there a replacement for pi const on OS X 10.8?
When I use it I get the following warning:
'pi' is deprecated: first deprecated on Mac OS X 10.8
It works but I want to get rid of that warning.
8
votes
3
answers
10k
views
Using basic arithmetics for calculating Pi with arbitary precision
I am looking for a formula/algorithm to calculate PI~3.14 in a given precision.
The formula/algorithm must have only very basic arithmetic as
+: Addition
-: Subtraction
*: Multiplication
/: Divison
...
7
votes
3
answers
20k
views
Function to find the nth digit of Pi
I have always wanted to find an algorithm that did this. I do not care how slow it is, just as long as it can return the nth digit of Pi:
ex:
size_t piAt(long long int n)
{
}
Preferably, not using ...
7
votes
3
answers
2k
views
Cython's calculations are incorrect
I implemented the Madhava–Leibniz series to calculate pi in Python, and then in Cython to improve the speed. The Python version:
from __future__ import division
pi = 0
l = 1
x = True
while True:
...
7
votes
5
answers
3k
views
Monte Carlo Method of finding pi using C
I have written a function that takes in a long long value n and uses that as the number of iterations to go through. The function should give a good estimate of pi, however, all the values for large n ...
7
votes
3
answers
12k
views
Bailey–Borwein–Plouffe formula implementation in C++?
EDIT: The requirement was vague and instead of calculating the n-th digit of pi they just wanted pi to the n-th digit not going beyond floats limitation so the brute force way worked for the ...
7
votes
1
answer
253
views
Why doesn't my program approximate pi?
For yesterday's Pi Day, Matt Harper published a video in which he approximated Pi by rolling two 120-sided dice 500 times (see the video here). Basically, for each pair of random numbers, you have to ...
7
votes
2
answers
727
views
Can't accurately calculate pi on Python
I am new member here and I'm gonna drive straight into this as I've spent my whole Sunday trying to get my head around it.
I'm new to Python, having previously learned coding on C++ to a basic-...
7
votes
2
answers
7k
views
javascript - More accurate value of Pi? [duplicate]
I type Math.PI; in Chrome Console and this is returned:
3.141592653589793
Then I type Math.PI - 3.141592653589793; and 0 was returned.
Is there a way to get a more accurate value (such as 3....
7
votes
4
answers
15k
views
Prevent Rounding to Zero in Python
I have a program meant to approximate pi using the Chudnovsky Algorithm, but a term in my equation that is very small keeps being rounded to zero.
Here is the algorithm:
import math
from decimal ...
7
votes
1
answer
2k
views
Get mouse position relative to pie chart (equation)
I have created a canvas pie chart from an array of data, I am now trying to locate the mouse position relative to the pie chart to detect which section of data is being hovered. I am almost there but ...
7
votes
2
answers
2k
views
Required Working Precision for the BBP Algorithm?
I'm looking to compute the nth digit of Pi in a low-memory environment. As I don't have decimals available to me, this integer-only BBP algorithm in Python has been a great starting point. I only need ...
6
votes
8
answers
2k
views
Why does the area come back as 0?
Here's the code.
int a;
int pi = 3.14;
int area;
int main()
{
cout << "Input the radius of the circle ";
cin >> a;
a *= a *= pi >> area;
cout << "The area ...