Skip to content

Instantly share code, notes, and snippets.

@jimfleming
Last active December 17, 2015 18:18
Show Gist options
  • Save jimfleming/5651764 to your computer and use it in GitHub Desktop.
Save jimfleming/5651764 to your computer and use it in GitHub Desktop.
Approximate hyperbolic tangent function (-15x-, nevermind, post Go 1.1 only 2x faster than `math.tanh`)
func ApproxTanh(x float64) float64 {
if x > 21 {
return 1
}
if x < -21 {
return -1
}
if x == +0 {
return +0
}
if x == -0 {
return -0
}
return x * (x*x + 27) / (9*x*x + 27)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment