diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
| commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
| tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/ja/web/javascript/reference/global_objects/math/sign | |
| parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
| download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip | |
initial commit
Diffstat (limited to 'files/ja/web/javascript/reference/global_objects/math/sign')
| -rw-r--r-- | files/ja/web/javascript/reference/global_objects/math/sign/index.html | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/global_objects/math/sign/index.html b/files/ja/web/javascript/reference/global_objects/math/sign/index.html new file mode 100644 index 0000000000..22df8ab790 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/math/sign/index.html @@ -0,0 +1,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 > 0) - (x < 0)) || +x; + // A more aesthetic pseudo-representation: + // + // ( (x > 0) ? 1 : 0 ) // if x is positive, then positive one + // + // else (because you can't be both - and +) + // ( (x < 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 > 0)</code> と <code>(x < 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> |
