一、概述
正整數(shù)直接按照源碼存儲名船,負整數(shù)按照補碼存儲诈茧,那么小數(shù)如何存儲表示?
二幔虏、浮點數(shù)的概念(IEEE754)
浮點數(shù)是計算機中用于表示實數(shù)的一種方法,類似于科學計數(shù)法纺念,如:
- 314.4=3.144x102
- 0.00314=3.14x10-3
- 255=2.55x102
由于電腦存儲2進制長度有限,這種方法僅能近似的表示某個數(shù)。
Java中存放浮點數(shù)遵守的標準是IEEE754想括。
三陷谱、IEEE754標準
其定義如何用2進制來存儲浮點數(shù),根據(jù)公式:
V=(-1)N x M x 2R
其中:
- N=為符號位,N=1,則為負數(shù);N=0瑟蜈,則為整數(shù)
- M=底數(shù)(尾數(shù))
- R=冪(指數(shù))
三部分(NMR)分開存儲,有兩種方案烟逊。
(一)、單精度(32位)
? 1(符號位)+8(指數(shù))+23(底數(shù))踪栋,N-(R+127)-M
![][1]
(二)焙格、雙精度(64位)
1(符號位)+11(指數(shù))+52(底數(shù)),N-(R+1023)-M
![][2]
(三)夷都、特殊值
- 無窮 : 指數(shù)全為1,底數(shù)全為0
0 11111111 000000000000....0000
1 11111111 000000000000....0000
- NaN(not a number) : 指數(shù)全為1,底數(shù)不全為0
0 1111111 101010010...0000
1 1111111 101010000...0000
四、如何存儲(如3.14)
(一)予颤、把小數(shù)轉換為2進制
3.14=0b11.0010001111010111...
(二)囤官、轉換為公式格式,底數(shù)部分小數(shù)點前為1
V=(-1)^N x M x 2^R
0b11.0010001111010111...
=(-1)0 x 1.10010001111010111... x 21
(三)、符號位N計算
正數(shù)為0,負數(shù)為1
3.14為正,N=0
(四)蛤虐、指數(shù)部分R計算
R=1
指數(shù)部分R要加上127,轉換為2進制(無符號)
指數(shù)1 加上127(1+127)=128
=0b1000 0000
R=0b1000 0000
(五)党饮、底數(shù)部分M計算
去掉小數(shù)點前面的1,把剩余的序列拼接即可(如何位數(shù)超過范圍,會進行四舍五入)
1.10010001111010111...=(丟掉1,這樣可以多表示一位數(shù))
M=10010001111010111...
(六)、6)組合起來
3.14=N-R-M
3.14=0b0_10000000_10010001111010111000011
五驳庭、總結
- 浮點數(shù)不能準確表示小數(shù)
- 單精度最大值約是3.40E38,最小值(1.40E-45)
- 雙精度最大值約是1.79E308,最小值(4.94E-324)
- 浮點數(shù)如何存儲了解即可
[1]:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmoAAAB9CAMAAAAlQL9HAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAxQTFRF////AAAA/8zMmcyZEGjNOAAABExJREFUeNrsnY1i6iAMRpvy/u98r/2BELqttGiEnnO3aXRak34kwG5hmgAAAAAAAAAAAAAAAAAAAAAAAAAAAOD7CWIfEXtHQie+SOT14V//kjMHjg7g8bfF//W13Jbx3+5vp6cM/HJ/ln5cjbey+J28WlwJ+yPLndC/x02jp0KYmmy11LaXFfFfD7AEPT5aCLGbRq6ymr5JSe3liESPQ/8etw7eFGtCEZiz4V9fV8T/Fe8U3lg9thYffzNIX1lNpbYUtTKHDeBx06Sm8tAlqe3VRFIVFfsepo3/N8PS/q8e8kv6ascNNBXHATxuWz5T7+pyAc2UK6XUTF9tF16HUtMpemuj1oUtvY3h8Rtqgtx7h71y2vhnpyjvuUjnWW3zJwpKioHoPIDHrStoSWX8p1h/TfyLUCcZ9p7V9vRtuv7BDAE69/gdw/ei11ujtCkflmVvdDQssOWkm2HBpIagqWOq6mbuSf8eN43d3mG71VcT1WrFdt+KAqo7yaGryY7Uv1VtTKWzoMUVolv9etw4enI06Xp5BCpTObM5hWxCMw39lyf6qSaSzQ9N+QhI9MyPmcLt1uPm0ctcl+p5tdjcy/ifKhVjNPGamvjIP0yZbvyFAgoAAAAAAABQPRIFQGowntSCL+4f4Gk4Sk1cHZflCz4XcE+pOSfXmVI+eMcJqSE1pAZIDakhNaQGSA2pPV5qE1JDakgNkBpSQ2oAAAAAAADXcf5PLTN8FqQGSA2pITWkBkgNqSE1pIbUkBogNaSG1O5qbF3ZUF23Jfl1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