- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Citardauq 公式计算二次方程的根,这是一种在数值上更稳定的计算这些根的方法。但是,例如,当我输入方程 x^2+200x-0.000002=0 时,该程序不会精确计算根。为什么?我在代码中没有发现任何错误,并且此处不应发生灾难性取消。
您可以了解 Citardauq 公式为何有效 here (第二个答案)。
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, determinant;
double root1, root2;
printf("Introduce coefficients a b and c:\n");
scanf("%lf %lf %lf", &a, &b, &c);
determinant = b * b - 4 * a * c;
if (0 > determinant)
{
printf("The equation has no real solution\n");
return 0;
}
if (b > 0)
{
root1 = (-b - sqrt(determinant)) / (2 * a);
root2 = (c / (a * root1));
printf("The solutions are %.16lf and %.16lf\n", root1, root2);
}
else if (b < 0)
{
root1 = (-b + sqrt(determinant)) / (2 * a);
root2 = (c / (a * root1));
printf("The solutions are %.16lf and %.16lf\n", root1, root2);
}
}
最佳答案
欢迎来到数值计算。这里有几个问题:
1) 正如some-programmer-dude
指出的那样, float 的精确表示存在问题 Is floating point math broken?
For 0.1 in the standard binary64 format, the representation can be written exactly as 0.1000000000000000055511151231257827021181583404541015625
2) double (double) 仅提供 52 位有效位、11 位指数位和 1 位符号位。C 中的 float 使用 IEEE 754 编码。
3) sqrt
精度也受到限制。
您可以看到,从精度的角度来看,这不是一个简单的方程。
On line calculator 1给出的解决方案为:
1.0000007932831068e-8 -200.00000001
你的程序更好:
Introduce coefficients a b and c:
1
200
-0.000002
The solutions are -200.0000000100000079 i 0.0000000100000000
所以根之一是-200.000000010000。忘记其余的数字。 这正是人们所期望的,因为 double 有 15
小数 精度位数!
关于Citardauq 公式无法精确发挥作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49096957/
在 Play 1.x 中有很棒的 play Idealize(和 play eclipsify),它为您最喜欢的 IDE 中的 Play 项目准备了项目文件。 我看到这是在 Play 2.X 中删除的
我是一名优秀的程序员,十分优秀!