# Implementing The Exponential Function Ask ordinary software developers how to code an exponential function (that is, ex) and most will tell you to simply write an expression in their favorite high level language. But a significant slice of Hackaday readers will program tiny machines down to the bare metal or need more speed or precision than available with a customary implementation. [Pseduorandom] knows quite a few ways to do the calculation, and while it isn’t light reading for the math-phobic, it is an interesting tour.

The paper covers a variety of ways to calculate the function ranging from various Taylor series approximations, Lagrange interpolation, and Chebyshev interpolation. The paper is somewhat abstract, but there are Python and C++ examples to help make it concrete.

The paper does cover a bit about why you might want to compute ex, but, honestly, we still love the Better Explained post about how it relates to any continually growing process. If you missed it, you can see the related video, below. We sure wish our math teachers had explained this to us.

We have to admit, if we had ever learned about some of these methods, we’ve forgotten them. But it is easier to get interested in this math when you aren’t having to cram it in right before a final exam.

We confess we are usually more interested in bit tweaking math these days. But we do occasionally open a program like Mathics.

## 16 thoughts on “Implementing The Exponential Function”

1. RW ver 0.0.3 says:

When I gotta start messing with logs and exponentials on a cramped system I’ll give it an analog co-processor and ADC the result back in :-D

1. Allan-H says:

I’ve actually done that in products.

Don’t forget the temperature sensor!

2. fizzymagic says:

Interesting. It’d be great if he would learn how to make graphs; the weird way he offsets zero on the y axis for values that *by definition* can never be <0 is quite unpleasant. It makes the graphs less clear, when it seems the intent was to make them clearer.

3. Ali Raheem says:

I need to dig out my PMFPU poor mans FPU. It ran on pic14 to do simple FP addition and implemented multiplication on top of that.

It’s main purpose was to allow statistical calculations, made use of Q-rsqrt algorithm modified to work with my hacky FP.

It was also ported to FPGA but I don’t think it ever worked well. Ill have to dig it up to confirm but I think it was modified from Pong Chu’a sample it was many years too late for it to be useful compared to the available FP capable units.

1. A little over 20 years ago, I implemented a full FP package for PIC16F74 (actually, I think back then it was the PIC16C74), which was part of a proprietary BASIC implementation. It had add, subtract, multiply, divide, log, exp, sin, cos, and arctan using IEEE-754 single-precision values. The trig functions were implemented using Chebyshev approximations, which I’d acquired from a magazine article some years earlier on implementing floating-point operations for x86.

4. bestLD says:

The Better Explained video is misleading, at best.

e^x , where x is real, is a continuous function, just as say, 2^x is. No more, no less. What is explained about 2^x is not correct. In fact for a positive real, A : A^x = e^(bx) , where b = ln(A) or e^x = A^(cx) , where c = logA(e) (logarithm in base A). All these exponential functions are the same, as it were, one can always be written as another.

What sets e^x appart is the fact that its rate of change is itself : d (e^x) /dx = e^x . (or d(A^x)/dx = b(A^x) , b as above)

There are much better, correct, explanations to be found. One by 3blue1brown (outstanding channel) :

https://youtu.be/4PDoT7jtxmw?t=1330

1. Ali Raheem says:

I’m not sure this is correct. I’ll have to watch the video before I can say confidently (but I am sure enough to comment before I watch the video!).

My problem is with e^x is continuous when power is defined (originally) only for integers.

The usual game is to use analytic continuation to define it else where. It’s a curious thing that If the function is defined over some dense point then there is a single analytic continuation.

But that’s not true of x^y where it was defined only at single points (the integers).

So it is not continuous and there is no single way to extend it to cover the complex numbers (analytic continuation applies to complex numbers). So that you could claim x^y is continuous.

There is more than one exponential function there are some good, some bad.

But the sum of two analytic functions is still analytic so there are infinite correct x^y

You don’t need analytic continuation for any of this. exp(x)=lim_{n->infty} (1+x/n)^n
You are probably thinking of how to boost powers and roots of rationals into real powers of reals via continuity. (Which is different than analytic continuation.)

2. GrahamM says:

+1 . e is special because in a function e^ something, the something (or component parts) are always directly proportional to the output of the function. For any other number than e, always becomes maybe (and a slim maybe).

5. Old Guy says:

“Ask ordinary software developers how to code an exponential function (that is, ex) and most will tell you to simply write an expression in their favorite high level language.”

Anyone worth paying will simply steal something someone else has done. Or use a table.

6. Tom says:

” The paper is someone abstract,” typo or intended?

1. Ren says:

B^)
Well, if a horse is a perfect sphere…

7. Comedicles says:

I would think one might do something clever with the fact that for evenly spaced x the ratios of consecutive pairs of e^x is a constant.

8. Drone says:

Mathematical approximations in numerical methods are like strolling in a mine field, you’ll never know when they will blow up. Use a LUT and curve fit between samples if need be (regression), otherwise just linearly interpolate.

1. Alex Rossie says:

It’s LUTs all the way down I tells ya!

9. Manuel says:

Made my autistic self cringe with e=2.7183… just at the beginning, but is a really interesting read.

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.