aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/math/tanh/index.html
blob: 9a3eb0f21d72f2ada2c62086d50f1e73d7719331 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
title: Math.tanh()
slug: Web/JavaScript/Reference/Global_Objects/Math/tanh
tags:
  - ECMAScript6
  - JavaScript
  - Math
  - Method
  - tanh
  - 双曲正切
translation_of: Web/JavaScript/Reference/Global_Objects/Math/tanh
---
<div>{{JSRef}}</div>

<p><strong><code>Math.tanh()</code></strong> 函数将会返回一个数的双曲正切函数值,公式如下:</p>

<p><math display="block"><semantics><mrow><mo lspace="0em" rspace="0em">tanh</mo><mi>x</mi><mo>=</mo><mfrac><mrow><mo lspace="0em" rspace="0em">sinh</mo><mi>x</mi></mrow><mrow><mo lspace="0em" rspace="0em">cosh</mo><mi>x</mi></mrow></mfrac><mo>=</mo><mfrac><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>-</mo><msup><mi>e</mi><mrow><mo>-</mo><mi>x</mi></mrow></msup></mrow><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>+</mo><msup><mi>e</mi><mrow><mo>-</mo><mi>x</mi></mrow></msup></mrow></mfrac><mo>=</mo><mfrac><mrow><msup><mi>e</mi><mrow><mn>2</mn><mi>x</mi></mrow></msup><mo>-</mo><mn>1</mn></mrow><mrow><msup><mi>e</mi><mrow><mn>2</mn><mi>x</mi></mrow></msup><mo>+</mo><mn>1</mn></mrow></mfrac></mrow><annotation encoding="TeX">\tanh x = \frac{\sinh x}{\cosh x} = \frac {e^x - e^{-x}} {e^x + e^{-x}} = \frac{e^{2x} - 1}{e^{2x}+1}</annotation></semantics></math></p>

<div>{{EmbedInteractiveExample("pages/js/math-tanh.html")}}</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><code>Math.tanh(<var>x</var>)</code></pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>x</code></dt>
 <dd>待计算的数字。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>所给数字的双曲正切值。</p>

<h2 id="描述">描述</h2>

<p>因为 <code>tanh()</code><code>Math</code> 的静态方法,所以总应该直接调用 <code>Math.tanh()</code> ,而不是创建 <code>Math</code> 对象再调用该方法(<code>Math</code> 不是一个构造函数)。</p>

<h2 id="示例">示例</h2>

<h3 id="使用_Math.tanh">使用 <code>Math.tanh()</code></h3>

<pre class="brush: js">Math.tanh(0);        // 0
Math.tanh(Infinity); // 1
Math.tanh(1);        // 0.7615941559557649
</pre>

<h2 id="向下兼容">向下兼容</h2>

<p><code>tanh()</code> 可以通过 {{jsxref("Math.exp()")}} 函数实现:</p>

<pre class="brush: js">Math.tanh = Math.tanh || function(x){
    var a = Math.exp(+x), b = Math.exp(-x);
    return a == Infinity ? 1 : b == Infinity ? -1 : (a - b) / (a + b);
}</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-math.tanh', 'Math.tanh')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

<p>{{Compat("javascript.builtins.Math.tanh")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li>{{jsxref("Math.acosh()")}}</li>
 <li>{{jsxref("Math.asinh()")}}</li>
 <li>{{jsxref("Math.atanh()")}}</li>
 <li>{{jsxref("Math.cosh()")}}</li>
 <li>{{jsxref("Math.sinh()")}}</li>
</ul>