獲得更高的浮點數(0x1p8f、0x1p16f、0x1p32f、0x1p64f)的應用


 

libgcc中讀取了一些代碼:

UDWtype __fixunsxfDI (XFtype a)
{
  if (a < 0) return 0; /* Compute high word of result, as a flonum. */ const XFtype b = (a / Wtype_MAXp1_F); /* Convert that to fixed (but not to DWtype!), and shift it into the high word. */ UDWtype v = (UWtype) b; v <<= W_TYPE_SIZE; /* Remove high part from the XFtype, leaving the low part as flonum. */ a -= (XFtype)v; /* Convert that to fixed (but not to DWtype!) and add it in. Sometimes A comes out negative. This is significant, since A has more bits than a long int does. */ if (a < 0) v -= (UWtype) (- a); else v += (UWtype) a; return v; } 

關於XFType:

typedef     float XFtype __attribute__ ((mode (XF))); 

關於Wtype_MAXp1_F:

#if W_TYPE_SIZE == 8 # define Wtype_MAXp1_F 0x1p8f #elif W_TYPE_SIZE == 16 # define Wtype_MAXp1_F 0x1p16f #elif W_TYPE_SIZE == 32 # define Wtype_MAXp1_F 0x1p32f #elif W_TYPE_SIZE == 64 # define Wtype_MAXp1_F 0x1p64f #else # error "expand the table" #endif

其中Wtype_MAXp1_F定義了 “0x1p8f” 、
“0x1p16f”、“0x1p32f”、“0x1p64f”,四種不同的值。
舉例來說“0x1p32f” p后面表示是2的32次方 並且類型為float,即0x1p32f=1*232 

0x之后,p之前的數字被解釋為十六進制數字並且可以包括小數點,即可以是整數也可以是小數。

在“0x2.3p-1”中,“2.3”表示2 + 3/16 = 2.1875 (轉為10進制數),
0x2.3p-1=2.1875*2-1=1.09375
所以,0x1p32f=1*232
 其中“f”或“F”表是float類型,“l”或“L”表示long double
當a/Wtype_MAXp1_F 即 a除以2的(8/16/32/64)次方后,可以成為擴展精度算術的一部分;它將a分成一些“高位”和一些“低位”。

實際項目會用到a/Wtype_MAXp1_F的情況,用於轉換16進制變量。如用vs編譯,建議用vs2017以上,以下貌似不支持(未驗證)

可以通過包含<float.h>並使用FLT_MAX獲得最大有限float,DBL_MAX獲得最大有限double來獲得最大有限浮點數,並使用LDBL_MAX獲得最大有限long double。
這些值遠大於Wtype_MAXp1_F。還可以通過包含<math.h>和使用INFINITY獲得最高的float值,即無窮大。

本文參考了一些其他資料,如侵則刪。

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM