双浮点数?计算错误,使用 double

发布于 2024-10-10 22:06:13 字数 519 浏览 0 评论 0原文

非常有趣的事情。找到方程结果的最简单的源代码:ax + b = 0。我想知道,当 double x = -13/12 = -1.000000; 时和 双 x = -12/13= 0.000000; (对于 a 和 b int(类型转换)或 double),但 float 或 double x = -13f/12f = 1.083333 (没错)。 双倍有什么问题?

如果 {int a,b;},方程 {double 或 float x = -((double 或 float)b)/a;} 正确吗?如果它不可能是正确的——为什么?

int main()
{
 double a, b, x; 
 scanf("%f %f",&a, &b);
 fflush(stdin);
 if(a!=0)
 {x = -b/a; printf("x = %f", x);}
 else printf("There is no solve in your equation.");
 getchar();
 return 0;
}

谢谢

Very interesting thing. The simplest source code to find the result of equation: ax + b = 0. I was wonder, when double x = -13/12 = -1.000000; and
double x = -12/13= 0.000000; (for a and b int(type converted) or double), but float or double x = -13f/12f = 1.083333 (that's right).
What wrong with double?

and can the equation {double or float x = -((double or float)b)/a;} be right if {int a,b;}?? if it cannot be right - why?

int main()
{
 double a, b, x; 
 scanf("%f %f",&a, &b);
 fflush(stdin);
 if(a!=0)
 {x = -b/a; printf("x = %f", x);}
 else printf("There is no solve in your equation.");
 getchar();
 return 0;
}

thank you

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

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

发布评论

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

评论(2

睫毛溺水了 2024-10-17 22:06:13

在C中,如果你编写

double x = 13/12;

编译器不会将除法视为浮点除法。相反,它将用整数进行除法,截断结果并将其存储在双精度型中。更一般地说,C 在确定要执行什么类型的算术时不会查看正在写入的变量的类型。它只查看操作数的类型。

要解决此问题,请编写

double x = 13.0/12.0;

这些文字的类型为 double,因此除法将正确工作。

In C, if you write

double x = 13/12;

The compiler will not treat the division as floating point division. Rather, it will do the division with integers, truncate the result, and store it in a double. More generally, C doesn't look at the type of the variable it's writing to when determining what type of arithmetic to do. It just looks at the type of the operands.

To fix this, write

double x = 13.0/12.0;

These literals have type double, so the division will work correctly.

蘸点软妹酱 2024-10-17 22:06:13

您的 scanf 类型错误:
它们应该是 %lf 而不是 %f

#include <stdio.h>

int main()
{
    double a, b, x;
    scanf("%lf %lf",&a, &b);
    fflush(stdin);
    if(a!=0)
    {
        x = -b/a;
        printf("x = %lf", x);
    }
    else
        printf("There is no solve in your equation.");
    return 0;
}

> g++ t.cpp
> ./a.out
12
13
x = -1.083333
> ./a.out
13
12
x = -0.923077

PS。学习编写漂亮的代码。
上面的内容太可怕了,没有人愿意阅读这样的劣质代码。

You have the scanf types wrong:
They should be %lf NOT %f

#include <stdio.h>

int main()
{
    double a, b, x;
    scanf("%lf %lf",&a, &b);
    fflush(stdin);
    if(a!=0)
    {
        x = -b/a;
        printf("x = %lf", x);
    }
    else
        printf("There is no solve in your equation.");
    return 0;
}

> g++ t.cpp
> ./a.out
12
13
x = -1.083333
> ./a.out
13
12
x = -0.923077

PS. Learn to write nice code.
The above is horrible and nobody wants to read shoddy code like that.

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