pi

generating pi to nth digit java

倾然丶 夕夏残阳落幕 提交于 2019-11-28 00:25:46
问题 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, I would need to increase the precision (I think) There is also Srinivasa Ramanujan's formula for generating PI which is known for it's rapid convergence. This formula seems difficult to implement. I believe, I would have to also increase deicmal precision here. So in short, either way, I would

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

徘徊边缘 提交于 2019-11-27 18:56:25
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? This style ensures that the maximum precision available on ANY architecture is used when assigning a value to PI. 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 calculate the result for you guarantees that neither of those downsides happen. These are equivalent and you'll

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

ε祈祈猫儿з 提交于 2019-11-27 18:26:07
In a project using SciPy and NumPy, should I use scipy.pi , numpy.pi , or math.pi ? >>> 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. One thing to note is that not all libraries will use the same meaning for pi, of course, so it never hurts to know what you're using. For

Strange behaviour of macros C/C++

久未见 提交于 2019-11-27 16:19:10
I'm using some macros, and observing some strange behaviour. I've defined PI as a constant, and then used it in macros to convert degrees to radians and radians to degrees. Degrees to radians works fine, but radians to degrees does not: piTest.cpp: #include <cmath> #include <iostream> using namespace std; #define PI atan(1) * 4 #define radians(deg) deg * PI / 180 #define degrees(rad) rad * 180 / PI int main() { cout << "pi: " << PI << endl; cout << "PI, in degrees: " << degrees(PI) << endl; cout << "45 degrees, in rad: " << radians(45) << endl; cout << "PI * 180 / PI: " << (PI * 180 / PI) <<

How to printf long long

为君一笑 提交于 2019-11-27 09:10:59
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); } } karadoc %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 is: %I64d So try this: if(e%n==0)printf("%15I64d -> %1.16I64d\n",e, 4*pi); and scanf("%I64d", &n);

I am attempting to print only a selected amount of Pi, it returns with an error of "Decimal has no attribute: __getitem__

主宰稳场 提交于 2019-11-27 08:54:23
问题 def pi(): prompt=">>> " print "\nWARNING: Pi may take some time to be calculated and may not always be correct beyond 100 digits." print "\nShow Pi to what digit?" n=raw_input(prompt) from decimal import Decimal, localcontext with localcontext() as ctx: ctx.prec = 10000 pi = Decimal(0) for k in range(350): pi += (Decimal(4)/(Decimal(8)*k+1) - Decimal(2)/(Decimal(8)*k+4) - Decimal(1)/(Decimal(8)*k+5) - Decimal(1)/(Decimal(8)*k+6)) / Decimal(16)**k print pi[:int(n)] pi() Traceback (most recent

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

亡梦爱人 提交于 2019-11-27 05:43:09
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? Mysticial Since I'm the current world record holder for the most digits of pi, I'll add my two cents : Unless you're actually setting a new world record, the common practice

How do I calculate PI in C#?

百般思念 提交于 2019-11-27 05:13:12
问题 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 a learning point of view. 回答1: If you want recursion: PI = 2 * (1 + 1/3 * (1 + 2/5 * (1 + 3/7 * (...)))) This would become, after some rewriting: PI = 2 * F(1); with F(i): double F (int i) { return 1 + i / (2.0 * i + 1) * F(i + 1); } Isaac Newton

1000 digits of pi in python

最后都变了- 提交于 2019-11-27 04:24:31
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 = [] for i in range(1000): my_array.append(str("hello, I'm an element in an array \n" )) big_string = ""

Gauss-Legendre Algorithm in python

折月煮酒 提交于 2019-11-27 02:04:45
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) t = 1/4 x = 1 for i in range(1000): y = a a = (a+b)/2 b = math.sqrt(b*y) t = t - x * square((y-a))