Math.asin(x) 返回數字 x 的反正弦值(反正弦),結果以弧度表示,範圍為 [-π/2, π/2](即 -90° 到 90°)。
Math.asin(x)
[-1, 1] 範圍內的數字-π/2 到 π/2 之間的弧度值;若 x 超出範圍,返回 NaN// 標準值
console.log(Math.asin(-1)); // -1.5707963267948966 (-π/2, -90°)
console.log(Math.asin(0)); // 0 (0°)
console.log(Math.asin(1)); // 1.5707963267948966 (π/2, 90°)
console.log(Math.asin(0.5)); // 0.5235987755982989 (π/6, 30°)
console.log(Math.asin(-0.5)); // -0.5235987755982989 (-π/6, -30°)
// 邊界與異常
console.log(Math.asin(1.1)); // NaN
console.log(Math.asin(null)); // 0 (null → 0)
console.log(Math.asin('0.5')); // 0.5235987755982989
console.log(Math.asin('abc')); // NaN
const radToDeg = rad => rad * 180 / Math.PI;
console.log(radToDeg(Math.asin(0.5))); // 30
console.log(radToDeg(Math.asin(-0.5))); // -30
console.log(radToDeg(Math.asin(1))); // 90
// 已知對邊 a、斜邊 c,求角度 θ
function angleFromOppositeHypotenuse(opposite, hypotenuse) {
const ratio = opposite / hypotenuse;
// 限制範圍防止浮點誤差
const clamped = Math.max(-1, Math.min(1, ratio));
return Math.asin(clamped);
}
const angleRad = angleFromOppositeHypotenuse(3, 5);
console.log(radToDeg(angleRad)); // 36.87°
// 向量 (x,y) 與正 X 軸夾角
// 注意:asin 只能得出 -90°~90°,無法區分象限
// 推薦使用 Math.atan2(y, x) 獲得完整 -180°~180°
function angleWithXAxis(x, y) {
const len = Math.hypot(x, y);
if (len === 0) return 0;
// 僅當向量在右半平面 (x>0) 時 asin 可用
return Math.asin(y / len);
}
// 正確通用做法
function angleWithXAxisCorrect(x, y) {
return Math.atan2(y, x); // 返回 -π 到 π
}
// 簡諧運動:x = A * sin(ωt + φ)
// 已知位移 x、振幅 A,求相位
function phaseFromDisplacement(x, amplitude) {
const ratio = x / amplitude;
return Math.asin(Math.max(-1, Math.min(1, ratio)));
}
| 特性 | Math.asin(y/r) | Math.atan2(y, x) |
|---|---|---|
| 返回範圍 | [-π/2, π/2] (-90°~90°) | [-π, π] (-180°~180°) |
| 象限判斷 | 無(僅右半平面正確) | 自動根據 x,y 符號判斷 |
| 參數 | 比值 y/r | 分量 y, x |
| 適用場景 | 已知對邊/斜邊比值 | 已知向量分量(推薦通用) |
ratio > 1,產生 NaN,必須 clampasin 無法區分第二、三象限,**向量角度請用 Math.atan2(y, x)**Math.acos(x) - 反餘弦,範圍 [0, π]Math.atan(x) - 反正切,範圍 [-π/2, π/2]Math.atan2(y, x) - **首選** 全平面向量角度Math.sin(x) - 正弦(正向操作)Math.asin() 適用於「已知正弦值求角度」的場景,但處理二維向量角度時,永遠優先使用 Math.atan2() 以獲得正確象限。