Javascript calculates ATAN function differently than Excel does

46 views Asked by At

I was given an Excel table with couple of formulas that I need to translate into a simple javascript calculator. I'm having issues with Angle of View.

Excel formula for qhAFOV= 2*DEGREES(ATAN(qhF/2*qf)) If

qhf = 12;
qf = 3.4; 

The result should be 174,39. (degrees if it's important) Instead I keep getting the result of 120.92

This is my JS line: var qhAFOV = (2 * Math.atan(qhf / (2 * qf))) * (180 / Math.PI);

And the whole small script:

function calculateFocalLengthLS() {
      // Get input values
      var qf = parseFloat(document.getElementById('qf').value);
      var qIC = parseFloat(document.getElementById('qic').value);
      var qvFOV = parseFloat(document.getElementById('qvfov').value);
      var qhN = parseFloat(document.getElementById('qhn').value);
      var qvN = parseFloat(document.getElementById('qvn').value);
      var qP = parseFloat(document.getElementById('qp').value);

      // Validate inputs
      if (isNaN(qf) || isNaN(qIC) || isNaN(qvFOV) || isNaN(qhN) || isNaN(qvN) || isNaN(qP)) {
        alert('Please enter valid values for all input fields.');
        return;
      }

      // Calculate sensor sizes
      var qhS = qhN * qP;
      var qvS = qvN * qP;
      var qdS = Math.sqrt(Math.pow(qhS, 2) + Math.pow(qvS, 2));

      // Calculate frame sizes
      var qhf = Math.min(qIC, qhS);
      var qvf = Math.min(qIC, qvS);
      var qdf = Math.min(qIC, qdS);

      // Calculate angle of view
      var qhAFOV = (2 * Math.atan(qhf / (2 * qf))) * (180 / Math.PI);
      var qvAFOV = (2 * Math.atan(qvf / (2 * qf))) * (180 / Math.PI);
      var qdAFOV = (2 * Math.atan(qdf / (2 * qf))) * (180 / Math.PI);

      // Display results
      document.getElementById('qhs').value = qhS.toFixed(2);
      document.getElementById('qvs').value = qvS.toFixed(2);
      document.getElementById('qds').value = qdS.toFixed(2);
      document.getElementById('qhf').value = qhf.toFixed(2);
      document.getElementById('qvf').value = qvf.toFixed(2);
      document.getElementById('qdf').value = qdf.toFixed(2);
      document.getElementById('qhafov').value = qhAFOV.toFixed(2);
      document.getElementById('qvafov').value = qvAFOV.toFixed(2);
      document.getElementById('qdafov').value = qdAFOV.toFixed(2);
    }

I found online that Excel somehow calculates ATAN differently, but I cannot figure out how to reflect that on my script.

Kindly help.

I tried reversing the formula to var qhAFOV = (2 * Math.atan2(2 * qf, qhf)) * (180 / Math.PI);

and

var hF = 12; // Replace with your actual value of qhF
var f = 3.4; // Replace with your actual value of qf

var angleInRadians = Math.atan2(qhF / (2 * qf), 1);
var angleInDegrees = 2 * (angleInRadians * (180 / Math.PI));

console.log(angleInDegrees); // Also wrong result
1

There are 1 answers

2
MilesZew On BEST ANSWER

This looks to be a pretty simple order of operations problem. In excel you're calculating: 2*DEGREES(ATAN(qhF/2*qf)) the problem here is the qhF/2*qf. This is interpreted as (qhF/2)*qf. But in you javascript you have additional brackets: qhf / (2 * qf)). Typing the values into a calculator confirms this. Considering you added the extra brackets in the javascript, I'm assuming you intended to calculate qhf / (2 * qf)), which would mean 120.92 is actually the correct value. Adding in the extra brackets to the excel formula should hopefully fix the discrepancy (ie. 2*DEGREES(ATAN(qhF/(2*qf)))).