1
我想要做一些「複雜」的數學,我需要調用一些JavaScript的數學屬性來解決二次方程。以下方法是否有效?這種方法是否適用於使用JavaScript解決二次方程?
root = Math.pow(inputb,2) - 4 * inputa * inputc;
root1 = (-inputb + Math.sqrt(root))/2*inputa;
root2 = (-inputb - Math.sqrt(root))/2*inputa;
這樣看起來正確嗎?
出於某種原因,我沒有看到正確的結果..
inputa
,inputb
和inputc
都是變量,從店面的方式在文本字段用戶輸入。
全碼
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
window.onload = function() {
$('.error').hide();
document.getElementById('quadraticcalculate').onclick = function calculateQuad()
{
var inputa = document.getElementById('variablea').value;
var inputb = document.getElementById('variableb').value;
var inputc = document.getElementById('variablec').value;
inputa = new Number(inputa); // try to convert to number
if (isNaN(inputa)) { // use built in method to check for NaN
$('#quadraticaerror').show();
return;
}
inputb = new Number(inputb); // try to convert to number
if (isNaN(inputb)) { // use built in method to check for NaN
$('#quadraticberror').show();
return;
}
inputc = new Number(inputc); // try to convert to number
if (isNaN(inputc)) { // use built in method to check for NaN
$('#quadraticcerror').show();
return;
}
root = Math.pow(inputb,2) - 4 * inputa * inputc;
root1 = (-inputb + Math.sqrt(root))/(2*inputa);
root2 = (-inputb - Math.sqrt(root))/(2*inputa);
document.getElementById('root1').value = root1;
document.getElementById('root2').value = root2;
if(root<'0')
{
document.getElementById('root1').value = 'No real solution'
document.getElementById('root2').value = 'No real solution'
}
else {
if(root=='0')
{
document.getElementById('root1').value = root1
document.getElementById('root2').value = 'No Second Answer'
}
else {
document.getElementById('root1').value = root1
document.getElementById('root2').value = root1
}
}
};
document.getElementById('quadraticerase').onclick = function()
{
document.getElementById('quadraticform').reset();
$('.error').hide();
}
document.getElementById('cubicerase').onclick = function()
{
document.getElementById('cubicform').reset();
$('.error').hide();
}
}
</script>
<style>
div.#wrapper
{
text-align: center;
}
.error
{
color: #FF0000;
}</style>
</head>
<body>
<div id="wrapper">
<div id="quadratic">
<form id="quadraticform">
<h1>Quadratic</h1>
a:<input id="variablea" value="" type="text">
<br/>
b:<input id="variableb" value="" type="text">
<br />
c:<input id="variablec" value="" type="text">
<br />
<input id="quadraticcalculate" value="Calculate!" type="button">
<input id="quadraticerase" value="Clear" type="button">
<br />
<br />
Roots:
<br />
<input id="root1" type="text" readonly>
<br />
<input id="root2" type="text" readonly>
<p id="quadraticaerror" class="error">Error: Variable a is not a valid integer!</p>
<br />
<p id="quadraticberror" class="error">Error: Variable b is not a valid integer!</p>
<br />
<p id="quadraticcerror" class="error">Error: Variable c is not a valid integer!</p>
</form>
</div>
<div id="cubic">
<form id="cubicform">
<h1>Cubic</h1>
a:<input id="variablea" value="" type="text">
<br/>
b:<input id="variableb" value="" type="text">
<br />
c:<input id="variablec" value="" type="text">
<br />
d:<input id="variabled" value="" type="text">
<br />
<input id="cubiccalculate" value="Calculate!" type="button">
<input id="cubicerase" value="Clear" type="button">
<br />
<br />
Roots:
<br />
<input id="root1" type="text" readonly>
<br />
<input id="root2" type="text" readonly>
<p id="cubicaerror" class="error">Error: Variable a is not a valid integer!</p>
<br />
<p id="cubicberror" class="error">Error: Variable b is not a valid integer!</p>
<br />
<p id="cubiccerror" class="error">Error: Variable c is not a valid integer!</p>
<br />
<p id="cubicderror" class="error">Error: Variable d is not a valid integer!</p>
</form>
</div>
</div>
</body>
</html>
哦哇,啊!我怎麼會錯過!謝謝!不過出於某種原因,它仍然吐出了錯誤的根源。例如,當輸入'a = 1','b = 1'和'c = 0'時,它將'x = 0'和'x = 0'作爲兩個獨立和真實的根。然而,給定'a,b,c'的真正的根是'x = -1'和'x = 0' .... – Qcom 2010-10-14 05:38:42
可能與浮點和整數截斷有關(你希望sqrt總是返回一個浮點數),在'root1 ='和'root2 ='開始的幾個'1.0 *'中查找並查看是否修復了它 – tobyodavies 2010-10-14 05:45:39
當我在JS控制檯中運行該命令時,我無法獲得正確答案'1.0 *'和螢火蟲相同 – tobyodavies 2010-10-14 05:48:27