不使用强制转换或库的功能,我必须使用位操作将整数强制转换为浮点。下面是我正在处理的代码。它基于我在Cast Integer to Float using Bit Manipulation breaks on some integers in C中找到的代码。我遇到的问题涉及IEEE 754中的舍入标准。更具体地说,我的代码向0舍入,但应该向偶数舍入。我需要做什么改变?
unsigned inttofloat(int x) {
int bias = 127;
int man;
int exp = bias + 31; //8-bit exp
int count = 0;
int tmin = 1 << 31;
int manpattern = 0x7FFFFF;
int sign = 0;
if (x == 0){
return 0;
}
else if (x == tmin){
return 0xcf << 24;
}
if (x < 0) {
sign = tmin;
x = ~x + 1; // makes x negative so that we can accurately represent it later on.
}
while((x & tmin) == 0){
exp--;
x <<= 1;
count++;
}
exp <<= 23;
man = (x >> 8) & manpattern;
return (sign | exp | man);
}
最佳答案
若要向最接近的方向取整,请将(x >> 8)
替换为:
unsigned u = x; // avoid any potential signed shifting issues
unsigned lease_significant_bit = (u >> 8) & 1;
unsigned round_bit = (u >> 7) & 1; // Most significant bit shifted out
unsigned sticky_bit_flag = !!(u & 0x7F); // All other bits shifts out
// OP's shifted answer.
u = (u >> 8):
// round away if more than half-way or
// if at half-way and number is odd
u += (round_bit & sticky_bit_flag) | (round_bit & lease_significant_bit);
让OP简化
请注意,
u += 1
可能会一直传播并需要指数增长。关于c - IEEE 754位操作舍入错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42031305/