0

我正在开发一个程序,该程序应该计算二次函数的根并输出它的根。但是,输出并不是所有情况下都应如此。当它应该没有解决方案或微不足道时,它输出为-nan(ind). 当它应该有一个解决方案时,它会输出x1 = -nan(ind)x2 = -inf. 我不太确定为什么会这样,我真的可以使用一些帮助。这是我的代码:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main() {

    // Initialize and define the variables:
    // a = the variable that stores the value for 'a' in the quadratic
    // b = the variable that stores the value for 'b' in the quadratic
    // c = the variable that stores the value for 'c' in the quadratic
    // d = the variable that stores the determinant of the quadratic function to find the nature of the roots (b^2-4ac)
    // root1 = the variable that stores the first possible root of a quadratic function
    // root2 = the variable that stores the second possible root of a quadratic function
    // realNum = the variable that stores the real portion of the complex roots
    // imaginaryNum = the variable that stores the imaginary portion of the complex roots
    double a, b, c, d, root1, root2, realNum, imaginaryNum;

    // Ask the user to input a value for variable 'a' of the quadratic
    // NOTE: 'setprecision' specifies the minimum precision, 'fixed' states a fixed number of decimals will
    // appear after the entered digit
    cout << "Please input a: " << setprecision(4) << fixed;
    cin >> a;                                   /// Store the value in variable 'a'

    // Ask the user to input a value for variable 'b' of the quadratic
    // NOTE: 'setprecision' specifies the minimum precision, 'fixed' states a fixed number of decimals will
    // appear after the entered digit
    cout << "Please input b: " << setprecision(4) << fixed;;
    cin >> b;                                   /// Store the value in variable 'b'

    // Ask the user to input a value for variable 'c' of the quadratic
    // NOTE: 'setprecision' specifies the minimum precision, 'fixed' states a fixed number of decimals will
    // appear after the entered digit
    cout << "Please input c: " << setprecision(4) << fixed;;
    cin >> c;                                   /// Store the value in variable 'c'

    // Calculate the determinant of the quadratic (b^2 - 2ac)
    d = ((pow(b, 2.0)) - (4 * a * c));


    // Check to see if the determinant is greater than 0
    if (d >= 0) {

        // Calculate each of the two possible roots for the quadratic
        root1 = (-b + sqrt(d)) / (2 * a);
        root2 = (-b - sqrt(d)) / (2 * a);

        // Display to the user that a solution does exist for the following quadratic
        cout << "Your equation has real roots: " << root1 << " and " << root2 << "." << endl;

    }


    // Check to see if the determinant is greater than 0
    else if (d < 0) {

        // Calculate the real portion of the complex roots for the quadratic
        realNum = (-b) / (2 * a);

        // Calculate the imaginary portion of the complex roots for the quadratic
        imaginaryNum = (sqrt(-d)) / (2 * a);

        // Combine the two portions of the complex roots and display the calculated complex roots to the user
        cout << "Your equation has complex roots: " << realNum << " + " << imaginaryNum << "i and "
        << realNum << " - " << imaginaryNum << "i." << endl;

    }

    // Indicate that the program ended successfully
    return 0;

} // End of function main
4

3 回答 3

0

首先,如果 a 很小,我们可以说平方项消失了。所以它变成了一条有一个根的直线(除非平行于 x 轴)。

然后,如果行列式为负,则方程不穿过 x 轴。所以没有根,或者,如果你愿意,根是虚构的。

要提高数值稳定性,请使用此功能。编写它是为了避免使用高中公式出现的接近 0/0 的术语。

int quadratic_roots(double a, double b, double c, double *out)
{
    double rootb2minus4ac;
    double b2minus4ac;

    if(fabs(a) < FLT_EPSILON * 0.01)
    {
        if(fabs(b) > FLT_EPSILON * 0.01)
        {
            out[0] = c/-b;
            return 1;
        }
        return 0;
    }

    b2minus4ac = b*b - 4*a*c;
    if(b2minus4ac > 0)
    {
        rootb2minus4ac = sqrt(b*b - 4*a*c);
        if(b >= 0)
        {
            out[0] = (-b -rootb2minus4ac)/(2*a);
            out[1] = (2*c)/ (-b -rootb2minus4ac);
        }
        else
        {
            out[0] = (2*c)/(-b + rootb2minus4ac);
            out[1] = (-b + rootb2minus4ac)/(2*a);
        }
        if(a < 0)
        {
            double temp = out[0];
            out[0] = out[1];
            out[1] = temp;
        }
        return 2;
    }
    else if(b2minus4ac == 0.0)
    {
        out[0] = (2*c)/-b;
        return 1;
    }
    return 0;
}
于 2017-02-06T10:54:19.113 回答
0

问题是你在这里除以0:

 imaginaryNum = (sqrt(-d)) / (2 * a);  

浮点数的行为由IEEE-754标准定义。它指出,如果您将一个数字除以 0,则该数字将表示为infinity。此外,除以零是C++ 中未定义的行为

这就是您要检查用户输入的原因。您可能不会输入无效数字,但用户可以很好地输入。此代码将是一个临时解决方法。请注意,这只是您问题的临时解决方案,因为仍会接受像“12a”这样的字符串:

#include <iostream>
#include <exception>

int main() {
    double input;
    bool is_valid = false;
    while (is_valid != true) {
        try {
            cin >> input; //
            if (!cin) {
                cin.clear(); // reset failbit
                cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                throw std::bad_exception();
            }
            else {
                is_valid = true;
            }
        }
        catch (...) {
            std::cout << "Input a number!\n";
        }
    }
    // ... Do this for all your inputs and proceed.
}
于 2017-02-06T10:02:17.833 回答
0

以下是代码中有问题的部分:

// Calculate each of the two possible roots for the quadratic
        root1 = (-b + sqrt(d)) / (2 * a);
        root2 = (-b - sqrt(d)) / (2 * a);
        //....
        // Calculate the real portion of the complex roots for the quadratic
        realNum = (-b) / (2 * a);

        // Calculate the imaginary portion of the complex roots for the quadratic
        imaginaryNum = (sqrt(-d)) / (2 * a);

可以清楚地看到,变量a在分母中。变量a可以具有任何值,包括零。除以零,可能导致C++ 中的未定义行为也正如ScY所指出的:

浮点数的行为由IEEE-754 标准定义。它指出,如果将一个数字除以 0,则该数字将表示为无穷大。

我的建议是检查a是否等于零。如果a等于 0,则抛出错误。

cout << "Please input a: " << setprecision(4) << fixed;
cin >> a;     
if (a == 0){
   std::cerr << "The variable a should not be equal with 0";
   return 1;
}

我强烈建议您创建一个函数来计算任何给定多项式的根。这可以使您的代码更具可读性和效率。此外, main() 函数并不是真正可重用的,所以它应该保持简短。

于 2017-02-06T15:14:38.300 回答