Math.tan()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Math.tan()
静的メソッドは、数値の正接(タンジェント)を返します。
試してみましょう
function getTanFromDegrees(degrees) {
return Math.tan((degrees * Math.PI) / 180);
}
console.log(getTanFromDegrees(0));
// Expected output: 0
console.log(getTanFromDegrees(45));
// Expected output: 0.9999999999999999
console.log(getTanFromDegrees(90));
// Expected output: 16331239353195370
構文
js
Math.tan(x)
引数
x
-
ラジアンで角度を表す数値です。
返値
解説
tan()
は Math
の静的メソッドであるため、生成した Math
オブジェクトのメソッドとしてではなく、常に Math.tan()
として使用するようにしてください (Math
はコンストラクターではありません)。
例
Math.tan() の使用
js
Math.tan(-Infinity); // NaN
Math.tan(-0); // -0
Math.tan(0); // 0
Math.tan(1); // 1.5574077246549023
Math.tan(Math.PI / 4); // 0.9999999999999999 (浮動小数点エラー)
Math.tan(Infinity); // NaN
Math.tan() および π/2
正確に tan(π/2)
を計算することはでいません。
js
Math.tan(Math.PI / 2); // 16331239353195370
Math.tan(Math.PI / 2 + Number.EPSILON); // -6218431163823738
Math.tan() に角度の値を使用
Math.tan()
関数はラジアンを受け付けますが、角度で使用したほうが簡単な場合が多いので、次の関数は角度の値を受け付け、それをラジアンに変換してタンジェントを返します。
js
function getTanDeg(deg) {
const rad = (deg * Math.PI) / 180;
return Math.tan(rad);
}
仕様書
Specification |
---|
ECMAScript® 2025 Language Specification # sec-math.tan |
ブラウザーの互換性
BCD tables only load in the browser