aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/math/tanh.v
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/vlib/math/tanh.v
downloadcli-tools-windows-master.tar.gz
cli-tools-windows-master.tar.bz2
cli-tools-windows-master.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/vlib/math/tanh.v')
-rw-r--r--v_windows/v/vlib/math/tanh.v45
1 files changed, 45 insertions, 0 deletions
diff --git a/v_windows/v/vlib/math/tanh.v b/v_windows/v/vlib/math/tanh.v
new file mode 100644
index 0000000..ac9590e
--- /dev/null
+++ b/v_windows/v/vlib/math/tanh.v
@@ -0,0 +1,45 @@
+module math
+
+const (
+ tanh_p = [
+ -9.64399179425052238628e-1,
+ -9.92877231001918586564e+1,
+ -1.61468768441708447952e+3,
+ ]
+ tanh_q = [
+ 1.12811678491632931402e+2,
+ 2.23548839060100448583e+3,
+ 4.84406305325125486048e+3,
+ ]
+)
+
+// tanh returns the hyperbolic tangent of x.
+//
+// special cases are:
+// tanh(±0) = ±0
+// tanh(±inf) = ±1
+// tanh(nan) = nan
+pub fn tanh(x f64) f64 {
+ maxlog := 8.8029691931113054295988e+01 // log(2**127)
+ mut z := abs(x)
+ if z > 0.5 * maxlog {
+ if x < 0 {
+ return f64(-1)
+ }
+ return 1.0
+ } else if z >= 0.625 {
+ s := exp(2.0 * z)
+ z = 1.0 - 2.0 / (s + 1.0)
+ if x < 0 {
+ z = -z
+ }
+ } else {
+ if x == 0 {
+ return x
+ }
+ s := x * x
+ z = x + x * s * ((math.tanh_p[0] * s + math.tanh_p[1]) * s + math.tanh_p[2]) / (((s +
+ math.tanh_q[0]) * s + math.tanh_q[1]) * s + math.tanh_q[2])
+ }
+ return z
+}