如何在javascript中使用Math.sin()来获得正确的答案?

前端之家收集整理的这篇文章主要介绍了如何在javascript中使用Math.sin()来获得正确的答案?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > How can I get sin,cos,and tan to use degrees instead of radians?5个
当我使用Math.sin(90)在javascript中计算90度的正弦时,它返回0.8939966636005565,但sin(90)是1.有没有办法解决这个问题?我需要任何角度的准确值.
<!DOCTYPE html>
<html>
<body>
    <p id="demo">Click the button calculate value of 90 degrees.</p>
    <button onclick="myFunction()">Try it</button>
<script>
function myFunction(){
    document.getElementById("demo").innerHTML=Math.sin(90);
}
</script>

解决方法

Math.sin期望输入为弧度,但您期望得到90度的结果.将它转换为弧度,就像这样
console.log(Math.sin(90 * Math.PI / 180.0));
# 1

根据维基百科的Radian to Degree conversion formula,

Angle in Radian = Angle in Degree * Math.PI / 180
原文链接:https://www.f2er.com/js/155296.html

猜你在找的JavaScript相关文章