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

This is a discussion on need some help with pi() func!!!!!! within the C++ Programming forums, part of the General Programming Boards category; i need to build a basic program that print the num pi.... output: 3.0000000000 what is wrong?? Code: # include ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    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;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    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

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    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!.

  4. #4
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    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).

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    Your function declarations are wrong. You mean long double not double long.
    Didn't your compiler complain about this?

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    thank's guys....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cos func.
    By omGeeK in forum C Programming
    Replies: 20
    Last Post: 10-17-2010, 11:12 AM
  2. When does a func() become too large?
    By dudeomanodude in forum C++ Programming
    Replies: 22
    Last Post: 02-05-2008, 09:20 AM
  3. global func
    By samsam1 in forum Windows Programming
    Replies: 1
    Last Post: 01-14-2003, 05:21 PM
  4. typedef a func
    By trekker in forum C Programming
    Replies: 4
    Last Post: 07-02-2002, 05:15 AM
  5. ...my line func...
    By StormySpike in forum C++ Programming
    Replies: 5
    Last Post: 09-28-2001, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21