derive exponential function using log() in C

320 Views Asked by At

I am trying to derive exponential function using logarithms. I know from below equation log(22026.4657948067), is 10 and exp(10) is 22026.4657948067

I would like to understand the basic math behind exp() and log(). At the moment, I have log() in C and I would like to know the conversion logic so that I can calculate exponential() from log(). Thank you

2

There are 2 best solutions below

3
SwirlyManager75 On

The logarithms returns the exponent to which a base must be raised to yield a given number.

So if we have x=9, then (base 3) log(9) = 2 because 3^2 = 9.

In C the log() function uses base e where e is the Euler's number (2.71828).

So in your case what's happening is basically: log(22026.4657948067) = 10 and then e^10 = 22026.4657948067

0
hobbs On

There's no "conversion logic". They're both transcendental functions and there's no nice way to implement one in terms of the other. Since they're inverses, you could use Newton's method or some other root-finding method to find the zero of log(x) - a, and call that value exp(a), but that would be both less accurate and vastly less efficient than using the builtin implementation.

If you're asking how those functions are usually implemented in mathematical libraries, a common approach is range reduction followed by polynomial approximation. For instance, any positive number can be reduced into the range [1,2) by multiplying or dividing it by two zero or more times; and log(x * 2) == log(x) + log(2), so all we need is a high-precision constant for log(2), to be added or subtracted from the final result the appropriate number of times. Then we can use a Taylor series for the function log(1+x) over [0,1); you can get high accuracy without too many terms because of the limited domain. Alternatively, lookup tables and linear interpolation could be used on platforms where multiplication is particularly expensive.