I try to follow the topic "Efficient implementation of natural logarithm (ln) and exponentiation" to be able to implement a log function without math.h. The described algorithm works well for values between 1 and 2 (normalized values). However, if the values are not normalized and I follow the normalization instructions, then I get wrong values.
Link: Click here
If I follow the code with an exemplary integer value of 12510, I get the following results:
y = 12510 (0x30DE), log2 = 13, divisor = 26, x = 481,1538
float ln(float y) {
int log2;
float divisor, x, result;
log2 = msb((int)y); // See: https://stackoverflow.com/a/4970859/6630230
divisor = (float)(1 << log2);
x = y / divisor; // normalized value between [1.0, 2.0]
result = -1.7417939 + (2.8212026 + (-1.4699568 + (0.44717955 - 0.056570851 * x) * x) * x) * x;
result += ((float)log2) * 0.69314718; // ln(2) = 0.69314718
return result;
}
The expected result for x should be a normalized value of 1 < x < 2. However, I fail in this calculation, because the received result is 481,1538.
Thanks in advance for any help