此方法适用于使用 JavaScript 求解二次方程吗?

发布于 2024-09-27 06:08:31 字数 5644 浏览 0 评论 0原文

我正在尝试做一些“复杂”的数学运算,我需要调用 JavaScript 的一些数学属性来求解二次方程。下面的方法有效吗?

    root = Math.pow(inputb,2) - 4 * inputa * inputc;
    root1 = (-inputb + Math.sqrt(root))/2*inputa;
    root2 = (-inputb - Math.sqrt(root))/2*inputa;

这看起来正确吗?

由于某种原因,我没有看到正确的结果。

inputainputbinputc 都是存储用户输入的变量顺便说一下文本字段。

完整代码

<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>

I'm trying to do some "complex" math where I need to call upon some of JavaScript's Math properties to solve the quadratic equation. Does the following method work?

    root = Math.pow(inputb,2) - 4 * inputa * inputc;
    root1 = (-inputb + Math.sqrt(root))/2*inputa;
    root2 = (-inputb - Math.sqrt(root))/2*inputa;

Does this look correct?

For some reason, I'm not seeing correct results..

inputa, inputb, and inputc are all variables which store user-input from a text field by the way.

FULL CODE

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2024-10-04 06:08:34

*/ 具有相同的优先级并且保持关联,因此您实际上得到的就是

root = Math.pow(inputb,2) - 4 * inputa * inputc;
root1 = ((-inputb + Math.sqrt(root))/2)*inputa;
root2 = ((-inputb - Math.sqrt(root))/2)*inputa;

您想要的

root = Math.pow(inputb,2) - 4 * inputa * inputc;
root1 = (-inputb + Math.sqrt(root))/(2*inputa);
root2 = (-inputb - Math.sqrt(root))/(2*inputa);

* and / have same precedence and are left associative so what you have effectively got is

root = Math.pow(inputb,2) - 4 * inputa * inputc;
root1 = ((-inputb + Math.sqrt(root))/2)*inputa;
root2 = ((-inputb - Math.sqrt(root))/2)*inputa;

what you want is

root = Math.pow(inputb,2) - 4 * inputa * inputc;
root1 = (-inputb + Math.sqrt(root))/(2*inputa);
root2 = (-inputb - Math.sqrt(root))/(2*inputa);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文