根据头文件的 C 的 pow() 函数无法正常工作

发布于 2024-09-15 00:00:51 字数 952 浏览 4 评论 0原文

我看到下面的代码产生如下结果,知道为什么输出是这样的吗?

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

int main() {
    int i = 0;
    for(i = 0; i < 10; i++) {
        printf("%d\t\t\t%d\t\t\t", i, (int)pow(10, i));    
        printf("%f\n", pow(10, i));
    }
    return 0;
}

输出:

0                       1                       1.000000

1                       10                      10.000000

2                       99                      100.000000

3                       1000                    1000.000000

4                       9999                    10000.000000

5                       100000                  100000.000000

6                       1000000                 1000000.000000

7                       9999999                 10000000.000000

8                       99999999                100000000.000000

9                       999999999               1000000000.000000

I see that for the following code produces the result as below, any idea on why is the output like this?

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

int main() {
    int i = 0;
    for(i = 0; i < 10; i++) {
        printf("%d\t\t\t%d\t\t\t", i, (int)pow(10, i));    
        printf("%f\n", pow(10, i));
    }
    return 0;
}

Outputs:

0                       1                       1.000000

1                       10                      10.000000

2                       99                      100.000000

3                       1000                    1000.000000

4                       9999                    10000.000000

5                       100000                  100000.000000

6                       1000000                 1000000.000000

7                       9999999                 10000000.000000

8                       99999999                100000000.000000

9                       999999999               1000000000.000000

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

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

发布评论

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

评论(2

别念他 2024-09-22 00:01:34

与浮点相关的十亿个其他问题中的每一个一样:-),答案是并非所有数字都可以准确表示。

而且,即使它们可以被精确地表示,您也可能会从像pow这样可能在循环中工作的东西得到一个糟糕的结果。换句话说,无论一个操作的误差有多小,如果执行多次,误差就会变大。

尝试使用 %.50f 或其他浮点说明符打印 pow(10,4) 的返回值,我保证您会看到类似 9999.9999999237623465 而不是 10000

无论如何,正确的结果是在 CygWin 下的 gcc 3.4.4 下生成的,因此您可能使用的是不太精确的 pow 实现。

As with every one of the billion other questions on SO to do with floating point :-), the answer is that not all numbers can be represented exactly.

And, even if they can be represented exacly, you may get a bad result from something like pow which may work within a loop. In other words, no matter how small the error on an operation, if you do it a lot of times, the error becomes large.

Try printing out the return from pow(10,4) with a %.50f or other floating point specifier, and I'll guarantee you'll see something like 9999.9999999237623465 instead of 10000.

For what it's worth, the correct results are produced under gcc 3.4.4 under CygWin so it's likely that you're using a less-exact implementation of pow.

缪败 2024-09-22 00:01:31

在转换为 int 之前使用 round() 函数,否则尾随数字将被截断。

Use the round() function before casting to int, else the trailing digits will just get truncated.

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