首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将float16最大值保存在float32中

将float16最大值保存在float32中
EN

Stack Overflow用户
提问于 2019-07-11 17:52:26
回答 3查看 1.6K关注 0票数 0

如何将float16 (format)最大值保存为float32 (format)格式?

我想要一个可以将0x7bff转换为65504的函数。0x7bff是最大值,可以用浮点半精度表示:

代码语言:javascript
复制
0 11110 1111111111 -> decimal value: 65504 

我希望有0x7bff来表示程序中的实际位数。

代码语言:javascript
复制
float fp16_max = bit_cast(0x7bff); 
# want "std::cout << fp16_max" to be 65504

我试图实现这样一个函数,但它似乎不起作用:

代码语言:javascript
复制
float bit_cast (uint32_t fp16_bits) {
    float i;
    memcpy(&i, &fp16_bits, 4);
    return i; 
}    
float test = bit_cast(0x7bff);
# print out test: 4.44814e-41
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-11 19:21:09

代码语言:javascript
复制
#include <cmath>
#include <cstdio>


/*  Decode the IEEE-754 binary16 encoding into a floating-point value.
    Details of NaNs are not handled.
*/
static float InterpretAsBinary16(unsigned Bits)
{
    //  Extract the fields from the binary16 encoding.
    unsigned SignCode        = Bits >> 15;
    unsigned ExponentCode    = Bits >> 10 & 0x1f;
    unsigned SignificandCode = Bits       & 0x3ff;

    //  Interpret the sign bit.
    float Sign = SignCode ? -1 : +1;

    //  Partition into cases based on exponent code.

    float Significand, Exponent;

    //  An exponent code of all ones denotes infinity or a NaN.
    if (ExponentCode == 0x1f)
        return Sign * (SignificandCode == 0 ? INFINITY : NAN);

    //  An exponent code of all zeros denotes zero or a subnormal.
    else if (ExponentCode == 0)
    {
        /*  Subnormal significands have a leading zero, and the exponent is the
            same as if the exponent code were 1.
        */
        Significand = 0 + SignificandCode * 0x1p-10;
        Exponent    = 1 - 0xf;
    }

    //  Other exponent codes denote normal numbers.
    else
    {
        /*  Normal significands have a leading one, and the exponent is biased
            by 0xf.
        */
        Significand = 1 + SignificandCode * 0x1p-10;
        Exponent    = ExponentCode - 0xf;
    }

    //  Combine the sign, significand, and exponent, and return the result.
    return Sign * std::ldexp(Significand, Exponent);
}


int main(void)
{
    unsigned Bits = 0x7bff;
    std::printf(
        "Interpreting the bits 0x%x as an IEEE-754 binary16 yields %.99g.\n",
        Bits,
        InterpretAsBinary16(Bits));
}
票数 3
EN

Stack Overflow用户

发布于 2019-07-11 18:35:16

通过声明float fp16_max,您的值已经是32位浮点数;无需在这里进行强制转换。我想你可以简单地:

代码语言:javascript
复制
float i = fp16_max;

这里的假设是,您的“神奇”bit_cast函数已经正确返回了32位浮点数。由于您尚未向我们展示bit-cast所做或实际返回的内容,我将假定它确实返回了一个适当的float值。

票数 1
EN

Stack Overflow用户

发布于 2019-07-11 18:44:27

如何将float16最大值保存为float32格式? 65504

您可以简单地将整数转换为浮动:

代码语言:javascript
复制
float half_max = 65504;

如果要计算值,可以使用ldexpf

代码语言:javascript
复制
float half_max = (2 - ldexpf(1, -10)) * ldexpf(1, 15)

或者一般情况下,对于任何IEEE浮点数:

代码语言:javascript
复制
// in case of half float
int bits = 16;
int man_bits = 10;

// the calculation
int exp_bits = bits - man_bits - 1;
int exp_max = (1 << (exp_bits - 1)) - 1;
long double max = (2 - ldexp(1, -1 * man_bits)) * ldexp(1, exp_max);

位铸造0x7bff不工作,因为0x7bff是binary16格式的表示形式(在某种程度上),而不是binary32格式。您不能使用相互冲突的表示。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56994878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档