【发布时间】:2011-08-10 04:20:33
【问题描述】:
为什么以下代码的行为与在 C 中的行为相同?
float x = 2147483647; //2^31
printf("%f\n", x); //Outputs 2147483648
这是我的思考过程:
2147483647 = 0 1001 1101 1111 1111 1111 1111 1111 111
(0.11111111111111111111111)base2 = (1-(0.5)^23)base10
=> (1.11111111111111111111111)base2 = (1 + 1-(0.5)^23)base10 = (1.99999988)base10
因此,要将 IEEE 754 表示法转换回十进制:1.99999988 * 2^30 = 2147483520
所以从技术上讲,C 程序一定打印出了 2147483520,对吧?
【问题讨论】:
标签: c floating-point floating-accuracy ieee-754