How to calculate natural logarithm
#2

Getting an explicit formula for the logarithm is impossible, as it is a transcendent function. This is why a lot of systems just calculate it by iterative measurements, like the root function. For this you would set some initial value x, then calculate e^x to see if the result is greater than the number you want to get the ln of, and add or sub a tibny bit to x according to the result. And this would be repeated until the difference is as small as you like. Of course compared to an explicit formula this takes AGES.

Edit: found a series in my notes that seems to converge quite fast. Its also mentioned on wikipedia:


As your computer got limited precision anyways you dont need to care about the R(x) part. Tested it in pawn and it needs max k=6 iterations to get a float with max precision. Here's my code, hope this helps:

pawn Код:
stock Float:guessLN(Float:x) {
    new Float:result = 0.0;
    for (new i = 0; i < 10; i++) { // can be optimized with (while (changed))
        result += (2.0 / (2.0 * i + 1.0)) * floatpower(((x - 1.0) / (x + 1.0)), 2.0 * i + 1.0);
        //printf("result: %f", result);
    }
    return result;
}
Reply


Messages In This Thread
How to calculate natural logarithm - by [OFK]Zuckerman - 10.11.2011, 16:51
Re: How to calculate natural logarithm - by Mauzen - 10.11.2011, 17:46
Re: How to calculate natural logarithm - by [OFK]Zuckerman - 11.11.2011, 12:40
Re: How to calculate natural logarithm - by Mauzen - 11.11.2011, 12:55
Re: How to calculate natural logarithm - by [OFK]Zuckerman - 11.11.2011, 13:14

Forum Jump:


Users browsing this thread: 1 Guest(s)