简体   繁体   English

如何在C ++(gcc)中表示float和double?

[英]How are float and doubles represented in C++ (gcc)?

How are floating points represented and interpreted by a compiler. 浮点数如何由编译器表示和解释。 I am trying to understand that so I can easily interpret what byte array would mean for floats and doubles. 我试图理解这一点,所以我可以很容易地解释字节数组对浮点数和双精度数的意义。

Thanks 谢谢

Try this link: http://en.wikipedia.org/wiki/IEEE_754 试试这个链接: http//en.wikipedia.org/wiki/IEEE_754

I just found that this might be a little more helpful: http://en.wikipedia.org/wiki/IEEE_754-1985 我刚刚发现这可能会有所帮助: http//en.wikipedia.org/wiki/IEEE_754-1985

This is the IEEE-Standard for floating point numbers. 这是浮点数的IEEE标准。 There is one from 1985 and a revised edition from 2008. Float is 32bit, double is 64bit (explained in the second link). 1985年有一个,2008年有修订版。浮点数为32位,双倍为64位(在第二个链接中说明)。


Edit: Thx to the comment by Don, here's the link to Intels 80bit floating point description: http://en.wikipedia.org/wiki/Extended_precision 编辑:这是Don的评论,这里是Intels 80bit浮点描述的链接: http//en.wikipedia.org/wiki/Extended_precision

It might also be worth noting that there is a static bool const member of std::numeric_limits , is_iec559 , which is naturally only available for floating point types. 值得注意的是, std::numeric_limitsis_iec559有一个static bool const成员,它自然只适用于浮点类型。 The name is pretty self explanatory... 这个名字很自我解释......

To actually interpret it you would probably not want to treat it as bytes anyway because mantisa boundries don't align to an 8bit boundry. 要实际解释它,您可能不希望将其视为字节,因为mantisa边界不会与8位边界对齐。

Something along the lines of: 有点像:

mantisa =  (*(unsigned int *)&floatVal) | MANTISA_MASK;
exp     = ((*(unsigned int *)&floatVal) | EXP_MASK    ) >> EXP_SHIFT;
sign    = ((*(unsigned int *)&floatVal) | SIGN_MASK   ) >> SIGN_SHIFT;

Would let you pull it apart to play with the juice center. 会让你把它拆开来玩果汁中心。

EDIT: 编辑:

    #include <stdio.h>

    void main()
    {
    float a = 4;
    unsigned int exp,sign,mantisa;
    int i;

        for(i = 0;i<4;i++)
        {
            exp      = (*((unsigned int *)&a) >>23) & 0xFF;
            sign     = (*((unsigned int *)&a) >>31) & 0x01;
            mantisa  = (*((unsigned int *)&a)) & 0x7FFFFF | 0x800000;

            printf("a       = %04x\r\n",*((unsigned int *)&a));
            printf("a       = %f\r\n",a);
            printf("exp     = %i, %02x\r\n",exp,exp);
            printf("sign    = %i, %02x\r\n",sign,sign);
            printf("mantisa = %i, %02x\r\n\r\n",mantisa,mantisa);
            a = -a / 2;

      }
    }

Produces: 生产:

    a       = 40800000
    a       = 4.000000
    exp     = 129, 81
    sign    = 0, 00
    mantisa = 8388608, 800000

    a       = c0000000
    a       = -2.000000
    exp     = 128, 80
    sign    = 1, 01
    mantisa = 8388608, 800000

    a       = 3f800000
    a       = 1.000000
    exp     = 127, 7f
    sign    = 0, 00
    mantisa = 8388608, 800000

    a       = bf000000
    a       = -0.500000
    exp     = 126, 7e
    sign    = 1, 01
    mantisa = 8388608, 800000

    Press any key to continue . . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM