一个有趣的问题

代码如下:

#include "stdafx.h"
#include <iostream>
#include <float.h>

int _tmain(int argc, _TCHAR* argv[])
{
	_controlfp(_MCW_PC, _PC_24);

	volatile unsigned long dw = 0x80000052;

	unsigned long di = 0;

	std::cin >> di;
	dw += di;

	double d1 = dw;
	double d2 = (int)dw;

	volatile unsigned long dw1 = (unsigned long)d1;
	volatile unsigned long dw2 = (unsigned long)d2;

	printf("%.8x %.8xn", dw1, dw2);

	return 0;
}

 

这段代码乍看没什么问题,只是将一个无符号的数字转换为双精度,再转换回无符号整型,但是运行Debug版和Release版会发现结果完全不同,查看汇编代码如下

继续阅读