度数转换不适用于反三角函数

Degrees conversion isn't working with inverse trignometry

我正在严格使用 JavaScript 创建一个计算器,但我发现自己在创建三角函数的反函数时遇到了问题。使用弧度时一切正常,但在度数方面,我得到了错误的答案。

我希望它像我的转换错误一样简单,但其他所有使用度数的东西(即非反三角函数)都运行良好。以下代码是计算器中与度数转换和三角函数有关的部分。

代码片段:

<html>
  <head>
      <script language="javascript" type="text/javascript">
        var degVal = true;
        var Deux = false;
        function toDegrees(angle, mode){ //This function converts radians to degrees, and vice versa
    if (degVal == true){
      mode = (Math.PI/180); //for some reason this incorrect conversion formula spits out the correct answers
    }
    else if (degVal == false){
       mode = 1;
    }
    return angle * mode;
    }
  function BasicTrig(TrigFunc, TrigInv){
    var val1 = toDegrees(parseFloat(document.getElementById("value1").value));
    var ansD = document.getElementById("answer");
          if (Deux == false){
      ansD.value = TrigFunc(val1);
    }
    else if (Deux == true){
            ansD.value = TrigInv(val1); //For some reason, doesn't work in degrees
          }
  }
     function MoreTrig(TrigEq, TrigEqInv){ // This function allows csc, sec, & cot to be performed
    var val1 = toDegrees(parseFloat(document.getElementById("value1").value));
    var ansD = document.getElementById("answer");
    if (Deux == false){
      ansD.value = 1 / TrigEq(val1);
    }
    else if (Deux == true){
            ansD.value = TrigEqInv(1 / val1); //Also doesn't work in degrees
          }
  }
        function Deuxieme(){ //2nd function (Calculator) button
          if (Deux == false){
            document.getElementById("LeDeux").style.backgroundColor = "orange";
   Deux = true;
   document.getElementById("sinbutton").value = "sin⁻¹"; //changes sin to sin⁻¹
   document.getElementById("cosbutton").value = "cos⁻¹"; //changes cos to cos⁻¹
   document.getElementById("tanbutton").value = "tan⁻¹"; //changes tan to tan⁻¹
   document.getElementById("cscbutton").value = "csc⁻¹"; //changes csc to csc⁻¹ csc being cosec for those who are used to that
   document.getElementById("secbutton").value = "sec⁻¹"; //changes sec to sec⁻¹
   document.getElementById("cotbutton").value = "cot⁻¹"; //changes cot to cot⁻¹
          }
    else if (Deux == true){
            document.getElementById("LeDeux").style.backgroundColor = "";
      Deux = false;
   document.getElementById("sinbutton").value = "sin";
   document.getElementById("cosbutton").value = "cos";
      document.getElementById("tanbutton").value = "tan";
      document.getElementById("cscbutton").value = "csc";
         document.getElementById("secbutton").value = "sec";    document.getElementById("cotbutton").value = "cot";
          }
  }
        function RadDegHighlight(){ //Switches between Radians and Degrees
    if (degVal == true){
      document.getElementById("drbutton").value = "rad";
   degVal = false;
    }
    else if (degVal == false){
      document.getElementById("drbutton").value = "deg";
      degVal = true;
    }
  }
      </script>
    </head>
    <body>
   <input type="text" placeholder="x" style="width:48.5%" id="value1"/>
   <input type="button" id="LeDeux" style="width:23.5%" value="2nd" onclick="Deuxieme()"/>
    <input type="button" id="drbutton" style="width:23.5%" value="deg" onclick="RadDegHighlight()"/>
    <input type="button" id="sinbutton" style="width:23.5%" value="sin" onclick="BasicTrig(Math.sin, Math.asin)"/>
    <input type="button" id="cosbutton" style="width:23.5%" value="cos" onclick="BasicTrig(Math.cos, Math.acos)"/>
    <input type="button" id="tanbutton" style="width:23.5%" value="tan" onclick="BasicTrig(Math.tan, Math.atan)"/>
    <input type="button" id="cscbutton" style="width:23.5%" value="csc" onclick="MoreTrig(Math.sin, Math.asin)"/>
    <input type="button" id="secbutton" style="width:23.5%" value="sec" onclick="MoreTrig(Math.cos, Math.acos)"/>
    <input type="button" id="cotbutton" style="width:23.5%" value="cot" onclick="MoreTrig(Math.tan, Math.atan)"/>
    <input type="text" placeholder="Answer" style="width:100%" id="answer"/>
  </body>
</html>

我认为问题出在 toDegrees 的某处,因为其他一切都以弧度为单位...关于弧度到度数转换的 Math.PI/180,正如我之前所说,它在其他任何地方都有效,我什至对其进行了测试,它只是把所有其他计算都搞乱了,并没有解决当前的问题。

我已经测试了所有其他计算,它们都是正确的。同样,以弧度为单位的一切都有效,但反函数不适用于度数,但正常函数有效。

您正在将反三角函数的输入从弧度转换为度,但它首先不是弧度。使用逆时需要转换输出。