简体   繁体   中英

Why is my output NaN? On excel, the program outputs a finite number. Calling all Math Wizzes

I am writing an equation calculator with JavaScript and HTMl, but when I input some numbers into my equation, the output on the screen reads NaN, even though on Microsoft Excel it outputs a number. Why is this? By the way, my equation works with imaginary numbers. The equation goes like this.

let MGErIn = document.getElementById('MGErIn');
let MGaIn = document.getElementById('MGaIn');
let MGFreqIn = document.getElementByID('MGFreqIn');

function WL3outcalc(){
      return 1000/25.4*1000*2*Math.PI/(Math.sqrt(Math.pow((2*Math.PI*parseFloat(MGFreqIn.value)*Math.pow(10,9)*Math.sqrt(parseFloat(MGErIn.value))/299792458),2)-Math.pow((Math.PI/(parseFloat(MGaIn.value)*25.4/1000)),2)))

I know the equation is very long, sorry about that.

For example, when I input 1 for MGErIn, I input 0.266 for MGaIn,and I input 35 for MGFreqIn My website outputs NaN, but on excel it outputs 435.8.

The equation used on excel is written below.

=IFERROR(1000/25.4*1000*2*PI()/(SQRT((2*PI()*A17*10^9*SQRT(D17)/299792458)^2-(PI()/(F17*25.4/1000))^2)),"Cutoff")

As you can see, our equations are identical minus the syntax of it, but my excel program outputs 435.8 but on my website it outputs NaN.

Math Wizards and Coding Geniuses, Help me with this Problem!!

I haven't tried this in excel, but JS seems to work just fine for me. I added a button to return the results, you could instead have it go automatically when the fields updated. No idea what your html looked like so this is just an example. Its returning 436.01086504866197 or 426.01 when I added the toFixed(2). If its not working for you perhaps there was other code that you haven't shared that is causing an issue? I added RobG's note about the 1e9.

 <html> <body> <input id="MGErIn" value="1"> <input id="MGaIn" value="0.266"> <input id="MGFreqIn" value="35"> <button onclick="WL3outcalc()">Submit</button> <h1>Result: </h1> <p id="answer"></p> <script> let MGErIn = document.getElementById('MGErIn'); let MGaIn = document.getElementById('MGaIn'); let MGFreqIn = document.getElementById('MGFreqIn'); function WL3outcalc() { var answer = 1000 / 25.4 * 1000 * 2 * Math.PI / (Math.sqrt(Math.pow((2 * Math.PI * parseFloat(MGFreqIn.value) * 1e9 * Math.sqrt(parseFloat(MGErIn.value)) / 299792458), 2) - Math.pow((Math.PI / (parseFloat(MGaIn.value) * 25.4 / 1000)), 2))); document.getElementById("answer").innerHTML = answer.toFixed(2); } </script> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM