我有我認爲將int轉換爲浮點值的功能,該浮點值拆分爲值的符號指數和分數部分。使用IEEE 754來表示浮點值。加倍和除法浮點值
unsigned test(unsigned x) {
// split the given bits of sign exponent and fraction, combine to return
unsigned int sign = (x & 0x80000000) >> 31;
unsigned int expo = (x & 0x7F800000) >> 23;
unsigned int frac = (x & 0x007fffff);
return (sign << 31) | (expo << 23) | frac;
}
我不確定我可是如何從這個浮點表示計算減半或加倍值。
unsigned doubled(unsigned x) {
// get float
// float = unsigned int to float
// doubleFloat = 2*f
// if float is not a number
// return unsigned float
// else return unsigned integer of half float
unsigned int sign = (x & 0x80000000) >> 31;
unsigned int expo = (x & 0x7F800000) >> 23;
unsigned int frac = (x & 0x007fffff);
if (expo == 0xff)
return uf;
else ...
}
只是改變指數之後。 Float = Fraction * 2^exponent *(sign?-1:1)。請注意,這裏的指數是一個有符號的值。 – slavanap