Calculate pi
Solved
CyRux
Posted messages
164
Registration date
Status
Member
Last intervention
-
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
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
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
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.
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.
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...
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,
just for your information, here is the final code:
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...