Chinaunix首页 | 论坛 | 博客
  • 博客访问: 850942
  • 博文数量: 158
  • 博客积分: 4380
  • 博客等级: 上校
  • 技术积分: 2367
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-21 10:45
文章分类

全部博文(158)

文章存档

2012年(158)

我的朋友

分类: C/C++

2012-11-23 16:50:55

#include
using namespace std;

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;
}

阅读(1079) | 评论(2) | 转发(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.

网友评论2012-11-23 16:51:28

ricardo
老大,你的这些问题,都是怎么发现的啊,是会程序的bug中吗,觉得你真牛B