Approximating Hyperbolic Tangent
jtomschroeder
38 points
4 comments
April 22, 2026
Related Discussions
Found 5 related stories in 60.3ms across 5,335 title embeddings via pgvector HNSW
- Avoiding Trigonometry (2013) WithinReason · 210 pts · March 12, 2026 · 43% similar
- Not all elementary functions can be expressed with exp-minus-log mmastrac · 43 pts · April 15, 2026 · 40% similar
- Notes on Lagrange Interpolating Polynomials ibobev · 38 pts · March 02, 2026 · 37% similar
- Show HN: I made a calculator that works over disjoint sets of intervals fouronnes3 · 51 pts · April 18, 2026 · 37% similar
- The 185-Microsecond Type Hint kianN · 58 pts · March 02, 2026 · 35% similar
Discussion Highlights (4 comments)
mjcohen
Looks interesting. Should start with a definition of the Hyperbolic Tangent. It is only about 2/3 of the way that the definition occurs in a discussion of computing exp(x).
agalunar
There’s an analysis of the Schraudolph approximation of the exponential function (along with an improvement upon it) that someone might find interesting at https://typ.dev/attention#affine-cast
raphlinus
A different approach, refining the square root based sigmoid with a polynomial, is in my blog post "a few of my favorite sigmoids" [1]. I'm not sure which is faster without benchmarking, but I'm pretty sure its worst case error is better than any of the fast approximations. [1]: https://raphlinus.github.io/audio/2018/09/05/sigmoid.html
AlotOfReading
A different floating point hack makes exp() easier to compute in hardware (and consequently tanh). You cast the input to an int and take the first 2 bits of what would be the mantissa. LUT[Index] and LUT[Index+1] from your 5-entry table are used to either lerp or poly approx. the function, with the remaining mantissa bits to help.