2016-10-23 4 views
2

で動作していないと三角法のものの逆関数を作成しながら、私が問題に自分自身を発見しました。ラジアンを使用している間はすべて動作しましたが、度合いが間違っています。度変換は、私は厳密に電卓を作成するJavaScriptを使用しています逆trignometry

私はそれは私の変換が間違っているのと同じくらい簡単でしたいが、度を使用して他のすべては(すなわち、非逆三角法のもの)完璧に働いていました。次のコードは、度変換と三角関数との関係を持つ計算機の部分です。

コードスニペット:

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

私は私のように、度への変換ラジアンためMath.PI/180について...他のすべてはラジアンで動作しますので、問題は、どこかtoDegreesであると仮定します前にも言った、それは他のどこでも動作し、私もそれをテストし、それはちょうど、他の計算のすべてを台無しにし、現在の問題を解決していません。

私は他の計算のすべてをテストしてみた、彼らは正しいです。繰り返しますが、ラジアン単位のすべてが機能しますが、逆関数は度には機能しませんが、通常の関数では機能します。

答えて

1

あなたはラジアンから度に逆三角関数の入力を変換しているが、それは最初の場所で、ラジアンではありません。逆関数を使用する場合は、出力を変換する必要があります。

+0

「toDegrees(Math.asin(val1))」を意味するのであれば、私はすでにそれを試していますが(疑問に言及するつもりですが忘れてしまった)、まだ動作しません。たとえば、 'toDegrees(Math.asin(.5))= 0.009138522593601258'、ここでは30、' toDegrees(Math.acos(.5))= 0.018277045187202516'は60になります。 – Gnago

+0

@Gnago:Your 'toDegrees'関数は度に変換されません。 '関数toDegrees(ラジアン){戻りラジアン* 180 /にMath.PIを書きます。 } ''と '' toRadians(degrees){return degrees * Math.PI/180; } '。 – Ryan

+0

ええ、それは問題を解決しました、私はそれが他の場所で働いたので、全体を避けました。ああ、助けてくれてありがとう – Gnago

関連する問題