CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: debugging PI

  1. #1
    Join Date
    Mar 2013
    Posts
    5

    debugging PI

    I am trying to figure out what is wrong with these code I kind of have the idea of what to do take the value of pi and verify if it is even add it or if is odd sustacted, however I am not sure about if I have to update the sum variable? and If if What I have done so far it is correct.


    I am trying to find the value of PI with the following serie Π = 4( 1 – 1/3 + 1/5 – 1/7 + 1/9 – 1/11 + … - 1/(2i -1) + 1/(2i + 1)

    assumeing that i = 1000000


    Code:
    import java.util.Scanner;
    	
       public class Pi{
          public static void main(String[] args)
          {
          
          
          int sum=0;
             for (int i=1 ; i< 1000000; i++)
             {
             
                int value  = 1/(2 * i-1);
             
                if (i%2==0)
                {
                
                   sum = sum + value;
                
                }
                else
                {
                   sum = sum-value; 
                }
             }
    			
    			
          }

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: debugging PI

    Don't you have a debugger? If so single step your code line by line while inspecting the result and you will see what's wrong.
    A hint on one issue: What is the integer part of 1 / n when n > 1?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured