sum calculate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sedaw
    New Member
    • Jan 2009
    • 36

    sum calculate

    need to calculte the sum of SIGMA(-1)^n*(1/(2n+1)) from n=0 to n=N

    Code:
    	for (i=0; i<N; i++)
    	{
    		X1=(-1)*(1/(1+2*i));
    	    X=X-X1;
    	}
    
    	printf("%lf", X);

    how to do the trick for + - + - + ........ ?

    TNX .......
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Use an "if" inside the loop that tests i % 2. Use the true part of the "if" for your + and the "else" for your -.

    Comment

    • sedaw
      New Member
      • Jan 2009
      • 36

      #3
      still have a problem the program printin " 1 ".

      Code:
      #include <stdio.h>
      void main()
      {
      	double X0,X=0 ;
      	int i ;
      	for (i=0; i<5; i++)
      	{
      		X0= 1/(1+2*i) ;
      		if (i%2!=0)
      		{X0=-X0;}
      		X=X+X0;
      	}
      
      	printf("%lf", X);
      	getchar();
      }
      whats wrong ?

      TNX ....

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        2 problems, the first trivial %lf is for the type long double not double. For double use %f. However there is a good chance that on your platform long double and double are the same size so that is probably not actually causing a visible problem.

        However

        X0= 1/(1+2*i);

        i is an integer and all the constants are integers so the calculation is done in integer arithmetic and then converted to a floating point (double).

        In integer arithmetic all fractions are dropped at every stage so for instance

        1/2 = 0

        the fraction 0.5 is dropped.

        In fact in general 1/n where n is an integer can have 3 values and 1 exception.

        If n == 0 then you get a divide by zero exception, you can't divide by zero.
        If n == 1 then 1/n = 1
        If n == -1 then 1/n = -1
        For any other n 1/n =0 because it is fractional and the fraction gets dropped.

        In you case 0 <= i < 5 resulting values of n of 1, 3, 5, 7, 9. So when i = 0 x0 = 1 and in all other cases x0 = 0 giving your result.

        The trick is not to do the arithmetic in integer arithmetic but to use floating point arithmetic. You can do this by making an one of the constants or variables used a floating point by either using a double constant (put on a decimal place) or by casting the variable.

        Comment

        • sedaw
          New Member
          • Jan 2009
          • 36

          #5
          you help me alot .
          thank you very much ! (-:

          Comment

          • Awaltii
            New Member
            • Jul 2009
            • 4

            #6
            Sorry to ask. I'm new in programming. Which language did you use, is it c++ or java

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Awaltii
              Sorry to ask. I'm new in programming. Which language did you use, is it c++ or java
              Look at the name of the forum where this was posted: C/C++ now guess again.

              kind regards,

              Jos

              Comment

              Working...