C++二次方程输出:-1.#IND

发布于 2024-12-09 13:57:00 字数 608 浏览 2 评论 0原文

我的代码工作正常: (1, -2, -8),当我输入 a=1 b=0 c=1 时,它给我上面的错误,

这是我的代码:

    double x=0,a=0,b=0,c=0,d=0;
    complexType solu1;
    complexType solu2;


    cout << "\n\nEnter values of quadratic a,b,c:";
    cin >> a >> b >> c;

    double solution1 = (-1.0 * b) + (sqrt((b * b) - (4 * a * c)));
    solu1 = solution1 / (2*a);

    cout << setprecision(5) << solu1;

    double solution2 = (-b) - (sqrt((b*b) - (4 * a * c)));
    solu2 = solution2 / (2*a);
    cout << setw(5) << setprecision(5)  << solu2;

我该如何解决这个问题?

My code works fine with: (1, -2, -8), It gives me the error above when I input a=1 b=0 c=1,

Here is my code:

    double x=0,a=0,b=0,c=0,d=0;
    complexType solu1;
    complexType solu2;


    cout << "\n\nEnter values of quadratic a,b,c:";
    cin >> a >> b >> c;

    double solution1 = (-1.0 * b) + (sqrt((b * b) - (4 * a * c)));
    solu1 = solution1 / (2*a);

    cout << setprecision(5) << solu1;

    double solution2 = (-b) - (sqrt((b*b) - (4 * a * c)));
    solu2 = solution2 / (2*a);
    cout << setw(5) << setprecision(5)  << solu2;

How can I remedy this?

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

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

发布评论

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

评论(2

靑春怀旧 2024-12-16 13:57:00

您正在尝试找到 x^2 + 1 = 0 的实数解,该解只有虚数解。

如果您想求解所有二次方程,那么您需要检查判别式是否为正,以确定解是实数还是复数:

double d = b*b - 4*a*c;
if (d >= 0) {
    double sol1 = (-b + sqrt(d))/(2*a);
    double sol2 = (-b - sqrt(d))/(2*a);
    std::cout << sol1 << ", " << sol2 << '\n';
} else {
    double real = -b/(2*a);
    double imag = sqrt(-d)/(2*a);
    std::cout << real << " +/- " << imag << "i\n";
}

如果您愿意,您可以使用 std::complex 做一些更简洁的事情。

You are trying to find a real solution to x^2 + 1 = 0, which only has imaginary solutions.

If you want to solve all quadratic equations, then you need to check whether the discriminant is positive to determine whether the solutions are real or complex:

double d = b*b - 4*a*c;
if (d >= 0) {
    double sol1 = (-b + sqrt(d))/(2*a);
    double sol2 = (-b - sqrt(d))/(2*a);
    std::cout << sol1 << ", " << sol2 << '\n';
} else {
    double real = -b/(2*a);
    double imag = sqrt(-d)/(2*a);
    std::cout << real << " +/- " << imag << "i\n";
}

You could do something neater with std::complex if you want.

绮筵 2024-12-16 13:57:00

输入的 sqrt((b*b) - (4 * a * c))sqrt(-4)。根据 http://www.cplusplus.com/reference/clibrary/cmath/sqrt/ , 如果参数为负数,则会发生域错误,将全局变量 errno 设置为值 EDOM。 在这种情况下,我没有看到它返回的内容的定义。不管怎样,这是错误的。

我发现您的代码中有 complexType 。如果这是 std::complex 的 typedef,那么代码很容易修复。

complexType top = b*b - 4*a*c;
solu1 = (-b + sqrt(top)) / (2*a);
solu2 = (-b - sqrt(top)) / (2*a);

由于 std::sqrt 具有 std::complex 的重载。

sqrt((b*b) - (4 * a * c)) for your inputs is sqrt(-4). According to http://www.cplusplus.com/reference/clibrary/cmath/sqrt/, If the argument is negative, a domain error occurs, setting the global variable errno to the value EDOM. I don't see a definition for what it returns in that case. Either way, that's wrong.

I see that you have complexType in your code. If that's a typedef of std::complex<T>, then the code is easily fixable.

complexType top = b*b - 4*a*c;
solu1 = (-b + sqrt(top)) / (2*a);
solu2 = (-b - sqrt(top)) / (2*a);

Since std::sqrt has an overload for std::complex<T>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文