简体   繁体   English

使用比较运算符比较具有相同值的int和float变量时会发生什么?

[英]What happens when an int and float variable both having same value is compared using comparison operator?

What happens when an int and float variable both having same value is compared using comparison operator? 使用比较运算符比较具有相同值的int和float变量时会发生什么?

 main( )
  {
    int x = 3 ;
    float y = 3.0 ;
    if ( x == y )
      printf ( "\nx and y are equal" ) ;
    else
      printf ( "\nx and y are not equal" ) ;
   }




output : x and y are equal
What happens when x is compared with y variable?

The int is converted implicitly to a float type; int 隐式转换为float类型; your code is equivalent to 您的代码等同于

if ((float)x == y)

Note this happens even if the conversion from an int to a float loses precision (which it doesn't in your case). 请注意, 即使intfloat的转换失去精度(在您的情况下也不是), 也会发生这种情况。

A good, general rule is that you should never compare floating-point numbers for exact equality. 一条好的通用规则是,绝对不要比较浮点数是否完全相等。 Famously, simple-looking code fragments like 著名的是,简单的代码片段,例如

float f1 = 1.1, f2 = 2.2;
float f3 = 3.3;
if(f1 + f2 == f3) printf("expected\n");
else printf("surprise\n");

are quite likely to behave surprisingly. 很有可能表现出令人惊讶的行为。 The reason is that most real numbers (including ordinary decimal fractions like 1.1 ) can not be represented exactly in a finite-width floating-point representation. 原因是大多数实数(包括普通的十进制小数,如1.1不能精确地用有限宽度的浮点表示形式表示。

Some numbers can be represented exactly, of course. 当然,某些数字可以准确表示。 In particular, small integers can typically be represented exactly even in low-precision floating-point representations. 特别是,即使在低精度浮点表示中,小整数通常也可以准确表示。 So to restate your example, code like 因此,为了重申您的示例,代码如下

int x = 3;
float y = 3.0;
if(x == y) printf("expected\n");
else printf("surprise\n");

is virtually guaranteed to print the expected result on any practical computer. 实际上可以保证在任何实际计算机上打印出预期的结果。

How does it work, exactly? 究竟如何运作? C supports mixed-mode arithmetic . C支持混合模式算术 You're perfectly allowed to write things like x == y even if x and y have different types. 即使xy具有不同的类型,也完全可以编写x == y类的东西。 What typically happens is that the compiler inserts implicit conversions to a common type. 通常发生的情况是,编译器将隐式转换插入到通用类型。 So when you write x == y , the compiler says to itself. 因此,当您编写x == y ,编译器会对自己说。 "Hmm. I don't have an instruction to compare an int and a float for equality. But if I convert the int to a float, I'll have two floats, and then I can use the machine's single-precision floating-point equality comparison operator. So I'll pretend the expression was (float)x == y ." “嗯。我没有指令比较int和float的相等性。但是如果将int转换为float,我将有两个float,然后可以使用机器的单精度浮点相等比较运算符。因此,我假设表达式为(float)x == y 。”

But the general rule still stands. 但是总的规则仍然存在。 The fact that a floating-point variable can exactly store a small integer like 3.0 (or an exact fraction like 0.5 ) does not mean that it can store all integers exactly. 浮点变量可以完全存储一个小整数(如3.0 (或一个精确的分数(如0.5 ))的事实并不意味着它可以完全存储所有整数。 For example, the similar-looking code 例如,看起来相似的代码

long int x = 123456789;
float y = 123456789.0;
if(x == (long int)y) printf("expected\n");
else printf("surprise\n");

is again quite likely to print a surprising result. 再次很有可能打印出令人惊讶的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将int转换为float时在后台会发生什么 - what happens at background when convert int to float 当我使用可变参数时,它可以很好地与int和double配合使用,但是当涉及浮动时,会发生错误 - When I using variable parameters, it works well with int and double, but when it comes to float, error happens 当浮点数变量超出浮动限制时,会发生什么? - When a float variable goes out of the float limits, what happens? 当我们将返回类型为void的函数调用返回值分配给int变量时会发生什么? - What happens when we assign a function call return value with return type void to a int variable? 当printf中使用%d时,float变量会发生什么? - What happens to a float variable when %d is used in a printf? 如果您将一个大整数转换为浮点数会发生什么 - what happens if you cast a big int to float 使用运算符“的结果是什么? :”表示int值 - what is the result using operator “? :” for int value 如果我为 int 指针分配一个浮点变量地址会发生什么,反之亦然? - What happens if I assign to an int pointer a float variable address and vice-versa? 当我们超过 Int8_t 的范围值时会发生什么 - What happens when we exceed the range value of Int8_t 将一个浮点变量与其包含的值进行比较 - Comparison of one float variable to its contained value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM