浮点数为32和64位二进制表示形式 [英] Floating point number to 32 and 64bit binary representation

查看:185
本文介绍了浮点数为32和64位二进制表示形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取某个浮点值(双精度值)的64位二进制表示形式(IEEE 754). 我有以下代码用于float的32位二进制表示形式:

I need to get 64 bit binary representation (IEEE 754) of some floating point value (double value). I have this code for 32 bit binary representation of float:

union
{
    float input;   // assumes sizeof(float) == sizeof(int)
    int   output;
}   data;
data.input = value;
std::bitset<sizeof(float) * CHAR_BIT>   bits(data.output);

在这种情况下工会与convert依者有何关系?为什么我应该使用它? 是否有一些漂亮的方法可以完成64位表示?

How union relates to convert in this situation? Why I should to use it? Is there some pretty way to do the same to get 64bit representation?

推荐答案

在这种情况下工会与convert依者有何关系?

How union relates to convert in this situation?

在C11中,允许您使用union s执行类型调整",这是此处转换的本质:将与float关联的位重新解释为(带符号的,假定为32-位)的整数.

In C11, you are permitted to use unions to perform "type-punning", which is the essence of your conversion here: reinterpreting the bits associated with a float instead as a (signed, presumed 32-bit) integer.

为什么我应该使用它?

Why I should to use it?

您不应使用此功能.在C ++中,这被认为是未定义的行为,尽管许多主流编译器将支持即开即用的基于联合类型的对等操作,但不能始终依靠它们来提供这种行为,尤其是当C ++标准在未来.

You should not use this. In C++, this is considered undefined behavior, and while many mainstream compilers will support union-based-type-punning out-of-the-box, they cannot be relied upon to always provide this behavior, especially as the C++ standard changes in the future.

是否有一些漂亮的方法可以完成64位表示?

Is there some pretty way to do the same to get 64bit representation?

如果您的编译器保证基于联合类型的双工,则只需将其替换为适当的64位整数:

If your compiler guarantees union-based-type-punning, then you need only replace this with the appropriate 64-bit integer:

static_assert(sizeof(double) == sizeof(uint64_t));
union {
    double input;
    uint64_t output;
} data;
data.input = value;
std::bitset<sizeof(double) * CHAR_BIT> bits(data.output);

如果没有,则没有 pretty 方法,但是有一种方法可以保证定义的行为:

If it doesn't, then there's no pretty way to do this, but there is a way to do it while guaranteeing defined behavior:

static_assert(sizeof(double) == sizeof(uint64_t));
uint64_t output;
double input = value;
memcpy(output, input, sizeof(double));
std::bitset<sizeof(double) * CHAR_BIT> bits(output);

这篇关于浮点数为32和64位二进制表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆