英文:
Javascript calculates ATAN function differently than Excel does
问题
Excel中的ATAN函数和JavaScript中的Math.atan函数计算方式不同。要在JavaScript中正确反映Excel的ATAN计算方式,您可以使用以下代码:
var qhAFOV = (2 * Math.atan(qhf / (2 * qf / 180 * Math.PI))) * (180 / Math.PI);
这将把Excel的ATAN计算方式正确应用到JavaScript中,以获得正确的结果。
请注意,Excel的ATAN函数默认使用角度(degrees),而JavaScript的Math.atan函数使用弧度(radians),因此在计算时需要进行适当的单位转换。在上面的代码中,我们将qf(角度)转换为弧度,并在最后将结果再次转换为角度以获得正确的答案。
这个修改应该能够解决您的问题。
英文:
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
得分: 1
这看起来是一个相当简单的操作顺序问题。在Excel中,您正在计算:2*DEGREES(ATAN(qhF/2*qf))
,问题在于qhF/2*qf
。这被解释为(qhF/2)*qf
。但在您的JavaScript中,有额外的括号:qhf / (2 * qf))
。将这些值输入计算器确认了这一点。考虑到您在JavaScript中添加了额外的括号,我假设您打算计算qhf / (2 * qf))
,这将意味着120.92实际上是正确的值。在Excel公式中添加额外的括号应该会解决这个差异(即2*DEGREES(ATAN(qhF/(2*qf)))
)。
英文:
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)))
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论