--- 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 ---
Math.sign()
関数は、引数として渡された数値の符号が正か負かを表す +/- 1 を返します。 Math.sign()
に渡された数が 0 であれば、 +/- 0 を返します。なお、数値が正である場合、明示的な (+) は返されません。
Math.sign(x)
x
number
ではない場合は、暗黙に変換されます。与えられた引数の符号を表す数値です。
1
を返します。-1
を返します。0
を返します。-0
を返します。sign()
は Math
の静的メソッドなので、常に Math.sign()
として使用し、自分で Math
オブジェクトを生成してそのメソッドとして使用しないでください。 (Math
にはコンストラクターがありません)。
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 }; }
上記のポリフィルでは (x > 0)
と (x < 0)
の数値を互いに減算することで、真偽値から数値型へ強制的に型変換されるため、追加の型強制は必要ありません。
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
仕様書 |
---|
{{SpecName('ESDraft', '#sec-math.sign', 'Math.sign')}} |
{{Compat("javascript.builtins.Math.sign")}}