0

我必须遵循将浮点值(封装在 int 中)转换为 int 值的按位代码。

问题:存在舍入问题,因此在输入为 0x80000001 的情况下会失败。我该如何处理?

这是代码:

  if(x == 0) return x;

  unsigned int signBit = 0;
  unsigned int absX = (unsigned int)x;
  if (x < 0)
  {
      signBit = 0x80000000u;
      absX = (unsigned int)-x;
  }

  unsigned int exponent = 158;
  while ((absX & 0x80000000) == 0)
  {
      exponent--;
      absX <<= 1;
  }

  unsigned int mantissa = absX >> 8;

  unsigned int result = signBit | (exponent << 23) | (mantissa & 0x7fffff);
  printf("\nfor x: %x, result: %x",x,result);
  return result;
4

1 回答 1

1

那是因为 的精度0x80000001超过了 afloat的精度。阅读链接的文章,浮点数的精度是 24 位,因此任何一对浮点数的差 ( x - y) 小于两者的最高位>> 24根本无法检测到。 gdb同意你的演员:

主.c:

#include <stdio.h>

int main() {
    float x = 0x80000001;
    printf("%f\n",x);
    return 0;
}

数据库:

Breakpoint 1, main () at test.c:4
4       float x = 0x80000001;
(gdb) n
5       printf("%f\n",x);
(gdb) p x
$1 = 2.14748365e+09
(gdb) p (int)x
$2 = -2147483648
(gdb) p/x (int)x
$3 = 0x80000000
(gdb) 

这种不精确的极限:

(gdb) p 0x80000000 == (float)0x80000080 
$21 = 1
(gdb) p 0x80000000 == (float)0x80000081
$20 = 0

实际的按位表示:

(gdb) p/x (int)(void*)(float)0x80000000
$27 = 0x4f000000
(gdb) p/x (int)(void*)(float)0x80000080
$28 = 0x4f000000
(gdb) p/x (int)(void*)(float)0x80000081
$29 = 0x4f000001

doubles 确实有足够的精度来区分:

(gdb) p 0x80000000 == (float)0x80000001
$1 = 1
(gdb) p 0x80000000 == (double)0x80000001
$2 = 0
于 2012-09-10T01:16:44.057 回答