Hacker News new | ask | show | jobs
by ncruces 760 days ago
Now write the formula in terms of expm1 for numerical stability, and express the constant in terms of “half life” measured in seconds to make it intuitive instead of magical.
1 comments

Seriously guys, don't write:

  a = lerp(a, B, 1.0 - exp(-delta * RATE2))
Write:

  a = lerp(a, B, -expm1(-delta * RATE2))
This is precisely the situation where you really want to use expm1: https://www.johndcook.com/blog/cpp_expm1/

And (as pointed in the above) if you're worried about the slowness of it, just Taylor expand it to x+x²/2.

Finally, unless division is too costly, do:

  a = lerp(a, B, -expm1(delta / -T))
Where T is the “time constant” (which you can intuitively express in time units, like seconds): https://en.wikipedia.org/wiki/Exponential_smoothing#Time_con...

It's not the half-life (sorry) but it's still a lot more intuitive as a parameter.

That's some very good recommendations you made here. I'm adding all 3 at the end, thank you.