为什么这两个程序的输出在C中有所不同? [英] Why is the output of these two programs different in C?

查看:97
本文介绍了为什么这两个程序的输出在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?

推荐答案

这是由于浮点数和舍入的内部表示(见< a href =https://en.wikipedia.org/wiki/Floating_point>维基百科 [ ^ ])。



如果条件比较单精度( 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值无法准确表示,因此两个值会略有不同。请参见 http://docs.oracle.com/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 http://docs.oracle.com/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中有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆