Index: src/third_party/fdlibm/fdlibm.js |
diff --git a/src/third_party/fdlibm/fdlibm.js b/src/third_party/fdlibm/fdlibm.js |
index d89f69b5f5900fe76adfeb69e25be89d84f641aa..983a86c151ac36e38f30610ef6f20570e82c9831 100644 |
--- a/src/third_party/fdlibm/fdlibm.js |
+++ b/src/third_party/fdlibm/fdlibm.js |
@@ -843,6 +843,63 @@ function MathCosh(x) { |
return INFINITY; |
} |
+// ES6 draft 09-27-13, section 20.2.2.33. |
+// Math.tanh(x) |
+// Method : |
+// x -x |
+// e - e |
+// 0. tanh(x) is defined to be ----------- |
+// x -x |
+// e + e |
+// 1. reduce x to non-negative by tanh(-x) = -tanh(x). |
+// 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x) |
+// -t |
+// 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x) |
+// t + 2 |
+// 2 |
+// 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t = expm1(2x) |
+// t + 2 |
+// 22.0 < x <= INF : tanh(x) := 1. |
+// |
+// Special cases: |
+// tanh(NaN) is NaN; |
+// only tanh(0) = 0 is exact for finite argument. |
+// |
+ |
+define TWO_M55 = 2.77555756156289135105e-17; // 2^-55, empty lower half |
Raymond Toy
2015/10/16 17:07:25
A bit late, but this comment is incorrect. Since
|
+ |
+function MathTanh(x) { |
+ x = x * 1; // Convert to number. |
+ // x is Infinity or NaN |
+ if (!NUMBER_IS_FINITE(x)) { |
+ if (x > 0) return 1; |
+ if (x < 0) return -1; |
+ return x; |
+ } |
+ |
+ var ax = MathAbs(x); |
+ var z; |
+ // |x| < 22 |
+ if (ax < 22) { |
+ if (ax < TWO_M55) { |
+ // |x| < 2^-55, tanh(small) = small. |
+ return x; |
+ } |
+ if (ax >= 1) { |
+ // |x| >= 1 |
+ var t = MathExpm1(2 * ax); |
+ z = 1 - 2 / (t + 2); |
+ } else { |
+ var t = MathExpm1(-2 * ax); |
+ z = -t / (t + 2); |
+ } |
+ } else { |
+ // |x| > 22, return +/- 1 |
+ z = 1; |
+ } |
+ return (x >= 0) ? z : -z; |
+} |
+ |
// ES6 draft 09-27-13, section 20.2.2.21. |
// Return the base 10 logarithm of x |
// |
@@ -1029,6 +1086,7 @@ utils.InstallFunctions(GlobalMath, DONT_ENUM, [ |
"tan", MathTan, |
"sinh", MathSinh, |
"cosh", MathCosh, |
+ "tanh", MathTanh, |
"log10", MathLog10, |
"log2", MathLog2, |
"log1p", MathLog1p, |