在C中检查int和float之间的相等性 [英] checking for equality between an int and float in C

查看:811
本文介绍了在C中检查int和float之间的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int x = 3; 
float y = 3.0;
if(x == y)
printf(x and y are equal);
else
printf(x和y不相等);

为什么这个代码打印出x和y相等?
这里如果y = 3.1(比如说),那么代码打印出x和y不相等。
请解释这是怎么发生的。

解决方案

算术类型之间的比较取决于所谓的通常的算术转换(§5/ 9,§5.9/ 2,§5.10/ 1)。强调我的。


许多期望算术或枚举类型的操作数的二元运算符会导致转换并以类似的方式产生
结果类型。目的是产生一个共同的类型,这也是结果的类型。
这种模式称为通常的算术转换,定义如下:

- 如果任一操作数是范围枚举类型(7.2),则不进行转换执行;如果另一个
操作数不具有相同的类型,则表达式格式不正确。



- 如果任一操作数的类型为长双,另一个应转换为 long double

- 否则,如果其中一个操作数是 double ,另一个将被转换为 double



- 否则,如果其中一个操作数是 float ,另一个应转换为 float

- 否则,积分促销(4.5)应在两个操作数上执行。然后,下面的
规则将被应用到提升的操作数:

lockquote

- 如果两个操作数具有相同的类型,否则,如果两个操作数都是有符号整数类型,或者都有无符号整数类型,则
操作数的类型应该是整数转换级别被转换为更高级别的
操作数的类型。

- 否则,如果具有无符号整数类型的操作数的rank大于或等于另一个操作数类型的
等级,带符号整数类型的操作数将转换为
无符号整数类型的操作数类型。

<否则,如果具有符号整数类型的操作数的类型可以表示具有无符号整数类型的操作数的类型
的所有值,则具有无符号整数类型的操作数应该为
转化使用有符号整数类型操作数的类型。

- 否则,两个操作数都将被转换为与
类型相对应的无符号整数类型操作数与有符号整数类型。



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"?? Here if y=3.1(say), then the code prints "x and y are not equal". 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). 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; 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.

— Otherwise, if either operand is double, the other shall be converted to double.

Otherwise, if either operand is float, the other shall be converted to float.

— Otherwise, the integral promotions (4.5) shall be performed on both operands. 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.

这篇关于在C中检查int和float之间的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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