javascript - 度数转换不适用于反三 Angular 函数

标签 javascript html trigonometry inverse degrees

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

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

代码片段:

<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,如我之前说过,它在其他任何地方都有效,我什至对其进行了测试,但它只是把所有其他计算都搞乱了,并没有解决当前的问题。

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

最佳答案

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

关于javascript - 度数转换不适用于反三 Angular 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40198409/

相关文章:

javascript - 使用 scripty2 滚动

javascript - 如何用JavaScript找到页面的中心点?

php - 发送抓取请求以获取 torrent 的种子和同行

Java netbeans Arctan 错误答案?

python - 为什么在使用 python 和 numpy 时 sin(180) 不为零?

java - 如何在java中调整sin(x)图形坐标以仅填充窗口的一部分

javascript - 有没有办法单独删除 Canvas 对象(一个一个)fabric js

javascript - 如何使用 sigma.js 显示来自 JSON 对象的数据

javascript - PHP MySQL 根据选择的选项和 onclick 显示数据库

html - 动态增加textarea和改变位置div