10.11.2011, 17:46
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:
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;
}