C/C++,用0x3f3f3f3f表示无穷大,0xc0c0c0c0表示无穷小

#include 

#define MaxN  0x3f3f3f3f
#define MinN  0xc0c0c0c0

int main(void)
{
    printf("%d %d\n",MaxN,MinN);
    //1061109567, -1061109568
    return 0;
}
#include 
#include 

#define MaxN  0x3f3f3f3f
#define MinN  0xc0c0c0c0

int main(void)
{
    int b=MinN;
    printf("%d %d %d\n\n",MaxN,MinN,b);
    //1061109567 -1061109568 -1061109568

    int a1 = MaxN;
    int b1= MinN;
    unsigned int a2 = MaxN;
    unsigned int b2 = MinN;
    long long a3 = MaxN;
    long long b3 = MinN;

    printf("a1 = %d  b1 = %d\n",a1,b1);
    printf("a2 = %d  b2 = %d\n",a2,b2);
    //注意 b3=%lld,两个l,否则,用%d或%ld都会导致其他都可能错误
    printf("a3 = %lld  b3 = %lld\n\n",a3,b3);
    //a1 = 1061109567  b1 = -1061109568
    //a2 = 1061109567  b2 = -1061109568
    //a3 = 1061109567  b3 = 3233857728  

    //看这里,b1=0. b3=-1061109568,就因为a3用的是%ld%
    printf("a1 = %d  a2 = %d  a3 = %ld\nb1 = %d  b2 = %d  b3 = %ld\n\n",a1,a2,a3,b1,b2,b3);
    //a1 = 1061109567  a2 = 1061109567  a3 = 1061109567
    //b1 = 0  b2 = -1061109568  b3 = -1061109568
    printf("a1 = %d  a2 = %d  a3 = %d\nb1 = %d  b2 = %d  b3 = %lld\n\n",a1,a2,a3,b1,b2,b3);
    //a1 = 1061109567  a2 = 1061109567  a3 = 1061109567
    //b1 = 0  b2 = -1061109568  b3 = -4557430888798830400


    //这样是不是靠谱多了
    std::cout <<"a1=" <" a2=" << a2 << " a3=" << a3 << std::endl;
    std::cout <<"b1=" <" b2=" << b2 << " b3=" << b3 << std::endl;
    //a1=1061109567 a2=1061109567 a3=1061109567
    //b1=-1061109568 b2=3233857728 b3=3233857728


    return 0;
}

从上面可以看出,不管哪种情况,0x3f3f3f3f都没变,但是0xc0c0c0c0表示无穷小却会出问题。

你可能感兴趣的:(C/C++)