简体   繁体   English

检查int与float中的相等性

[英]checking for equality between an int and float in C

I came across this piece of code : 我遇到了这段代码:

int x=3;
float y=3.0;
if(x==y)
  printf("x and y are equal");
else
  printf("x and y are not equal");

Why does this code print "x and y are equal"?? 为什么此代码显示“ x和y相等”? Here if y=3.1(say), then the code prints "x and y are not equal". 在这里,如果y = 3.1(例如),则代码显示“ x和y不相等”。 Someone please explain how is this happening. 有人请解释这是怎么回事。

Comparisons between arithmetic types are subject to the so-called usual arithmetic conversions (§5/9, §5.9/2, §5.10/1). 算术类型之间的比较受制于通常的算术转换 (第5/9,第5.9 / 2,第5.10 / 1节)。 Emphasis mine. 强调我的。

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. 许多期望算术或枚举类型的操作数的二进制运算符都以类似的方式引起转换并产生结果类型。 The purpose is to yield a common type, which is also the type of the result. 目的是产生一个通用类型,它也是结果的类型。 This pattern is called the usual arithmetic conversions, which are defined as follows: 这种模式称为通常的算术转换,其定义如下:

— If either operand is of scoped enumeration type (7.2), no conversions are performed; —如果任何一个操作数都属于范围枚举类型(7.2),则不执行任何转换;否则,将不执行任何转换。 if the other operand does not have the same type, the expression is ill-formed. 如果另一个操作数不具有相同的类型,则表达式格式错误。

— If either operand is of type long double , the other shall be converted to long double . —如果一个操作数的类型为long double ,则另一个应转换为long double

— Otherwise, if either operand is double , the other shall be converted to double . —否则,如果其中一个操作数为double ,则另一个应转换为double

Otherwise, if either operand is float , the other shall be converted to float . 否则,如果其中一个操作数为float ,则另一个应转换为float

— Otherwise, the integral promotions (4.5) shall be performed on both operands. —否则,应在两个操作数上执行积分提升(4.5)。 Then the following rules shall be applied to the promoted operands: 然后,将以下规则应用于提升后的操作数:

— If both operands have the same type, no further conversion is needed. —如果两个操作数具有相同的类型,则无需进一步转换。

— Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank. —否则,如果两个操作数都具有符号整数类型或都具有无符号整数类型,则应将具有较小整数转换等级的操作数转换为具有较大等级的操作数的类型。

— Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type. —否则,如果具有无符号整数类型的操作数的秩大于或等于另一个操作数的类型的秩,则应将具有符号整数类型的操作数转换为具有无符号整数类型的操作数的类型。

— Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, the operand with unsigned integer type shall be converted to the type of the operand with signed integer type. —否则,如果带符号整数类型的操作数的类型可以表示无符号整数类型的操作数的所有值,则应将无符号整数类型的操作数转换为带符号整数类型的操作数的类型。

— Otherwise, both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type. —否则,两个操作数均应转换为与带符号整数类型的操作数类型相对应的无符号整数类型。

When you try to compare an int with a float, the int gets converted to float first. 当您尝试将一个int与一个float进行比较时,该int会首先转换为float。

So 3 == 3.0f actually tests float(3) == 3.0f , which is true. 所以3 == 3.0f实际上测试了float(3) == 3.0f ,这是真的。

On the other hand, 3 == 3.1f tests float(3) == 3.1f , which is false. 另一方面, 3 == 3.1f测试float(3) == 3.1f ,这是错误的。

在这个问题中x是一个整数值,而y是一个浮点值...一个整数值和一个浮点值不能相等...所以为了检查相等性,我们必须将浮点型转换为整数,反之亦然...希望这可能有助于清除您的概念

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM