问题描述
在不使用类型转换或库功能的情况下,我必须使用位操作将整数转换为浮点型.下面是我当前正在处理的代码.它基于我在中的某些整数.我遇到的问题涉及IEEE 754中的四舍五入标准.更具体地说,我的代码四舍五入为0,但是应该四舍五入为偶数.我需要进行哪些更改?
Without using casts or functionality of libraries, I must cast an integer to a float with bit manipulation. Below is the code I am currently working on. It is based off of code that I found in Cast Integer to Float using Bit Manipulation breaks on some integers in C. The problem that I have ran into involves the rounding standards in IEEE 754. More specifically my code rounds towards 0, but it should round towards even numbers. What changes do I need to make?
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)
替换为:
To round toward nearest - ties to even, replace (x >> 8)
with:
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
可能会一直传播,并需要呈指数增加.
Note that u += 1
may propagate all the way through and require an exponent increase.
这篇关于IEEE 754位操作舍入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!