-2

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

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
Coder
  • 27
  • 7
  • 1
    `the basic math behind exp() and log()` is e^x and the natural logarithm of x. What do you actually want to know? – phuclv Feb 02 '22 at 15:20
  • exp and log are inverse functions. I don't know of a good way of implementing one in terms of the other. Since they're inverses, you could use a general-purpose inversion metafunction, such as [Newton's method](https://en.wikipedia.org/wiki/Newton%27s_method), but it wouldn't be a very efficient way of doing it. I believe that both of them are conventionally, and reasonably straightforwardly, calculated using [Taylor series](https://en.wikipedia.org/wiki/Taylor_series) expansions. – Steve Summit Feb 02 '22 at 15:26
  • But then , I should know pow() and I don't have power function implementation in fixed point C. You can suggest please other option - how to go about it . Thanks – Coder Feb 02 '22 at 15:27
  • I have successfully computed both of these using what I learned in the "Computation" sections of the Wikipedia articles on the [Logarithm](https://en.wikipedia.org/wiki/Logarithm#Power_series) and [Exponential function](https://en.wikipedia.org/wiki/Exponential_function#Computation), respectively. It's not easy at first — there are some significant hurdles to overcome, if you want your functions to work well, and be accurate, over their entire input range — but it's an interesting and instructive challenge. – Steve Summit Feb 02 '22 at 15:31
  • can you please share a little easy and numeric way of doing, I have fixed point log() available. – Coder Feb 02 '22 at 15:49
  • The `log()` function you have isn't going to help you. As I suggested earlier, start with [the Wikipedia article](https://en.wikipedia.org/wiki/Exponential_function#Computation) (or maybe [this one](https://en.wikipedia.org/wiki/Taylor_series#Exponential_function)). But, no, there is not a "little, easy" way of doing this: it's real work. – Steve Summit Feb 02 '22 at 16:13
  • 1
    The question seems to be a potential duplicate of various existing questions, e.g. [this](https://stackoverflow.com/questions/64941736/efficient-implementation-of-fixed-point-power-pow-function-with-argument-const/64978770#64978770), [this](https://stackoverflow.com/questions/4657468/fast-fixed-point-pow-log-exp-and-sqrt), and [this](https://stackoverflow.com/questions/53736820/fixed-point-approximation-of-2x-with-input-range-of-s5-26/53740645#53740645) – njuffa Feb 04 '22 at 03:11
  • @Coder I am not a downvoter, nor do I speak for the downvoters. Please note that before posting a question, askers on this site are expected to search for relevant *existing* questions whose answers may already provide the desired information. The purpose of this is to avoid redundant questions. In my previous comment, I linked three potential duplicate questions that appear relevant to the question asked here. Is the information you see there sufficient for your use case? If not, please clarify your question to specify how the information you seek differs from that found in existing contents. – njuffa Feb 04 '22 at 07:22
  • @Coder I can't explain the downvotes, either, but I can say that I'm perplexed. You've asked for help writing a fixed-point `exp()`, and you're received lots of hints and pointers. I'm not sure what you're still looking for, or why you posted [that follow-up question](https://stackoverflow.com/questions/70964009). If you're looking for someone to actually write the function for you, you're in the wrong place: that's not what Stack Overflow is for, and it looks like no one here is going to do that for you. – Steve Summit Feb 04 '22 at 12:59
  • @SteveSummit, Understood , I am bit non regular and new to stackoverflow so i am unaware of stuff. Many thanks for all the help and suggestion. I have to work my self and improve. May be should delete the followup question :-) – Coder Feb 04 '22 at 17:26
  • It appears that what you ask is more a mathematical question than a programming one, an illustration of a theoretical idea. The "express way" to define the "natural" logarithm and its inverse exponential, and what is "natural" about them, is to define `ln(x)=int(1 .. x, t => 1/t)`, prove its elementary properties and define the exponential as its inverse. All this is horribly inefficient from a numerical point-of-view, but might be used as a test for a faster implementation. – Lutz Lehmann Feb 09 '22 at 09:14
  • @LutzLehmann, Exactly the inverse derivation is what I am looking from numeric calculation. Thanks!! – Coder Feb 11 '22 at 05:59

2 Answers2

2

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.

hobbs
  • 223,387
  • 19
  • 210
  • 288
0

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

  • But basically power(base,exp) are the two entities which should be known , but the problem is missing power function . Any clue how to implement for all base and exponent? Unfortunately, you answer is not helpful for me . – Coder Feb 02 '22 at 15:46
  • So you are saying that you would like to implement a way to calculate the power and the logarithm of a number given the base? – SwirlyManager75 Feb 02 '22 at 16:27
  • I am of the opinion power() can do exponent () using logarithm. Since I don't have fixed point power() either I am looking other options as well. – Coder Feb 02 '22 at 22:55