2012年(158)
分类: C/C++
2012-11-23 16:50:55
#include
int main()
{
int a = +1.0E20; // to hpho:
类似这样的编译期时立即数溢出,标准中有没有什么规定?
int b =
-1.0E20;
cout << "0x" << hex
<< a << '(' << dec << a
<< ')' << endl;
cout <<
"0x" << hex << b << '(' << dec << b << ')' << endl;
return 0;
}
/*
gcc3.4.5输出
0x7fffffff(2147483647)
0x80000000(-2147483648)
VC2005输出
0x63100000(1661992960)
0x9cf00000(-1661992960)
*/
----------------------------------------------------------------------------------
以下在gcc3.4.5中测试:
#include
using
namespace std;
int main()
{
double x = 1.0E20;
double
y = -1.0E20;
int a1 = x;
// 0x80000000(-2147483648)
int b1 = 1.0E20; //
0x7fffffff(2147483647)
unsigned int c1 = x;
// 0x0(0)
unsigned
int d1 = 1.0E20; //
0xffffffff(4294967295)
int a2 = y;
// 0x80000000(-2147483648)
int b2 = -1.0E20; //
0x80000000(-2147483648)
unsigned int c2 = y;
// 0x0(0)
unsigned
int d2 = -1.0E20; // 0x0(0)
cout << "0x" << hex
<< a1 << '(' << dec << a1
<< ')' << endl;
cout <<
"0x" << hex << b1 << '(' << dec << b1 << ')' << endl;
cout << "0x" << hex << c1 << '(' << dec << c1 << ')' << endl;
cout << "0x" << hex << d1 << '(' << dec << d1 << ')' << endl;
cout << "0x" << hex
<< a2 << '(' << dec << a2
<< ')' << endl;
cout <<
"0x" << hex << b2 << '(' << dec << b2 << ')' << endl;
cout << "0x" << hex << c2 << '(' << dec << c2 << ')' << endl;
cout << "0x" << hex << d2 << '(' << dec << d2 << ')' << endl;
return 0;
}
网友评论2012-11-23 16:51:35
100000
不知道这句话和星星说的错误有关系没有:
If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable
values for its type, the behavior is undefined, unless such an expression is a constant expression
(5.19), in which case the program is ill-formed.