为什么这两个程序的输出在C中有所不同?

编程入门 行业动态 更新时间:2024-10-27 10:23:49
本文介绍了为什么这两个程序的输出在C中有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

#include<stdio.h> int main(){ float x = 0.1; if (x == 0.1) printf("IF"); else if (x == 0.1f) printf("ELSE IF"); else printf("ELSE"); return 0; }

#include<stdio.h> int main{ float x = 0.5; if (x == 0.5) printf("IF"); else if (x == 0.5f) printf("ELSE IF") else printf("ELSE"); return 0; }

第一节目输出:ELSE IF 第二节目输出:IF 两个程序都是相似的。但是输出是不同的。为什么?

First program Output: ELSE IF Second program Output: IF Both programs are similar.But outputs are different.Why?

推荐答案

这是由于浮点数和舍入的内部表示(见维基百科 [ ^ ])。 如果条件比较单精度( float )值具有双精度常量值。因此编译器将单精度值转换为双精度。 否则,如果条件与单个精度常量(应该始终为真)进行比较。 第二个值0.5可以用浮点值精确表示。但是0.1的情况并非如此。从单精度转换为双精度时,将清除其他可用位。但是,双常数值0.1设置了一些这些位。因此值不是二进制相同。 您可以在此页 [ ^ ]查看单精度数的二进制表示和相应的双精度十进制。 This is due to the internal representation of floating point numbers and rounding (see Wikipedia[^]). Your if conditions compares the single precision (float) value with a double precision constant value. So the compiler converts the single precision value to double precision. The else if condition compares with a single precision constant (which should always be true). The second value 0.5 can be represented exactly by floating point values. But this is not true for 0.1. When converting from single precision to double, the additional available bits will be cleared. But the double constant value 0.1 has some of these bits set. So the values are not binary identical. You may use the converter at this page[^] to see the binary representation of single precision numbers and the corresponding double precision decimal.

在表达式 x == 0.1 中0.1将创建为double而不是float,并将设置更多位。由于浮点数的0.1值无法准确表示,因此两个值会略有不同。请参见 docs.oracle/cd/E19957-01/806-3568 /ncg_goldberg.html [ ^ ]以获得正确的解释。 In the expression x == 0.1 the 0.1 will be created as a double rather than a float, and will have more bits set. And since the value of 0.1 in floating point cannot be represented exactly the two values will differ slightly. See docs.oracle/cd/E19957-01/806-3568/ncg_goldberg.html[^] for a proper explanation.

因为当你将它们视为二进制值时,浮点数并不总能正常工作。 0.5很容易转换为二进制,1/2总是会给出二进制是2的基础! 但是0.1是1/10,它不能很好地映射到基数2。 当你写0.1f时,你将它定义为单个精度浮点数,而不是你得到的双倍精度为0.1 所以当你写 Because floating point numbers don't always work well when you look at them as binary values. 0.5 is easy to convert to binary, 1/2 always will be given that binary is base 2! But 0.1 is 1/10 which doesn't "map" nicely to base 2. When you write 0.1f you are defining it as a single precision float, rather than the double precision you get for 0.1 So when you write float x = 0.1;

你实际得到的是

What you actually get is

double d = 0.1; float x = (float)d;

这实际上并不是相同的数字(即使它看起来像!)

which isn't actually the same number (even though it looks like it!)

更多推荐

为什么这两个程序的输出在C中有所不同?

本文发布于:2023-11-17 06:00:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这两个   有所不同   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!