Calculate pi

Solved
CyRux Posted messages 164 Registration date   Status Member Last intervention   -  
CyRux Posted messages 164 Registration date   Status Member Last intervention   -
Hello everyone,
I would like to create a program that calculates pi ... so I based it on this algorithm: ? = (4/1) - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + (4/13) - (4/15)...
and I created this code
#include <stdio.h> int main () { int b = 1; float pi = 4 / b; int i; for (i=0; i < 10; i++) { b = b + 2; pi = pi - 4 / b; printf ("%f", pi); b += 2; pi = pi + 4 / b; printf ("%f", pi); } return 0; } 

but it gives me "3.00000 3.00000 3.00000 3.00000 3.00000 3.00000 3.00000 3.00000 3.00000 3.00000"
Thank you in advance

ps: I'm a beginner in C

1 answer

fiddy Posted messages 441 Registration date   Status Contributor Last intervention   1 847
 
Hello,

First of all, I advise you to use doubles instead of floats.

b = b + 2; and b+=2;
You might as well use the same instruction for consistency ;-).

And by the way, the error: pi = pi - 4 / b;
4/b is a division of two integers. In C, such a division returns the integer part (so 0). Pi will thus be 4-1-0-0...=3.
Instead, use: pi=pi-4.f/b; (if you are working in float) or pi=pi-4./b; (if you switched to double).
Don't forget to change it for: pi=pi+4/b;

Regards,

Google is your friend.
0
CyRux Posted messages 164 Registration date   Status Member Last intervention   3
 
Thank you very much, it works, it works. I've stayed in float because it didn't work in double (gcc was reporting an error).
The only problem is that my result has only 6 digits after the decimal point...
So I thought about multiplying it by 10000000000000[...]000, but I don't know how to make my result accurate...
0
fiddy Posted messages 441 Registration date   Status Contributor Last intervention   1 847
 
If gcc reports an error, it means there is something to correct ;-).
Don't hesitate to post your code along with the double version and the errors reported by gcc so that we can see where the problem comes from.

The only problem is that my result has only 6 digits after the decimal point...
No, the result actually has many more digits after the decimal point. But you are asking for the display of only 6 digits after the decimal point (by default).
If you want 10: printf("%.10f\n", pi);
Note: don't forget to include a separator for better readability.

Best regards,
0
CyRux Posted messages 164 Registration date   Status Member Last intervention   3 > fiddy Posted messages 441 Registration date   Status Contributor Last intervention  
 
It's good, it works ^^ Thank you very much
just for your information, here is the final code:
#include <stdio.h>

int main ()
{
int b = 1;
double pi = 4 / b;
int i;
for (i=0; i < 1000000; i++)
{
b += 2;
pi = pi - 4./b;
printf ("%.200f\n", pi);
b += 2;
pi = pi + 4./b;
printf ("%.200f\n", pi);
}
return 0;
}

but the only problem is that the algorithm I used is not very good... it takes 1,000,000 calculations to get 5 correct decimals...

ps: Sorry for the delay in my response, but I wasn't at home...
0