c - C 中的二次函数

标签 c quadratic

我正在尝试用 C 构建一个小型二次计算器,但无论我输入什么,我都会得到以下结果....

enter image description here

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {
float a, b, c;

printf("Enter a:");
scanf("%f", &a);
printf("Enter b:");
scanf("%f", &b);
printf("Enter c:");
scanf("%f", &c);

float discriminant = sqrt(b*b - 4*a*c);
float root1 = (-b + discriminant) / (2*a);
float root2 = (-b - discriminant) / (2*a);

printf("Root 1: %f\n", root1);
printf("Root 2: %f\n", root2);
}

最佳答案

浮点判别 = sqrt(b*b - 4*a*c)

这条线有潜在的危险,因为它有时会计算负数的平方根。如果您希望能够处理复杂的根,则必须为此定制构建解决方案。

你可以用它来改变的是验证输入不会导致它为负数。

关于c - C 中的二次函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48939964/

相关文章:

javascript - 四面二维形状的分割

C++ + glut + OpenGL + gluSphere 不绘制任何东西

algorithm - 二次函数的渐近紧界,再访

c - C 中显示虚数的类型

c - 如果多个 pthread 使用相同的函数会发生什么

android - 在宏中连接格式化字符串

c - 从函数传递一 block 内存

python - 求解 3 个变量的隐式二次系统

c - 在 C 中使用套接字的 HTTP 请求

c++ - 一种保护通过网络发送的数据的方法?