![]() |
PCDVD數位科技討論區
(https://www.pcdvd.com.tw/index.php)
- 疑難雜症區
(https://www.pcdvd.com.tw/forumdisplay.php?f=34)
- - 關於半精度浮點問題
(https://www.pcdvd.com.tw/showthread.php?t=1160481)
|
|---|
關於半精度浮點問題
有沒有 半精度浮點 (Half float) 轉換 int16_t 的方法 :confused:
|
除了每天支支叫外 :unbelief:
google 一下 IEEE 754-1985 的規範可以嗎? :unbelief: |
引用:
支歸支,浮點歸浮點 不懂不要亂回答可以嗎? :confused: 你不要又要來說 Linux 是 GCC 寫的 :think: |
都告訴你去看 spec 了, 裡面就有答案了 :p
還是不會嗎? :laugh: 可以再問, 我會告訴你怎麼做的, 我可是很少教人這種junior的問題 :D 對了:D 你的水土散熱器進化到哪一版了啊? :D |
引用:
有答案 :confused: 意思就是說 你知道就對了 那測試一下 看你是真知道 還是假知道 :D 這段你看得懂嗎 :laugh: :laugh: 代碼:
unsigned int2hfloat(int x)
{
unsigned sign = x < 0;
unsigned absx = ((unsigned)x ^ -sign) + sign; // safe abs(x)
unsigned tmp = absx, manbits = 0;
int exp = 0, truncated = 0;
// calculate the number of bits needed for the mantissa
while (tmp)
{
tmp >>= 1;
manbits++;
}
// half-precision floats have 11 bits in the mantissa.
// truncate the excess or insert the lacking 0s until there are 11.
if (manbits)
{
exp = 10; // exp bias because 1.0 is at bit position 10
while (manbits > 11)
{
truncated |= absx & 1;
absx >>= 1;
manbits--;
exp++;
}
while (manbits < 11)
{
absx <<= 1;
manbits++;
exp--;
}
}
if (exp + truncated > 15)
{
// absx was too big, force it to +/- infinity
exp = 31; // special infinity value
absx = 0;
}
else if (manbits)
{
// normal case, absx > 0
exp += 15; // bias the exponent
}
return (sign << 15) | ((unsigned)exp << 10) | (absx & ((1u<<10)-1));
}
半精度浮點是幾 byte :D :D 你知道不知道 我想問的是什麼東西 :laugh: :laugh: |
這種演算法網路上隨便找就一大堆
老是幹這種沒價值的事情 還用 android 2 要跑 ruby 有什麼好屁的 :D |
引用:
所以你根本就來亂的嘛 :laugh: :laugh: 上面我貼出來的是 int 轉 半精度浮點 我想知道的是 半精度浮點 轉 int 你說找一大堆 那你找來給我看看 :laugh: :laugh: |
引用:
我剛剛就講了, 這種code網路上一堆 去看懂 IEEE 754-1985 規範就會寫float to int了 根本就不需要去找別人寫的東西 這還是念資工大2的回家功課 :p 連結貼給你 https://en.wikipedia.org/wiki/IEEE_754-1985 問這種超基本的問題 我不覺得你適合當工程師 :think: |
引用:
你是來亂的沒錯阿 半精度浮點在哪 要不要我提示你一下 :laugh: :laugh: https://zh.wikipedia.org/wiki/%E5%8...%82%B9%E6%95%B0 你貼的 32-bit / 64-bit 的給我幹嘛 我早就看爛了 :laugh: 32-bit / 64-bit 轉換我還用你教阿 :think: |
god bless your boss
|
| 所有的時間均為GMT +8。 現在的時間是01:34 PM. |
vBulletin Version 3.0.1
powered_by_vbulletin 2025。