I have been given a question to write a program to read a number x from the user and then
print the value of ln(1+x).

I have been given the following equation to perform the calculation:

1     2     3     4     5     6       100
     ln(1+x) =  x     x     x     x     x     x       x
               --- - --- + --- - --- + --- - --- ... ---
                1     2     3     4     5     6      100

but I did not know how to calculate the lawn x value. Can someone here kind enough to guide me through this????

Edited by vergil1983: n/a

6 Years
Discussion Span
Last Post by raptr_dflo

Ok, so what you'll start with is some value x, and you'll write a function that computes ln(1+x) by using the formula above:

double ln_one_plus_x(double x)
    double val;
    // insert math here
    return val;

Now you want to use exponents from 1 to 100 inclusive, so a loop is in order.
Each term will be added (if the exponent is odd) or subtracted (if the exponent is even) to/from the total. And instead of computing the powers-of-x for each term, you can notice that x^57 = (x^56 * x). And then the power-of-x for each term must also be divided by the power. Is that enough to get you started? If you need to test your result, try http://www.cplusplus.com/reference/clibrary/cmath/log/ (and note that the formula given is valid when |x| < 1, otherwise the powers-of-x grow instead of shrink and the formula diverges from any solution).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.