Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: could you tell me whats wrong....

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default could you tell me whats wrong....

    double denom = 3;
    double pi;
    double x = -1;
    int i;

    pi = 4*(1-((double)1/denom));

    for (i=1 ; i<=30000 ; i++){
    x = x*-1;
    denom = (double)x*(denom+2);
    pi = pi + denom;



    System.out.println(i + " " + denom);


    this code supposed to print denom at an alternating operation sequence in increasing value by 2.. but it only prints


    5, -7 ,5 ,-3 , 5, -7 , 5,-3 over and over. helP!


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: could you tell me whats wrong....

    double denom = 3;
    double pi;
    double x = -1;
    int i;



    for (i=1 ; i<=30000 ; i++){
    pi = 4*(1-((double)1/denom));
    x = x*-1;



    if (i % 1000 == 0){

    System.out.println(i + " " + pi);
    denom = x*((double)1/(denom+2));
    pi = pi + denom;

    }
    }



    }
    }


    narrowed it down to this but still its not working

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: could you tell me whats wrong....

    I believe the problem is that switching the sign of denom makes it so when you ADD two each time, you are not always adding to the absolute value of the number, as adding to a negative gets it closer to 0

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: could you tell me whats wrong....

    yeah nevermind i got it already thank you anyways....

Similar Threads

  1. Whats wrong with my looping?
    By argie123sky in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 21st, 2010, 08:13 PM
  2. whats wrong with my program??
    By jrodriguo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2010, 06:16 AM
  3. whats wrong with this one....
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 6th, 2009, 10:08 AM
  4. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM
  5. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM