简体   繁体   English

在C89中,如何截断并将双精度浮点数拆分为两个32位字?

[英]In C89, how can I truncate and split a double precision floating point into two 32-bit words?

When targeting C99, I could truncate and split a double value into two 32-bit integers with the following code: 以C99为目标时,我可以使用以下代码截断并将double值拆分为两个32位整数:

#include <stdint.h>

void split(double d, unsigned long *most_significant_word, unsigned long *least_significant_word)
{
   uint64_t i = (uint64_t)d;
   *most_significant_word = i >> 32;
   *least_significant_word = i & 0xffffffff;
}

C89 however, does not seem to define a 64-bit integer type, so I can't use the compiler to perform the truncation. 但是C89似乎没有定义64位整数类型,因此我不能使用编译器执行截断。 Even if the truncation was not necessary(the value already represented an integer) I also could not use bit operators like & or >> since these don't work on double values. 即使不需要截断(该值已经表示一个整数),我也无法使用&>>类的位运算符&因为这些运算符不适用于double值。

So, how can the above split() function be implemented in pure C89(and thus without relying on 64-bit integers), returning the 21/32 bit words that make up a 53-bit integer stored in a double value? 那么,如何在纯C89中实现上面的split()函数(从而不依赖于64位整数),返回组成存储在double值中的53位整数的21/32位字?

Ignoring the sign (as does the original code) it should be as simple as this: 忽略符号(与原始代码一样)应该像下面这样简单:

void split(double d, unsigned long *most_significant_word, unsigned long *least_significant_word)
{
   *most_significant_word = d/4294967296.; // d>>32 in double
   *least_significant_word = fmod(d, 4294967296.);
}

To take negative numbers into consideration, operate on the absolute value then compute the two's complement: 要考虑负数,请对绝对值进行运算,然后计算两者的补数:

void split(double d, unsigned long *most_significant_word, unsigned long *least_significant_word)
{
   double dabs = d < 0 ? -d : d;
   *most_significant_word = dabs/4294967296.;
   *least_significant_word = fmod(dabs, 4294967296.);
   if (d < 0) {
       *most_significant_word = ~*most_significant_word;
       *least_significant_word = ~*least_significant_word + 1;
       if (!*least_significant_word) *most_significant_word += 1;
   }
}

暂无
暂无

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

相关问题 如何在c89中定义浮点双精度常量 - How to define constant in floating point double precision in c89 如何检查是否使用了 IEEE 754 单精度(32 位)浮点表示法? - How to check that IEEE 754 single-precision (32-bit) floating-point representation is used? 在不支持双精度的C编译器上将64位双精度浮点数据转换为Uint32 - Convert 64-bit double precision floating point data to Uint32 on a C compiler that doesn't support double precision 如何在AT&T程序集中以双精度将80位浮点数从内存移动到XMM0 - How can I move an 80-bit floating point number with double precision from memory to XMM0 in AT&T assembly 如何在32位和64位模式下获得双精度操作的相同行为? - How do I get the same behavior for double precision operations in both 32-bit and 64-bit modes? 为什么从 32 位二进制大端编码的文件中读取 IEEE-754 浮点时会丢失精度? - Why am I losing precision when reading in IEEE-754 floating point from a file encoded in 32-bit binary big endian? 如何在 C89 中读取字符串直到逗号? - How do I read a string until a comma in C89? C / C ++-将32位浮点值转换为24位归一化定点值? - C/C++ - convert 32-bit floating-point value to 24-bit normalized fixed-point value? 如何在C中使用两个32位整数作为64位? - How do I use two 32-bit integers as a 64-bit in C? 将4个原始字节转换为32位浮点 - Converting 4 raw bytes into 32-bit floating point
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM