gpt4 book ai didi

c - C 中 intBitsTofloat 中丢弃的精度值

转载 作者:行者123 更新时间:2023-11-30 15:27:00 29 4
gpt4 key购买 nike

我有一个 double 值=10.233387434223。我想通过网络传输。我通过使用将此值转换为 int32 位下面的函数

    int floatToRawIntBits(float_t  x)
{
union {
float_t f; // assuming 32-bit IEEE 754 single-precision
int32_t i; // assuming 32-bit 2's complement int
} u;

u.f = x;
return u.i;
}

然后我尝试使用函数来取回它

union int_float_bits {
int64_t int_bits;
float_t float_bits;
};

float_t intBitsToFloat:(int64_t x)
{
union int_float_bits bits;
bits.int_bits = x;
return bits.float_bits;
}

输出为:10.233387。但我想取回原始值 0f 10.233387434223 而不是 10.233387 。

intBitsToDouble 可能是一个可能的解决方案吗?

我也尝试使用这些代码

int DoubleToRawIntBits(double_t  x)
{
union {
double_t f;
int64_t i;
} u;

u.f = x;
return u.i;
}

union int_double_bits {
int64_t int_bits;
double_t double_bits;
};
double_t intBitsToDouble(int64_t x)
{
union int_double_bits bits;
bits.int_bits = x;
return bits.double_bits;
}

但这里的结果是 0.0000000。

请帮助我,谢谢。

最佳答案

您的问题是第一个函数仅返回 64 位 double 中的 32 位。仅传输 32 位。因此,下一个函数无法构造整个 double 型,因为它实际上不具有 32 位 double 型。它无法无中生有地获取这些信息。如果它不存在,它就不存在。您需要传输整个 double 。我不知道你到底在做什么,但如果你可以发送 32 位,你也应该能够发送 64 位。

关于c - C 中 intBitsTofloat 中丢弃的精度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27221659/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com