Results 1 to 6 of 6
  1. #1
    camper2 is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default pi and the wallis' product

    I'm having trouble getting to pi (3.14159)using the wallis' product (2*2/3*4/3 etc.) I think there is something wrong with the division but I'm not sure how to fix it. Anyway here is what I have. Any help would be appreciated. Thanks!

    Java Code:
    public static void main(String [] args)
        {
            int nextDenominator = 1;
            int nextNumerator = 2;
            int termCount = 1;
            float half_pi = 1.0f;
            
            while(termCount < 1000000) {
                half_pi *= nextNumerator / nextDenominator;
                nextDenominator += 2;
                half_pi *= nextNumerator / nextDenominator;
                nextNumerator += 2;
                
                if (half_pi * 2 == 3.14159)
                 break;
                else
                 termCount++;
            }
            System.out.println("half_pi = " + (half_pi * 2));
            System.out.println("termCount = " + termCount);
        }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I think there is something wrong with the division
    The best way to check is to put in debug statements and verify whether the code is doing what you want.

    / performs integer division when it has two integer arguments - and I don't think this is what you want here.

    It usual to use double as your floating point type, rather than float.

  3. #3
    camper2 is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    I removed the integers and changed termCount and half_pi to doubles. However I still get half_pi = 2.0. This tells me that half_pi is equal to 1.0 until termCount reaches 1000000 then it multiplies half_pi by 2.

    Java Code:
    public static void main(String[] args)
        {
            double termCount = 2.0; 
            double half_pi = 1.0;
            
            while(termCount < 1000000) {
                half_pi *= termCount / termCount--; 
                half_pi *= termCount / termCount++;
              if (half_pi * 2.0 == 3.14159)
               break;
              else
               termCount += 2;
           }
           System.out.println("termCount = " + termCount);
           half_pi *= 2.0;
           System.out.println("half_pi = " + half_pi);
    
    }

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    whay don't just use Math lib here?

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    do you know 5 / 7 is 0
    5 / 7.0 is 0.714....

    for nextDenominator and nextNumerator, please declare them as double or float

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Java Code:
    half_pi *= termCount / termCount--; 
    half_pi *= termCount / termCount++;
    These lines may not be doing what you think. (Well, clearly they're not or you wouldn't have a problem ... .;-)

    Try this:

    Java Code:
    public static void main(String args[]) {
        double foo = 10;
        System.out.print(foo / foo--);
    }
    Consult your textbook or whatever to see what the -- operator does.

    One way to fix this would be you use more variables, eg explicit numerator and denominator variables. This would enable you to print out and see exactly what is being divided by what.

Similar Threads

  1. Replies: 1
    Last Post: 03-01-2009, 06:26 PM
  2. Finished Product: What now?
    By Unome in forum Java Applets
    Replies: 5
    Last Post: 02-11-2009, 11:41 AM
  3. Product update service for Java application.
    By Eranga in forum Advanced Java
    Replies: 4
    Last Post: 04-22-2008, 10:24 AM
  4. sum,product,average program
    By chitwood in forum New To Java
    Replies: 7
    Last Post: 01-24-2008, 06:18 AM
  5. Replies: 0
    Last Post: 10-29-2007, 06:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •