As I noted in a comment, OCaml's float are boxed, which puts OCaml to a disadvantage compared to Clang. However, I may be noticing another typical rough edge trying OCaml after Haskell: if I see what your program is doing, you are creating a list of stuff, to then map...

The 4/j is entirely integer math, which means it will round down to an integer before being added. Some options are 4.0 / j and (double)4 / j. The MSDN has an article on C floating-point constants that explains the notation you should use to ensure a literal value is...

You use getcontext only for Decimal objects, but when you use traditional math functions you lose precision. Just floating points of 64 bits gives 15 - 17 significant decimal digits precision. Maybe, you can try with sympy module for more precision over symbolic expresions for your approximation.

There are only 10,000 possible sequences of four digits (0000 to 9999), so at some point you will have found that every sequence has been duplicated, and there's no need to process further digits. If you assume that pi is a perfectly uniform random number generator, then each new digit...

python,numpy,vectorization,montecarlo,pi

You are invoking the python builtin sum, rather than numpy's vectorized method sum: import numpy as np import random as rd def np_pi(n): x = np.random.random(n) y = np.random.random(n) return (x*x + y*y <= 1).sum() def dart_board(): x,y = rd.random(), rd.random() return (x*x + y*y <= 1) def pi(n): s...

try this: it will also get rid of the decimal. public static int[] MakePie(int n) { double pi = Math.PI; var str = pi.ToString().Remove(1, 1); var chararray = str.ToCharArray(); var numbers = new int[n]; for (int i = 0; i < n; i++) { numbers[i] = int.Parse(chararray[i].ToString()); } return numbers;...

python,python-2.7,syntax-error,pi

It has to be y += 4 * ((-1)**(n - 1) / ((2 * n) + 1)) The last ) is missed in line 5: which is y += 4 * ((-1)**(n - 1) / ((2 * n) + 1) ...

So far its working till digit 32, and then, an error occurs. Digit 31 is wrong too, it should be 5 not a 4, and digit 32 should be a 0. When you get a 10 digit you need to carry over 1 to the previous digit and change...

The bottleneck here is actually your for loop. Python for loops are relatively slow, so if you need to iterate over a million items, you can gain a lot of speed by avoiding them altogether. In this case, it's quite easy. Instead of this: for item in range(n): if ((s1[item])**2...

python,algorithm,pi,arbitrary-precision

By default, python will use the standard floating points as defined by IEEE-754. This has a precision of some 12 digits and can represent numbers as lows as 2-1022, now you can resolve this problem by calling the mpf operator earlier in the process, a more readable and more precise...

Change your code replacing all n to i in the for loop: #include <iostream> #include <cmath> using namespace std; int main() { long double n; cin >> n; long double first_part = 0.0, second_part = 0.0, pi = 0.0; for(int i = 0; i <= n; i++) { first_part +=...

The only way to do this that I am aware of is to write the output to a file. The modified code below opens a file pi_out.txt, writes the first hundred digits of pi to it and then closes the file at the end. import sys def calcPi(): q, r,...

Here is an article about calculating pi to 1 million digit in C#, you can narrow it down to 36 digit here is a sample code, I suggest reading the full article and the logic behind it. public static HighPrecision GetPi(int digits) { HighPrecision.Precision = digits; HighPrecision first = 4...

It seems you are losing precision in this line: pi = pi * Decimal(12)/Decimal(640320**(1.5)) Try using: pi = pi * Decimal(12)/Decimal(640320**Decimal(1.5)) This happens because even though Python can handle arbitrary scale integers, it doesn't do so well with floats. Bonus A single line implementation using another algorithm (the BBP formula):...

The constants embedded in the Boost headers have a limited precision, and you've hit that limit. If you need a higher-precision version of pi (for instance), you'll need to bring it yourself. For instance, the definition of boost::math::constants::pi in the headers is: BOOST_DEFINE_MATH_CONSTANT(pi, 3.141592653589793238462643383279502884e+00, "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651e+00") —...

No need to loop, just compute the point between the center of the circle and the mouse that is on the circle. var dx = x - 200, dy = y - 200, dist = Math.sqrt(dx*dx + dy*dy), newX = 200 + dx * radius / dist, newY = 200...

Yes it is a problem with your python version. Add an external pair of parenthesis: print( ('{0:.%df}' % min(30, int(digits))).format(4 * (4 * atan(1.0/5.0) - atan(1.0/239.0))) ) This is because in python3 print is a function and that expression is parsed as: (print('{0:.%df}' % min(30, int(digits)))).format(...) In other words print...

I am not absolutely sure if I got this right. I have something like this on my mind: Assume that we have the whole arraypi is in a browser You use the key combination ctrl+f for find Start typing the contents of arraye letter by letter until you see the...

'Allow Math.PI as a reference to the PI constant' means that your code will have to look like this in order to work: static double getCircumference(double radius ) { return Math.PI * 2 * radius; } public static double getArea(double radius) { return Math.PI * radius * radius; } What...

You're computing pi as a double and then converting it to BigDecimal. That won't magically add more precision than was in the double, which by definition can't be more than 53 bits. You need to do the computation of pi in BigDecimal....

This is what you're computing: 4*(1-1/3+1/5-1/5+1/7-1/7+1/9...) You can fix it just by adding a k += 2 at the end of your loop: def piApprox(num): pi=4.0 k=1.0 est=1.0 while 1<num: k+=2 est=est-(1/k)+1/(k+2) num=num-1 k+=2 return pi*est Also the way you're counting your iterations is wrong since you're adding two elements...

string,random,compression,pi,xz

It's a matter of information density. Compression is about removing redundant information. In the string "314159", each character occupies 8 bits, and can therefore have any of 28 or 256 distinct values, but only 10 of those values are actually used. Even a painfully naive compression scheme could represent the...

java,python,algorithm,variable-assignment,pi

In Python integers can be arbitrarily large. In Java a long consists of 64 bits and can therefore only store numbers smaller than about 2**64 / 2. If a number is too big, the first bits of it are discarded and the most significant bit that is not overwrites the...

To solve the issue, add use std::f32 or use std::f32::consts::PI, so that the compiler knows we're talking about the module f32 here, not the type f32.

There's more precision in that number than you're displaying. You just have to ask for it: cout << std::setprecision(40) << pi << endl; That gives me 3.141592653543740176758092275122180581093 when run on Codepad. As double should have way more than enough precision for basic calculations. If you need to compute millions of...

I found this code on this website: <html> <head> <title>Pi</title> <script type="text/javascript"> mess = ""; Base = Math.pow(10, 11); cellSize = Math.floor(Math.log(Base) / Math.LN10); a = Number.MAX_VALUE; MaxDiv = Math.floor(Math.sqrt(a)); function makeArray(n, aX, Integer) { var i = 0; for (i = 1; i < n; i++) aX[i] = null;...

1) Remove the single white space before the opening php tag. 2) There is no need to create "$theGoodPi", just use theTestPi, its just copying it anyway. It makes it take longer for nothing. 3)$theTestPi = file_get_contents("http://www.hithlonde.com/pi/pi"); You may not need to change 3 4)imagesetpixel($gd, $theX, $theY, NumToColor($gd, $theTestPi[$PiWalker])); Change...