Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中的正反两面_Javascript - Fatal编程技术网

JavaScript中的正反两面

JavaScript中的正反两面,javascript,Javascript,我正在尝试用JavaScript构建一个计算器。除了罪和因,它是有效的,它们会产生错误和意外的结果 正反两方面都会产生错误的结果 sin(180)=0 在代码sin(180)=-0.8011526357338304中 我的代码如下所示 JavaScript: HTML: 请告诉我如何求sin和cos的值将你的角度乘以:(π/180) 其中,π=Math.PI 这些功能以弧度运行 因此,虽然您的意思是180°,但函数接收到180弧度 这是10313.2403° }触发

我正在尝试用JavaScript构建一个计算器。除了罪和因,它是有效的,它们会产生错误和意外的结果

正反两方面都会产生错误的结果 sin(180)=0

在代码sin(180)=-0.8011526357338304中

我的代码如下所示

JavaScript: HTML:



请告诉我如何求sin和cos的值

将你的角度乘以:
(π/180)

其中,
π=Math.PI

这些功能以
弧度运行

因此,虽然您的意思是180°,但函数接收到180
弧度

这是10313.2403°


}

触发功能以弧度(而非度)为单位测量角度。180度是π弧度,因此要在调用函数之前将角度乘以
(π/180)
。您可以为数字定义一个方便的
toRad()
方法,我将其乘以,但如果(val==“sin”){var x=document.getElementById(“d”).value;x=x*(Math.PI/180);document.getElementById(“d”)则会给出错误的结果.value=Math.sin(x);}“错误结果”到底是什么?sin(180)=1.2246467991473532e-16
function d(val){
    if(val=="sin"){
        var x=document.getElementById("d").value;
        document.getElementById("d").value=Math.sin(x);
    }
    if(val=="cos"){
        var x=document.getElementById("d").value;
        document.getElementById("d").value=Math.cos(x);
    }
    if(val=="envers"){
        var x=document.getElementById("d").value;
        document.getElementById("d").value=eval(1/x);
    }
    if(val=="sqrt"){
        var x=document.getElementById("d").value;
        document.getElementById("d").value=Math.sqrt(x);
    }
}

function c(val){
    document.getElementById("d").value=val;
}

function v(val){
    document.getElementById("d").value+=val;
}

function e(){ 
    try { 
      c(eval(document.getElementById("d").value)) 
    } 
    catch(e){
        c('Error') 
    } 
}
<div class="display">
    <p>
        <input type="text" readonly size="14" id="d">
        <input type="button" class="button black" value="C" onclick='c("")'>  
    </p>
</div>
<div class="keys">
    <p>
        <input type="button" class="button black" value="1" onclick='v("1")'>
        <input type="button" class="button black" value="2" onclick='v("2")'>
        <input type="button" class="button black" value="3" onclick='v("3")'>
        <input type="button" class="button pink" value="+" onclick='v("+")'>
    </p>
    <p>
        <input type="button" class="button black" value="4" onclick='v("4")'>
        <input type="button" class="button black" value="5" onclick='v("5")'>
        <input type="button" class="button black" value="6" onclick='v("6")'>
        <input type="button" class="button pink" value="-" onclick='v("-")'>
    </p>
    <p>
        <input type="button" class="button black" value="7" onclick='v("7")'>
        <input type="button" class="button black" value="8" onclick='v("8")'>
        <input type="button" class="button black" value="9" onclick='v("9")'>
        <input type="button" class="button pink" value="*" onclick='v("*")'></p>

    <p>
        <input type="button" class="button black" value="." onclick='v(".")'>
        <input type="button" class="button black" value="0" onclick='v("0")'>
        <input type="button" class="button orange" value="=" onclick='e()'>
        <input type="button" class="button pink" value="/" onclick='v("/")'>
    </p>

    <p>
        <input type="button" class="button black" value="sin" onclick='d("sin")'>
        <input type="button" class="button black" value="cos" onclick='d("cos")'>
        <input type="button" class="button orange" value="1/x" onclick='d("envers")'>
        <input type="button" class="button pink" value="sqrt" onclick='d("sqrt")'>
    </p>
</div>
function d(val)
{

if(val=="sin")
{
    var x=document.getElementById("d").value;
    x= x * Math.PI / 180;
    document.getElementById("d").value=Math.sin(x);
}
if(val=="cos")
{
    var x=document.getElementById("d").value;
    x= x * Math.PI / 180;
    document.getElementById("d").value=Math.cos(x);
}
if(val=="envers")
{
    var x=document.getElementById("d").value;
    document.getElementById("d").value=eval(1/x);
}
if(val=="sqrt")
{
    var x=document.getElementById("d").value;
    document.getElementById("d").value=Math.sqrt(x);
}