Live data from Hacker News

Implementing the Exponential Function

pseudorandom.com

21–30 of 62 posts

Re: Implementing the Exponential Function

#21
post #16
post #12

Need to be careful about confusing smooth (infinitely differentiable) and analytic (= to Taylor series) functions. > Any function with this property can be uniquely represented as a Taylor series expansion “centered” at a It's fairly easy to construct tame looking functions where this isn't true.

Blessedly, in the complex analytic setting, these coincide, to they not? [Attempting to sanity-check my own understanding here]

In fact it is enough for functions to be complex differentiable once (in which case they'll automatically be differentiable infinitely often).

Re: Implementing the Exponential Function

#22
post #6

A couple other ways to compute the exponential function: The Intel 8087 coprocessor used CORDIC. The chip contained a ROM with the values of log2(1+2^-n) for various n. These constants allowed the exponential to be rapidly computed with shifts and adds. The Sinclair Scientific calculator was notable for cramming transcendental functions into a chip designed as a 4-function calculator, so they took some severe shortcu…

I was going to say there is a great article about the Sinclair Scientific here:

http://files.righto.com/calculator/sinclair_scientific_simul...

And then I noticed who I'm replying to...

Despite its flaws, this calculator was quite an achievement, doing everything it did with a ROM that holds only 320 instructions.

Re: Implementing the Exponential Function

#23
post #6

A couple other ways to compute the exponential function: The Intel 8087 coprocessor used CORDIC. The chip contained a ROM with the values of log2(1+2^-n) for various n. These constants allowed the exponential to be rapidly computed with shifts and adds. The Sinclair Scientific calculator was notable for cramming transcendental functions into a chip designed as a 4-function calculator, so they took some severe shortcu…

I was going to say there is a great article about the Sinclair Scientific here: http://files.righto.com/calculator/sinclair_scientific_simul... And then I noticed who I'm replying to... Despite its flaws, this calculator was quite an achievement, doing everything it did with a ROM that holds only 320 instructions.

Yes, I like that article :-)

Re: Implementing the Exponential Function

#25

I recently implemented the exponential function for a sound synthesizer toolkit I’m working on. The method I used was not mentioned in the article, so I’ll explain it here. I used the Remez algorithm, modified to minimize equivalent input error. This algorithm lets you find a polynomial with the smallest maximum error. You start with a set of X coordinates, and create a polynomial which oscillates up and down around…

I wouldn't recommend doing this directly -- there's no good polynomial that can represent the exponential function well over a wide range.

Instead, it's better to exploit the definition of IEEE754 as an exponent and a mantissa. You calculate the exponent directly (because e^x = 2^(x/ln2)), and use the Remez algorithm to find a polynomial that fits just the mantissa.

Re: Implementing the Exponential Function

#26
post #6

A couple other ways to compute the exponential function: The Intel 8087 coprocessor used CORDIC. The chip contained a ROM with the values of log2(1+2^-n) for various n. These constants allowed the exponential to be rapidly computed with shifts and adds. The Sinclair Scientific calculator was notable for cramming transcendental functions into a chip designed as a 4-function calculator, so they took some severe shortcu…

I was going to say there is a great article about the Sinclair Scientific here: http://files.righto.com/calculator/sinclair_scientific_simul... And then I noticed who I'm replying to... Despite its flaws, this calculator was quite an achievement, doing everything it did with a ROM that holds only 320 instructions.

That calculator is a hack in the most positive and brilliant sense of the word. Crazy constraints required crazy solutions. Amazing!

Re: Implementing the Exponential Function

#27
post #19
post #14

Ugh I had to implement all the transcendental functions a while ago. It was miserable, especially dealing with intermediate rounding, etc I don’t recommend it. That said I liked that there’s a specific exp-1 function (otherwise you drop most precision).

To go with that, you need ln(1+x). HP calculators had both.

As has C (since C99)

http://www.cplusplus.com/reference/cmath/log1p/

Re: Implementing the Exponential Function

#28
post #24

IMHO it's a bit cheeky to use exp2 from the standard library. It's likely got all the same math exp does. Fortunately, if you know that your exponent is an integer, there's a constant time version. It's 5 instructions: add, and, shift, mov, ret. https://godbolt.org/z/KNyoVd

Excellent, thank you! I agree, I had the use of `exp2(x)` on my list of implementation nits to revisit. I'm going to revise to use your method and credit the comment.

Re: Implementing the Exponential Function

#29
Few miscellaneous meta nitpicks:

* The article looks beautiful, but am I the only one that hates left-centered content? Wide monitors are the norm these days, and reading left centered content literally hurts my neck (need to resize manually to center the content, approximately.

* Why would someone make the effort to write such detailed analysis and conceal their identity? I just don't get what's the objective of not providing any sort of provenance... just a gripe of mine.

Post reply on HN