简体   繁体   English

C-如何存储IEEE 754双精度和单精度

[英]C - how to store IEEE 754 double and single precision

I have to work with IEEE 745 double and single precision numbers. 我必须使用IEEE 745双精度和单精度数字。 I have no idea, how to work with them correctly. 我不知道如何正确使用它们。

I've got a buffer of binary data and I want to get the numbers like 我有一个二进制数据缓冲区,我想获得像

uint8_t bufer[] = {................};
//data I want are at 8th position, (IEEE745_t is my imaginary format)
IEEE745double_t first8bytes = *(IEEE745double_t*)(buffer + 8);
IEEE745single_t next4bytes = *(IEEE745single_t*)(buffer + 16);

What do I put instead of IEE745double_t and IEEE745single_t ? 我要代替IEE745double_tIEEE745single_t什么? Is it possible to do it with double and float? 是否可以使用double和float? And if so, how can I guarantee, that they will be 8 and 4 bytes long on every platform? 如果是这样,我如何保证在每个平台上它们的长度分别为8和4个字节?

First of all, you cannot do the pointer cast hack. 首先,您不能进行指针强制转换。 There is absolutely no guarantee that your byte buffer is correctly aligned. 绝对不能保证字节缓冲区正确对齐。 This results in undefined behaviour . 这导致不确定的行为 Use memcpy instead: 使用memcpy代替:

memcpy(&first8bytes, &buffer[8], 8); 
memcpy(&next4bytes, &buffer[16], 4); 

What do I put instead of IEE745double_t and IEEE745single_t ? 我要代替IEE745double_tIEEE745single_t什么? Is it possible to do it with double and float? 是否可以使用double和float?

Yes, it's possible, if : 是的, 如果有可能:

  • double is 8 bytes, float is 4 bytes and both use IEEE754 presentation. double是8个字节, float是4个字节,都使用IEEE754表示。
  • Data on buffer uses same byte order as your host. buffer数据使用与主机相同的字节顺序。 If not, you need to copy to temporary unsigned integer type first, where you fix the endianness. 如果不是,则需要先复制到临时的无符号整数类型,然后再固定字节序。

And if so, how can I guarantee, that they will be 8 and 4 bytes long on every platform? 如果是这样,我如何保证在每个平台上它们的长度分别为8和4个字节?

Use static assertion to detect when it's not. 使用静态断言来检测何时不存在。 For example: 例如:

static_assert(sizeof(float) == 4, "invalid float size");

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

相关问题 十进制到 IEEE 754 使用 C 的单精度 IEEE 754 代码 - Decimal to IEEE 754 Single-precision IEEE 754 code using C 如何在C中使用union和struc将float转换为IEEE754 Double Precision格式? - How to convert float to IEEE754 Double Precision format by using union and struc in C? 将字节重新解释为C中的浮点数(IEEE 754单精度二进制) - Reinterpret bytes as float in C (IEEE 754 single-precision binary) IEEE 754 双精度数从 MIPS C 到 Java 的转换 - IEEE 754 double-precision numbers from MIPS C to Java conversion IEEE 754和浮点精度 - IEEE 754 and float precision 用C语言解释32位无符号长单精度IEEE-754浮点数 - Interpreting a 32bit unsigned long as Single Precision IEEE-754 Float in C 如何将 DEC 64 位双精度浮点数转换为 IEEE-754(DEC 不是十进制) - How to convert DEC 64bit double precision floating point to IEEE-754 (DEC is not decimal) 如何将IEEE 754单精度二进制浮点数转换为十进制? - How to convert an IEEE 754 single-precision binary floating-point to decimal? 如何检查是否使用了 IEEE 754 单精度(32 位)浮点表示法? - How to check that IEEE 754 single-precision (32-bit) floating-point representation is used? 如何仅使用整数算法生成IEEE 754单精度浮点数? - How to generate an IEEE 754 Single-precision float using only integer arithmetic?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM