gpt4 book ai didi

c - 将 float 转换为 int(大整数)的函数

转载 作者:太空宇宙 更新时间:2023-11-04 02:47:32 25 4
gpt4 key购买 nike

这是一道大学题。只是为了确保 :-) 我们需要实现 (float)x

我有以下代码,它必须将整数 x 转换为存储在无符号整数中的浮点二进制表示形式。

unsigned float_i2f(int x) {
if (!x) return x;

/* get sign of x */
int sign = (x>>31) & 0x1;

/* absolute value of x */
int a = sign ? ~x + 1 : x;

/* calculate exponent */
int e = 0;
int t = a;
while(t != 1) {
/* divide by two until t is 0*/
t >>= 1;
e++;
};

/* calculate mantissa */
int m = a << (32 - e);
/* logical right shift */
m = (m >> 9) & ~(((0x1 << 31) >> 9 << 1));

/* add bias for 32bit float */
e += 127;

int res = sign << 31;
res |= (e << 23);
res |= m;

/* lots of printf */

return res;
}

我现在遇到的一个问题是,当我的整数太大时,我的代码就会失败。我已实现此控制程序:

float f = (float)x;
unsigned int r;
memcpy(&r, &f, sizeof(unsigned int));

这当然总是会产生正确的输出。

现在当我进行一些测试运行时,这是我的输出(目标是它需要的,结果是我得到的)

:!make && ./btest -f float_i2f -1 0x80004999                                                                  
make: Nothing to be done for `all'.
Score Rating Errors Function
x: [-2147464807] 10000000000000000100100110011001
sign: 1
expone: 01001110100000000000000000000000
mantis: 00000000011111111111111101101100
result: 11001110111111111111111101101100
GOAL: 11001110111111111111111101101101

所以在这种情况下,添加一个 1 作为 LSB。

下一个案例:

:!make && ./btest -f float_i2f -1 0x80000001
make: Nothing to be done for `all'.
Score Rating Errors Function
x: [-2147483647] 10000000000000000000000000000001
sign: 1
expone: 01001110100000000000000000000000
mantis: 00000000011111111111111111111111
result: 11001110111111111111111111111111
GOAL: 11001111000000000000000000000000

这里的指数加1,尾数是它的补码。

我花了几个小时在互联网上和我的书中查找 ip 等,但我找不到任何关于这个问题的引用。我想这与尾数只有 23 位有关。但是我该如何处理呢?

编辑:由于以下评论,这部分已过时。 int l 必须是 unsigned l。

int x = 2147483647; 
float f = (float)x;

int l = f;
printf("l: %d\n", l);

然后 l 变成 -2147483648。

这怎么会发生?那么 C 的转换是错误的吗?

希望有人能帮帮我!谢谢马库斯

编辑 2:

我更新后的代码现在是这样的:

unsigned float_i2f(int x) {
if (x == 0) return 0;
/* get sign of x */
int sign = (x>>31) & 0x1;

/* absolute value of x */
int a = sign ? ~x + 1 : x;

/* calculate exponent */
int e = 158;
int t = a;
while (!(t >> 31) & 0x1) {
t <<= 1;
e--;
};

/* calculate mantissa */
int m = (t >> 8) & ~(((0x1 << 31) >> 8 << 1));
m &= 0x7fffff;

int res = sign << 31;
res |= (e << 23);
res |= m;

return res;
}

我还发现该代码适用于 -2^24、2^24 范围内的所有整数。上面/下面的所有内容有时都有效,但大多数情况下无效。

有些东西不见了,但我真的不知道是什么。谁能帮帮我?

最佳答案

打印出来的答案是绝对正确的,因为它完全取决于所投数字的基本表示形式。但是,如果我们了解数字的二进制表示,您就不会对这个结果感到惊讶。

要了解隐式转换与赋值运算符相关联(引用 C99 标准 6.5.16)。 C99 标准接着说:

6.3.1.4 Real floating and integer When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.

您之前的示例说明了由于分配目标类型范围之外的值而导致的未定义行为。尝试将负值分配给无符号类型,而不是将 float 转换为整数。

以下代码段中的断言应防止发生任何未定义的行为。

#include <limits.h>
#include <math.h>
unsigned int convertFloatingPoint(double v) {
double d;
assert(isfinite(v));
d = trunc(v);
assert((d>=0.0) && (d<=(double)UINT_MAX));
return (unsigned int)d;
}

做同样事情的另一种方法,创建一个包含一个 32 位整数和一个 float 的 union 。 int 和 float 现在只是查看相同内存位的不同方式;

union {
int myInt;
float myFloat;
} my_union;

my_union.myInt = 0x BFFFF2E5;

printf("float is &#37;f\n", my_union.myFloat);

float is -1.999600

您是在告诉编译器获取您拥有的数字(大整数)并将其转换为 float ,而不是将数字解释为 float 。为此,您需要告诉编译器以不同的形式从该地址读取数字,因此:

myFloat = *(float *)&myInt ;

也就是说,如果我们把它拆开,从右边开始:

  • &myInt - 内存中保存整数的位置。
  • (float *) - 真的,我希望编译器将其用作指向 float 的指针,而不是编译器认为它可能是什么。
  • * - 从右边的地址读取。
  • myFloat = - 将此变量设置为右边的值。

所以,你是在告诉编译器:在 (myInt) 的位置,有一个 float ,现在把那个 float 放到 myFloat 中。

关于c - 将 float 转换为 int(大整数)的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25699585/

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