# need some help with pi() func!!!!!!

Printable View

• 01-04-2012
oron_e
need some help with pi() func!!!!!!
i need to build a basic program that print the num pi....
output: 3.0000000000
what is wrong??
Code:

```# include <stdio.h> double long exp1(double base, unsigned long exp) {     double sum=1;     unsigned long i;     for (i=0; i<exp; i++)     {         sum=sum*base;     }     return sum; } double long pi(int k) {     double long sum=0;     if (k==0)     {         sum=(47/15);         return sum;     }     sum=((1/(exp1(16,k)))*((4/((8*k)+1))-(2/((8*k)+4))-(1/((8*k)+5))-(1/((8*k)+6)))) + pi(k-1);     return sum; } int main() {     int k=0;     printf("please enter how many k of pi do you like\n");     scanf_s("%d",&k);         if (k<0)     {         while (k<0)         {             printf("please enter positive number\n");             scanf_s("%d",&k);         }     }     printf("the number is %.10lf\n",pi(k));     return 0; }```
• 01-04-2012
jimblumberg
The biggest problem I see is all the integer math. Remember when using integer math there are no fractions. So sum=(47/15); would evaluate to 3 not 3.1333333, 1/2 would evaluate to zero not .5.

Jim
• 01-04-2012
cauberong09
When using integer fraction will be truncated.
Solution:add .0 extentions for integer(example:47.0/15) or type casting it(example:sum=(float(47),15)).
Sorry my english is very bad!.
• 01-04-2012
gardhr
I realize that you're trying to compute it in a very specific way, but FWIW, a very simple and useful equivalence to remember is PI == 4 * atan(1). :)
• 01-05-2012
Caligulaminus
Your function declarations are wrong. You mean long double not double long.
Didn't your compiler complain about this?
• 01-07-2012
oron_e
thank's guys....