C中的隐式类型转换

我在维基百科( http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion)上偶然发现了以下示例.
#include <stdio.h>

int main()
{
    int i_value   = 16777217;
    float f_value = 16777217.0;
    printf("The integer is: %i\n",i_value); // 16777217
    printf("The float is:   %f\n",f_value); // 16777216.000000
    printf("Their equality: %i\n",i_value == f_value); // result is 0
}

他们的解释是:“这种奇怪的行为是由于与f_value进行比较时隐式转换为i_value引起的浮动;一个失去精度的转换,使得比较的值不同.”

这不对吗?如果i_value被转换为浮动,那么两者都会有相同的精度损失并且它们是相等的.
所以i_value必须加倍.

解决方法

不,在等于运算符的情况下,会发生“通常的算术转换”,它开始于:
  • First,if the corresponding real type of either operand is long double,the other operand is converted,without change of type
    domain,to a type whose corresponding real type is long double.
  • Otherwise,if the corresponding real type of either operand is double,to a type whose corresponding real type is double.
  • Otherwise,if the corresponding real type of either operand is float,to a type whose corresponding real type is float.

最后一种情况适用于此:i_value转换为float.

尽管如此,你可以从比较中看到一个奇怪的结果的原因是因为对通常的算术转换的这个警告:

The values of floating operands and of the results of floating
expressions may be represented in greater precision and range than
that required by the type; the types are not changed thereby.

这就是发生的事情:转换后的i_value的类型仍然是浮点数,但在此表达式中,编译器利用此纬度并以比float更精确的方式表示它.这是编译387兼容浮点时的典型编译器行为,因为编译器在浮点堆栈上保留临时值,浮点堆栈以80位扩展精度格式存储浮点数.

如果编译器是gcc,则可以通过提供-ffloat-store命令行选项来禁用此额外精度.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...