C/C++ä¸ï¼
æµ®ç¹æ°ï¼float以å double å¨å
åä¸æ¯ææ ·åå¨çï¼
åå¦ï¼ææ32-bit
8bit 8bit 8bit 0 0 0 0 0 1 1 1 1
对äºæ´å½¢intï¼æ们å¯ä»¥å¾å¿«å¾åºï¼è¿æ¯ int i = 15çå
åå½¢å¼ã
å设ï¼æä½ä½çbitçä½æ为-1ï¼æé«ä½ä¸º30ã é£ä¹è¿ä¸ªå°±ä¸å表示æ°å15äºï¼èæ¯
2^-1+2^0+2^1+2^2 = 7.5 äºã
å½ç¶ï¼ä¸é¢åªæ¯å设ï¼é£ä¹çæ£çFloat æµ®ç¹å å¨å
åä¸æ¯ä»ä¹æ ·åçå¢ï¼
é¦å
éè¦ç¥éçæ¯ float å¨å
åä¸ å 32-bit doubleå å 64-bitã
æµ®ç¹å å¨å
åä¸ï¼æ3é¨åææã
Sign bit
Exponent (ææ°ï¼
Mantissa(å°¾æ°ï¼æææ°å)
sign bit
æ¯ææµ®ç¹æ°å¨å
åä¸ç æé«ä½ï¼0 表示 æ£æ°ï¼1 表示è´æ°ãSing bit å¨æµ®ç¹æ°floatï¼32-bitå
åä¸ï¼å 1-bit ã
Exponent
ææ°ï¼æ¯å¦ 10^5ï¼2^6ï¼è¿ä¸¤ä¸ªæ°ç 5ï¼6æ¢æ¯exponentãå½ç¶ï¼æ°åå¨å
åä¸é½æ¯ä»¥2è¿å¶ä½ç°çï¼æ以è¿éçææ°ï¼æ¯æ以2ä¸ºåº çææ°ãæ¯å¦
0 0 0 0 0 1 1 0
å¾å®¹æå¯ä»¥ç¥é Exponent为 6ï¼å¨è¡¨ç¤ºæµ®ç¹æ°çå
åä¸ï¼è¡¨ç¤ºçæ¯ 2^6 = 64ã
Expoent å¨ Float 32-bitçå
åä¸ï¼å 8-bitï¼å¨è¿éææ¤8-bitè§ä¸ºè¡¨ç¤º
unsigned int çbit patternãé£ä¹å¯ä»¥è¡¨ç¤ºçèå´æ¯0~256çæ´æ°(ææ°èå´)ï¼ ä½æ¯ææ°æ¢å¯ä»¥ä¸º
æ£æ´æ°ï¼ä¹å¯ä»¥ä¸ºè´æ´æ°ï¼è¿æ ·ä»¥æ¥æ æ³è¡¨ç¤º-1ï¼-2....è¿æ ·çè´æ´æ°äºï¼æ以 IEEE Standard 754 Floating-Point 对æ¤å¼å
¥äºBiasï¼
å移éçæ¦å¿µï¼å¯¹äºFloatåï¼æ¤å移é为127. ä¹å°±æ¯è¯´ 127 è¿ä¸ªæ°åå·²ç»è¢«åå¨å° Exponentè¿ä¸ªé¨åä¸äºï¼åä¹åçé£ä¸ªä¾åï¼
0 0 0 0 0 1 1 0
表示çæ¯ææ°6ï¼ä½æ¯å¨floatå
åç»æä¸ï¼å
¶å®è¡¨ç¤ºçæ¯ (6-127)= -121ãéè¦åå»å·²åå
¥çå移é 127ã
åå¦ 2^(1),ææ°1å¨float çå
åç»æä¸ç bit patternæ¯ä»ä¹æ ·åçï¼
é£ä¼ä¸ä¼å°±æ¯ç®åç
0 0 0 0 0 0 0 1
åºè¯¥æ¯ exponent - 127 = 1;(2^(1)ä¸çææ°1æ¯è¿æ ·å¾æ¥ç)
exponent = 127+1 = 128.(2^(1)ä¸çææ°1ï¼å¨floatå
åç»æä¸åºè¯¥æ¯128çbit patternæ对ï¼
1 0 0 0 0 0 0 0
è¿åªæ¯ä¸ªä¾åï¼å¸®å©ç解exponentï¼ä¸ä¼ççé®è¿æ ·çé®é¢ãããã
Doubleåï¼éè¦å ç¨64-bit å
å空é´ãåæ ·ï¼ä¹æ¯ç± Sign bitï¼Exponent,Mantissa 3é¨åææï¼ä¸è¿ Exponenté¨åï¼å¨æ´ä¸ª64-bitä¸ è¦å å° 11-bitãæ¤å¤å移é 为1023ã
Mantissa
Mantissa å°¾æ°é¨åï¼å¨floatç32-bitçå
å空é´ä¸ï¼å å°23-bit注æä¹å说çexponent ææ°ï¼æä½ä½æ¯ä»0å¼å§çï¼é£ä¹Mantissaï¼å°¾æ°çæé«ä½å½ç¶æ¯ -1äºã
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
é£ä¹å¤§å®¶è¯´ä¸ï¼ä¸é¢çå°¾æ°é¨åå¨ float æµ®ç¹æ°çå
åä¸ï¼è¡¨ç¤ºå¤å°ï¼ å¾å¿«å¯ä»¥å¾å°æ¯
2^(-2)+2^(-3) = 0.375ã æéäºï¼åºè¯¥æ¯1.375ã
大家åæ³ä¸å°å¦å¦ç
ç§å¦è®°æ°æ³ï¼5 = 5.0*10^0 , 0.75 = 7.5*10^(-1)ã对å§ï¼
å¨Floatçå
å表示ä¸ï¼è¿23-bitçå°¾æ° ä»
ä»
表示 ç§å¦è®°æ°æ³ ä¸ éé¶å®æ°å°æ°ç¹åç精度ã æ¢å¥è¯è¯´ï¼Mantissa å
æ¬ä¸¤é¨åï¼ä¸ä¸ªæ¯leading bitï¼ç§å¦è®°æ°æ³çéé¶å®æ°ï¼,å¦ä¸ä¸ªæ¯fraction bitsï¼å³ç²¾åº¦ï¼ï¼æ¤23-bitä»
ä»
表示çæ¯ fraction bitsãèå¨
äºè¿å¶ä¸ï¼éé¶å®æ°èªç¶æ¯1äºï¼æ以leading bité»è®¤æ¯1äºãæ以ä¸è¿°è¡¨æ ¼å®é
ä¸æ¯è¡¨ç¤º
å¼ç¨
1 + 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
è¿ä¹å°±æ¯ä¸ºä»ä¹ï¼å¨floatçå
åä¸ï¼å°¾æ°é¨åå¯ä»¥ç¨23-bit pattern æ¥è¡¨ç¤ºåº24-bitçä¸åæ°åäºã
å¨Doubleåç 64-bit å
åç»æä¸ï¼å°¾æ°é¨åè¦å å°52-bitã
æ们ç¨ä¸ªè¡¨æ ¼æ¥è¡¨ç¤º å¨å
åä¸ï¼floatæ¯ææ ·åå¨çã
+/-Sign Exponent ææ° Fraction bit -> .f
s <---------------- 8 ----------------> <-------------------------------------- 23----------------------------->
Unsigned int 2^(-1)ï¼ 2^(-2)ï¼ 2^(-3)............
ä¸é¢è¿ä¸ªè¡¨æ ¼æè¦ è¡¨ç¤ºçæ¯å¦ä¸çæµ®ç¹æ°
(-1)^s * 1.f * 2^(Exponent-127)
éæåäºä¸ª32-bit patternï¼
0 0 0 0 1 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2^8 2^0 2^-23
åå¦åè¯ä½ ï¼è¿æ¯ä¸ä¸ªæµ®ç¹åçå
åç»æï¼é£ä¹è¿ä¸ªæµ®ç¹æ°æ¯å¤å°å¢ï¼
è¿ä¸ªæµ®ç¹æ°å¯ä»¥å¾å¿«çå¾å° (-1)^0*1.(2^-2+2^-3)*2^(2^1+2^2+2^4-127)ã
追é®è½ç®ç»åçåããã
追çæµ®ç¹å å¨å
åä¸ï¼æ3é¨åææã
Sign bit
Exponent (ææ°ï¼
Mantissa(å°¾æ°ï¼æææ°å)
(-1)^s * 1.f * 2^(Exponent-127)
追é®æå°±åé®ä¸ªé¶ç å§ï¼N=±SÃ2*pæè¿æ¯ç¥éçï¼æ³ç¥éPæä¹ç®åºæ¥
追çå
ææµ®ç¹æ° 转æäºè¿å¶
ç¶å 移å¨å°æ°ç¹ç§°ä¸º1.xxxxxxå½¢å¼
移å¨äºå¤å°ä½, å ä¸127å°±æ¯äº.