c [Xcode IDE] 中的浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5754900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:52:46  来源:igfitidea点击:

float numbers in c [Xcode IDE]

xcodefloating-pointnumbersdouble

提问by Centurion

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:

我已经开始学习 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);  

The output is:

输出是:

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

The question is, why x1, x2lose their float digits after 1.12345678? And why x3and x4are truncated?

现在的问题是,为什么x1x2之后失去了浮动的数字1.12345678?为什么x3x4被截断?

回答by Joachim Sauer

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

这里的基本问题是浮点数只有有限的精度:它们在空间用完之前只能表示那么多有效数字。它并没有问题,如果这些数字是在小数点之前或之后的显著数字事项的总数。请注意,此问题不仅限于 C,在所有使用浮点数的语言/环境中都可以看到。

doubleshould be able to store about twice as many digits as float, but all your floating point literals are floatliterals. Remove the fpostfix on the doublelines to actually use all the bits available for a double.

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

回答by Michael Borgwardt

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.

您缺少的是,您将每个数字都通过 2 次基本转换,而 x2 和 x4 也通过扩大类型转换。

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 doublevariables, 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.

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

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.

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

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?

这与您尝试将 1/3 转换为十进制分数 0.333 并返回到适当的分数 333/1000 基本相同 - 嘿,为什么不是 1/3?

Read the floating-point guidefor more information.

阅读浮点指南以获取更多信息。