can comment about this and help to check the error. its related to my previous post.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zewl
    New Member
    • Oct 2009
    • 9

    can comment about this and help to check the error. its related to my previous post.

    The following series summation is an approximation for computing the natural logarithm of a floatingpoint
    value x:

    logx=[(x-1)]–[[(x-1)^2]/2]+[[(x-1)^3]/3]–[[(x-1)^4]/4]+….+(-1)n+1(x-1)^n/n

    Where 2 >= x > 0 and n is a positive integer.
    Design and implement an interactive program that does the following:
    1. Prompts the user to enter a floating-point value for x and reads it.
    2. Ensures that x is in the proper range; if not, re prompts the user to enter another value.

    i've done it like this...

    float x = 123.45F; // example value to find the log
    float x1 = (x - 1);
    for(int i = 1; i < n; i++)
    {
    float k = ( pow((x-1), i) / i );
    if( (i % 2) == 0)
    x1 -= k;
    else
    x1 += k;
    }

    i dont know how to edit the i.
    because if it like that it will become (x-1)-((x-1)^1)/1 where the result will zero.
  • zewl
    New Member
    • Oct 2009
    • 9

    #2
    btw,wat should be the value of n?

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      What is 'to edit the i' ?
      With 123.45 it will never converge. Range [0..2] was in your task for a reason.
      N must be such that remainder of the sequence is less than precision of the result - for floats, if result is about 1, stop when current element is less than sum* (2**-24)
      And the most important - do not calculate (x-1)**i using pow function, hold the (x-1)**(i-1) somewhere and multiply it by (x-1) on each iteration.

      Comment

      • JonathanS
        New Member
        • Jan 2008
        • 37

        #4
        in [(x-1)^2] all over 2, the exponent and the denominator will not cancel (and not for any n).


        The value for N is a bit trickier. N is something that either your course instructor will specify or something you decide on based on how close you want your approximation to come to your answer. Think of the series as being "dominated" by the first term (x-1). So the log of 2 is going to be close to 1. The next terms are smaller and smaller refinements (in the positive and negative direction) to the actual logarithm (convince yourself why the series is only valid for 0<x<=2).

        So, to give you a concrete answer, you could decide you want to throw away terms that have an absolute values less than 0.0001 (or smaller). You could conceivably hedge your bets by having an actual value for log x in your program and checking how close you are to it.

        Hopefully that's what you needed but if you're interested (or need the info for the course) there are tons of sites on Taylor Series expansion.

        Edit: sry newb, your answer wasn't there when I started. Your float tolerance explanation makes a bit more sense unless the OP has other requirements.

        Comment

        Working...