c 中的浮点数 [Xcode IDE]

发布于 2024-11-03 03:58:48 字数 623 浏览 0 评论 0原文

我已经开始学习c语言,但问题是我对如何使用浮点数感到困惑。我使用 Xcode 作为 IDE。以下是让我困惑的结果:

float x1 = 1.123456789123456789f;  
double x2 = 1.123456789123456789f;  
float x3 = 987654321.123456789f;  
double x4 = 987654321.123456789f;  

printf("x1 = %.20f\n", x1);  
printf("x2 = %.20f\n", x2);  
printf("x3 = %10.10f\n", x3);  
printf("x4 = %10.10f\n", x4);  

输出是:

x1 = 1.12345683574676513672  
x2 = 1.12345683574676513672  
x3 = 987654336.0000000000  
x4 = 987654336.0000000000  

问题是,为什么x1x21.12345678之后丢失了它们的浮点数字?为什么x3x4被截断?

I have started to learn c language but the problem is that I'm confused about how to use float numbers. I am using Xcode as IDE. Here are the results that got me confused:

float x1 = 1.123456789123456789f;  
double x2 = 1.123456789123456789f;  
float x3 = 987654321.123456789f;  
double x4 = 987654321.123456789f;  

printf("x1 = %.20f\n", x1);  
printf("x2 = %.20f\n", x2);  
printf("x3 = %10.10f\n", x3);  
printf("x4 = %10.10f\n", x4);  

The output is:

x1 = 1.12345683574676513672  
x2 = 1.12345683574676513672  
x3 = 987654336.0000000000  
x4 = 987654336.0000000000  

The question is, why x1, x2 lose their float digits after 1.12345678? And why x3 and x4 are truncated?

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

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

发布评论

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

评论(2

凡间太子 2024-11-10 03:58:48

这里的基本问题是浮点数的精度有限:它们只能表示那么多在空间耗尽之前保留有效数字。这些数字是在小数点之前还是之后并不重要,重要的是有效数字的总数。请注意,此问题仅限于 C,它可以在所有使用浮点数的语言/环境中出现。

double 应该能够存储大约是 float 两倍的数字,但所有浮点文字都是 float 文字。删除 double 行上的 f 后缀,以实际使用 double 可用的所有位。

The basic problem here is that floating point numbers only have a limited precision: they can only represent that many significant digits before they run out of space. It does not matter if those digits are before the decimal point or after it, the total number of significant digits matters. Note that this issue is not restricted to C, it can be seen in all languages/environments that use floating point numbers.

double should be able to store about twice as many digits as float, but all your floating point literals are float literals. Remove the f postfix on the double lines to actually use all the bits available for a double.

梅窗月明清似水 2024-11-10 03:58:48

您缺少的是,您将每个数字都经过 2 次基本转换,并且 x2 和 x4 也经过了扩展类型转换。

首先,你有十进制文字,编译器将其转换为 float 类型的二进制分数,其精度为 23 位二进制数字(相当于大约 7.2 位十进制数字),无法准确表示大多数十进制分数,即使是符合其精度的那些。然后 x2 和 x4 被分配给 double 变量,但由于所有不适合 23 个二进制数字的内容都已被裁剪,因此它们不会神奇地重新获得文字中存在的精度。

然后,通过 printf 将二进制分数转换回十进制,并得到前面提到的 ca。 7.2 位十进制数字正确,此后的所有内容都反映了基数转换产生的代表性舍入误差。

这基本上与您尝试将 1/3 转换为小数 0.333 再转换回真分数 333/1000 相同 - 嘿,为什么不是 1/3?

阅读浮点指南了解更多信息。

What you're missing is that you're putting each of your numbers through 2 base conversions, and x2 and x4 also through a widening type conversion.

First of all you have decimal literals, which the compiler converts to binary fractions of type float, which have a precision of 23 binary digits (equivalent to about 7.2 decimal digits) and cannot accurately represent most decimal fractions, even those that fit into their precision. Then x2 and x4 are assigned to double variables, but since everything that does not fit into 23 binary digits has already been cropped, they don't magically regain the precision that was present in the literals.

Then, you convert the binary fractions back to decimal via printf, and get the previously mention ca. 7.2 decimal digits correct, and everything after that reflects the representational rounding error created by the base conversion.

It's basically the same as if you try to convert 1/3 to a decimal fraction 0.333 and back to a proper fraction 333/1000 - hey, why isn't it 1/3?

Read the floating-point guide for more information.

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