You can always implement exp()
and log()
yourself.
And it's easier to actually implement 2x and log2x for the purpose and use in the same way as exp()
and log()
.
2x = 2integer_part(x)+fractional_part(x) = 2integer_part(x) * 2fractional_part(x)
2fractional_part(x) can be calculated for -1 <= x <= +1 using Taylor series expansion.
And then multiplying by 2integer_part(x) amounts to adjusting the exponent part of the floating point number by integer_part(x) or you can indeed raise 2 to the integer power of integer_part(x) and multiply by that.
Similarly, log2x = log2(x * 2N) - N
where N (an integer, a power of 2) is chosen such that 0.5 <= x * 2N <= 1 (or, alternatively, between 1 and 2).
After choosing N, again, we can use Taylor series expansion to calculate log2(x * 2N).
And that's all, just a little bit of math.
EDIT: It's also possible to use approximating polynomials instead of Taylor series, they are more efficient. Thanks Eric Postpischil for reminding. But you'd probably need a math reference to find or construct those.