08-25-2022, 04:41 PM
Your endless string formulas are too complicated and cumbersome. There I get headdache.
If one implement the formulas it is short and concise. I have now implemented a guide for C++ (running) in C. It's Chudnovsky's formula for pi, but it won't work in QB64. Does anyone see where the error is? - I suspect it has to do with "pi += ..." - maybe.
Chudnovskys Pi formula in C
That has to be! Unfortunately there is no explanation why.
Chudnovskys Pi formula in QB64
If one implement the formulas it is short and concise. I have now implemented a guide for C++ (running) in C. It's Chudnovsky's formula for pi, but it won't work in QB64. Does anyone see where the error is? - I suspect it has to do with "pi += ..." - maybe.
Chudnovskys Pi formula in C
Code: (Select All)
//PI nach Cudnowsky - https://stackoverflow.com/questions/12028313/c-chudnovsky-formula-for-pi
//24. Aug. 2022, in C 25. Aug. 2022
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long double fac(double num);
int main(void)
{
long double pi = 0.0;
for (double k = 0.0; k < 10.0; k++)
{
pi += (pow(-1.0, k) * fac(6.0 * k) * (13591409.0 + (545140134.0 * k)))
/ (fac(3.0 * k) * pow(fac(k), 3.0) * pow(640320.0, 3.0 * k + 3.0/2.0));
}
pi *= 12.0;
__mingw_printf("%.12Lf\n\n", 1.0 / pi);
return 0;
}
long double fac(double num)
{
double result = 1.0;
for (double i = 2.0; i < num; i++)
{ result *= i; }
return result;
}
That has to be! Unfortunately there is no explanation why.
Code: (Select All)
__mingw_printf("%.12Lf\n\n", 1.0 / pi);
Chudnovskys Pi formula in QB64
Code: (Select All)
'Pi nach Chudnovsky berechnen - 25. Aug. 2022
Option _Explicit
Declare Function fac (num as Double) as Double
Dim As Double pi, k
pi = 0.0: k = 0.0
While k < 10.0
k = k + 1.0
pi = pi + (-1.0 ^ k) * fac(6.0 * k) * (13591409.0 + (545140134.0 * k)) / fac(3.0 * k) * fac(k) ^ 3.0 * 640320.0 ^ 3.0 * k + (3.0 / 2.0)
'pi = pi + pi
Wend
pi = pi * 12.0
Print
Print 1.0 / pi
End
Function fac (num As Double)
Dim As Double resultat, i
resultat = 1.0: i = 2.0
While i < num
i = i + i
resultat = resultat * i
Wend
fac = resultat
End Function