Last active
December 17, 2015 18:18
-
-
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`)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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