關(guān)于移碼和浮點(diǎn)數(shù)
移碼的英文是offset?binary
它還有很多種叫法,可以看這里
Offset binary,[1] also referred to as excess-K,[1] excess-N, excess-e,[2][3] excess code or biased representation, is a method for signed number representation where a signed number n is represented by the bit pattern corresponding to the unsigned number n+K, K being the biasing value or offset.
這里有幾個(gè)關(guān)鍵點(diǎn):
1.移碼是針對(duì)有符號(hào)數(shù)的一種編碼。
2.移碼的編碼是說(shuō)把原來(lái)的二進(jìn)制編碼再加上一個(gè)另外的一個(gè)數(shù),這就叫移碼。只要你多加了,就叫移碼,不是說(shuō)非得加128或127才叫移碼,你加3也叫移碼,比如余三碼里面,要給結(jié)果加3,這種也叫移碼。
There is no standard for offset binary,?
but most often the K for an n-bit binary word is K = 2n?1 (for example, the offset for a four-digit binary number would be 8).?
?意思是移碼并沒(méi)有統(tǒng)一的標(biāo)準(zhǔn),加多少都可以的。
This has the consequence that the minimal negative value is represented by all-zeros,
?the "zero" value is represented by a 1 in the most significant bit and zero in all other bits,?
?and the maximal positive value is represented by all-ones (conveniently,
?this is the same as using two's complement but with the most significant bit inverted).
用了移碼表示有這樣的好處。
如果用移碼表示的話,比如四個(gè)二進(jìn)制位,最小的那個(gè)負(fù)數(shù)就是0000,最大的那個(gè)數(shù)就是1111。
. It also has the consequence that in a logical comparison operation,
?one gets the same result as with a true form numerical comparison operation,?
whereas, in two's complement notation a logical comparison will agree with true form numerical comparison operation
?if and only if the numbers being compared have the same sign. Otherwise the sense of the comparison will be inverted,
?with all negative values being taken as being larger than all positive values.
意思是說(shuō),有了移碼可以保證邏輯比較運(yùn)算符和算數(shù)比較運(yùn)算符的結(jié)果是一致的。
說(shuō)實(shí)話我沒(méi)有看懂,啥是邏輯比較運(yùn)算符??算數(shù)比較運(yùn)算符就是大于小于這些。
但我覺(jué)得可能是這樣的意思。
我們可以用邏輯操作來(lái)比較兩個(gè)數(shù)大小的,比如移位操作,
比如比較1110?和1010?這兩個(gè)數(shù),假設(shè)它們都是補(bǔ)碼存儲(chǔ)的,從左到右除了符號(hào)位之外每次從前面那個(gè)數(shù)取一個(gè)二進(jìn)制位,同時(shí)也從后面那個(gè)數(shù)取一個(gè)二進(jìn)制位,這種操作應(yīng)該可以用移位操作完成的。如果取到(1,0),那么就證明前面的那個(gè)數(shù)的絕對(duì)值是大的,那根據(jù)補(bǔ)碼的規(guī)則,絕對(duì)值大的對(duì)應(yīng)的真數(shù)反而小,因?yàn)樗?jīng)過(guò)了反碼,那這種感覺(jué)就和我們算數(shù)比較的結(jié)果是相悖的,我們算數(shù)比較的依據(jù)是,你絕對(duì)值大,那么你對(duì)應(yīng)的數(shù)就大。
有了移碼之后
1110?經(jīng)過(guò)移碼編碼之后就是0110,1010經(jīng)過(guò)移碼編碼之后就是0010。
0110?和0010,你很容易就看出哪個(gè)數(shù)大,哪個(gè)數(shù)小。
我在一篇pdf里面找到了一些觀點(diǎn)。
搜索關(guān)鍵字:Episode-3.05-Transcript.pdf?
In computing, this lexicographical order is important. There are times when computers must compare or
sort values. The circuitry to do this is simple if the patterns of ones and zeros are ordered from all zeros
representing the lowest value to all ones representing the highest. To compare two binary values, we
start with the left-most bit, and moving left to right, look for the first pair of bits where one of the bits is
a one and the other is a zero. The pattern containing the one is the larger of the two values.
This brings us to biased notation. Biased notation takes the ordered patterns of ones and zeros and
shifts them along the integer number line so that the pattern of all zeros is equivalent to the offset or
bias. Before doing any conversions, we need to define a bias, sometimes identified as K. This means that
the pattern of all zeros in biased notation using an offset of K is negative K. The remaining values
increment up from negative K
意思是說(shuō)?字典序很重要,比較數(shù)字的時(shí)候很常用。
我在zhihu上也看到類似的觀點(diǎn),說(shuō)是移碼在比較數(shù)字大小這方面很方便。相當(dāng)于把
[-127,-1]?映射到了[1,127] ,你負(fù)數(shù)不好比較大小,正數(shù)就好比較了唄,大概是這意思,我覺(jué)得這還挺好理解的。
有的網(wǎng)友也提出了不一樣的觀點(diǎn),說(shuō)兩個(gè)數(shù)比較大小,用CMP指令或者SUB指令就可以了,這不很簡(jiǎn)單的事情嗎??而且補(bǔ)碼就可以直接用CMP和SUB指令計(jì)算,為什么需要移碼?
我也挺疑惑的。
我還有一個(gè)小的疑問(wèn),既然移碼這么方便比較數(shù)大小,為什么只用在了浮點(diǎn)數(shù),為什么不把正數(shù)都用移碼來(lái)表示呢??
我目前確實(shí)沒(méi)體會(huì)到移碼的好處,好像和我們關(guān)系不太大,都是計(jì)算機(jī)直接幫我們給處理了。但我覺(jué)得平常有個(gè)小技巧可能會(huì)有用,就是比較兩個(gè)數(shù)大小,比如1101,和1111,這兩個(gè)數(shù),哪個(gè)大,哪個(gè)小,換成移碼0101 0111,很明顯后者大,確實(shí)很方便。
說(shuō)完了移碼再說(shuō)浮點(diǎn)數(shù)。
先來(lái)說(shuō)幾個(gè)概念
定點(diǎn)整數(shù),就是純整數(shù),比如15.0,1560.0
定點(diǎn)小數(shù),就是純小數(shù),比如0.1,0.1567
對(duì)于定點(diǎn)整數(shù)和定點(diǎn)小數(shù)我們完全可以按照整數(shù)的方式來(lái)存儲(chǔ),就是補(bǔ)碼那一套規(guī)則存儲(chǔ)即可。
但是如果遇到-15.3456789?這種數(shù),究竟?該怎么存儲(chǔ),這是一個(gè)問(wèn)題。
假設(shè)有32個(gè)二進(jìn)制位來(lái)存儲(chǔ)這個(gè)數(shù),究竟該分配多少個(gè)二進(jìn)制位來(lái)存儲(chǔ)整數(shù)部分,多少個(gè)二進(jìn)制位來(lái)存儲(chǔ)小數(shù)部分。
-15.3456789=-15+(-0.3456789)?
那是不是要把這兩個(gè)負(fù)數(shù)都得存儲(chǔ)起來(lái)。假設(shè)我們給整數(shù)部分分配8個(gè)二進(jìn)制位(其中一位是符號(hào)位),小數(shù)部分來(lái)存儲(chǔ)25個(gè)二進(jìn)制位(其中一位是符號(hào)位)。
8個(gè)二進(jìn)制位,可以表示[-128,127]這些數(shù),能表示的數(shù)的范圍太小了呀。
那,要不給整數(shù)部分分配16個(gè)二進(jìn)制位,可以表示[-32768,32767],這范圍也太小了呀,而且你給整數(shù)分配的二進(jìn)制位多了,那么意味著小數(shù)部分的精度就小了。
為什么用整數(shù)的存放方法倆存儲(chǔ)分?jǐn)?shù),表示的范圍這么少呢??因?yàn)闆](méi)有采用科學(xué)計(jì)數(shù)法
IEEE754其實(shí)就是針對(duì)分?jǐn)?shù)的科學(xué)計(jì)數(shù)法。
這個(gè)標(biāo)準(zhǔn)規(guī)定了,你必須把數(shù)先寫成2的幾次方?乘以某個(gè)小數(shù)的這種形式。然后存儲(chǔ)數(shù)據(jù)的時(shí)候,只存儲(chǔ)幾次方的那個(gè)數(shù)據(jù),假設(shè)我們分配8個(gè)二進(jìn)制位來(lái)表示,那么就可以表示2的-128到2的127?這個(gè)范圍的數(shù),這個(gè)存儲(chǔ)實(shí)數(shù)的方法比我們存儲(chǔ)整數(shù)那個(gè)方法好太多了,表示的范圍大太多了。