aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/math/sign/index.html
blob: 22df8ab79031d03113e71f9fa75aa95efc2e71da (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
---
title: Math.sign()
slug: Web/JavaScript/Reference/Global_Objects/Math/sign
tags:
  - JavaScript
  - Math
  - Method
  - Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Math/sign
---
<div>{{JSRef}}</div>

<p><strong><code>Math.sign()</code></strong> 関数は、引数として渡された数値の符号が<strong></strong><strong></strong>かを表す +/- 1 を返します。 <code>Math.sign()</code> に渡された数が 0 であれば、 +/- 0 を返します。なお、数値が正である場合、明示的な (+) は返され<strong>ません</strong></p>

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

<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力してくださる場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox notranslate">Math.sign(<var>x</var>)</pre>

<h3 id="Parameters" name="Parameters">引数</h3>

<dl>
 <dt><code><var>x</var></code></dt>
 <dd>数値です。引数が <code>number</code> ではない場合は、暗黙に変換されます。</dd>
</dl>

<h3 id="Return_value" name="Return_value">返値</h3>

<p>与えられた引数の符号を表す数値です。</p>

<ul>
 <li>引数が正の値の場合は、 <code>1</code> を返します。</li>
 <li>引数が負の値の場合は、 <code>-1</code> を返します。</li>
 <li>引数が正のゼロの場合は、 <code>0</code> を返します。</li>
 <li>引数が負のゼロの場合は、 <code>-0</code> を返します。</li>
 <li>それ以外は {{jsxref("NaN")}} を返します。</li>
</ul>

<h2 id="Description" name="Description">解説</h2>

<p><code>sign()</code><code>Math</code> の静的メソッドなので、常に <code>Math.sign()</code> として使用し、自分で <code>Math</code> オブジェクトを生成してそのメソッドとして使用しないでください。 (<code>Math</code> にはコンストラクターがありません)。</p>

<h2 id="Polyfill" name="Polyfill">ポリフィル</h2>

<pre class="brush: js notranslate">if (!Math.sign) {
  Math.sign = function(x) {
    // If x is NaN, the result is NaN.
    // If x is -0, the result is -0.
    // If x is +0, the result is +0.
    // If x is negative and not -0, the result is -1.
    // If x is positive and not +0, the result is +1.
    return ((x &gt; 0) - (x &lt; 0)) || +x;
    // A more aesthetic pseudo-representation:
    //
    // ( (x &gt; 0) ? 1 : 0 )  // if x is positive, then positive one
    //          +           // else (because you can't be both - and +)
    // ( (x &lt; 0) ? -1 : 0 ) // if x is negative, then negative one
    //         ||           // if x is 0, -0, or NaN, or not a number,
    //         +x           // then the result will be x, (or) if x is
    //                      // not a number, then x converts to number
  };
}
</pre>

<p>上記のポリフィルでは <code>(x &gt; 0)</code><code>(x &lt; 0)</code> の数値を互いに減算することで、真偽値から数値型へ強制的に型変換されるため、追加の型強制は必要ありません。</p>

<h2 id="Examples" name="Examples"></h2>

<h3 id="Using_Math.sign" name="Using_Math.sign">Math.sign() の使用</h3>

<pre class="brush: js notranslate">Math.sign(3);     //  1
Math.sign(-3);    // -1
Math.sign('-3');  // -1
Math.sign(0);     //  0
Math.sign(-0);    // -0
Math.sign(NaN);   // NaN
Math.sign('foo'); // NaN
Math.sign();      // NaN
</pre>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-math.sign', 'Math.sign')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>

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

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>{{jsxref("Math.abs()")}}</li>
 <li>{{jsxref("Math.ceil()")}}</li>
 <li>{{jsxref("Math.floor()")}}</li>
 <li>{{jsxref("Math.round()")}}</li>
 <li>{{jsxref("Math.trunc()")}}</li>
</ul>