1 Star 0 Fork 3

王布衣/num

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
tanh.go 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2024-02-08 08:51 . first commit
package math32
// The original C code, the long comment, and the constants
// below were from http://netlib.sandia.gov/cephes/cmath/tanh.c,
// available from http://www.netlib.org/cephes/single.tgz.
// The go code is a simplified version of the original C.
// tanhf.c
//
// Hyperbolic tangent
//
//
//
// SYNOPSIS:
//
// float x, y, tanhf();
//
// y = tanhf( x );
//
//
//
// DESCRIPTION:
//
// Returns hyperbolic tangent of argument in the range MINLOG to
// MAXLOG.
//
// A polynomial approximation is used for |x| < 0.625.
// Otherwise,
//
// tanh(x) = sinh(x)/cosh(x) = 1 - 2/(exp(2x) + 1).
//
//
//
// ACCURACY:
//
// Relative error:
// arithmetic domain # trials peak rms
// IEEE -2,2 100000 1.3e-7 2.6e-8
//
//
/*
Cephes Math Library Release 2.2: June, 1992
Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier
Direct inquiries to 30 Frost Street, Cambridge, MA 02140
*/
/* Single precision hyperbolic tangent
* test interval: [-0.625, +0.625]
* trials: 10000
* peak relative error: 7.2e-8
* rms relative error: 2.6e-8
*/
func Tanh(x float32) float32 {
const MAXLOG = 88.02969187150841
z := Abs(x)
switch {
case z > 0.5*MAXLOG:
if x < 0 {
return -1
}
return 1
case z >= 0.625:
s := Exp(z + z)
z = 1 - 2/(s+1)
if x < 0 {
z = -z
}
default:
if x == 0 {
return x
}
s := x * x
z = ((((-5.70498872745e-3*s+2.06390887954e-2)*s-5.37397155531e-2)*s+1.33314422036e-1)*s-3.33332819422e-1)*s*x + x
}
return z
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/num.git
git@gitee.com:quant1x/num.git
quant1x
num
num
v0.2.4

搜索帮助