简体   繁体   English

加倍和除以浮点值

[英]Doubling and dividing floating point values

I have the function which I believe will convert an int into a floating point value split into the sign exponent and fraction components of the value. 我有一个函数,我相信将int转换为浮点值,拆分为值的符号指数和分数组成部分。 Using IEEE 754 to represent Float values. 使用IEEE 754表示浮点值。

在此输入图像描述

unsigned test(unsigned x) {    
    // split the given bits of sign exponent and fraction, combine to return

    unsigned int sign = (x & 0x80000000) >> 31;
    unsigned int expo = (x & 0x7F800000) >> 23;
    unsigned int frac = (x & 0x007fffff);

    return (sign << 31) | (expo << 23) | frac;
}

I'm unsure however how I could compute the halved or doubled values from this floating point representation. 但我不确定如何从这个浮点表示计算减半或加倍的值。

unsigned doubled(unsigned x) {
    // get float
    // float = unsigned int to float
    // doubleFloat  = 2*f
    // if float is not a number
        // return unsigned float
    // else return unsigned integer of half float

    unsigned int sign = (x & 0x80000000) >> 31;
    unsigned int expo = (x & 0x7F800000) >> 23;
    unsigned int frac = (x & 0x007fffff);

    if (expo == 0xff)
        return uf;
    else ...
}

It seems that you are using IEEE 754 to represent Float values. 您似乎使用IEEE 754来表示Float值。

If you want to double the binary representation, you just need to increment by 1 the expoent. 如果要将二进制表示加倍,则只需要将expoent加1。 The same is true if you want to half, just decrement by 1 如果你想减半,只减1,也是如此

Remember that this is true only if your number is in normal range, including even after doubling or halving 请记住,只有当您的号码在正常范围内时才会出现这种情况,包括即使在加倍或减半之后也是如此

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

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