問題描述
我正在嘗試將浮點數(shù)轉換為二進制表示;我怎樣才能做到這一點?但是,我的目標是不受 2m 的限制,因此我希望可以輕松擴展到任何基礎 (3, 4, 8) ecc.
I'm trying to convert a floating point number to binary representation; how can I achieve this? My goal is, however, not to be limited by 2m so I'm hoping for something that could be easily extended to any base (3, 4, 8) ecc.
到目前為止,我有一個簡單的整數(shù)實現(xiàn):
I've got a straightforward implementation so far for integers:
import string
LETTER = '0123456789' + string.ascii_lowercase
def convert_int(num, base):
if base == 1:
print "WARNING! ASKING FOR BASE = 1"
return '1' * num if num != 0 else '0'
if base > 36: raise ValueError('base must be >= 1 and <= 36')
num, rest = divmod(num, base)
rest = [LETTER[rest]]
while num >= base:
num, r = divmod(num, base)
rest.append(LETTER[r])
rest.reverse()
return (LETTER[num] if num else '') + ''.join(str(x) for x in rest)
任何幫助表示贊賞:)
def convert_float(num, base, digits=None):
num = float(num)
if digits is None: digits = 6
num = int(round(num * pow(base, digits)))
num = convert_int(num, base)
num = num[:-digits] + '.' + num[:digits]
if num.startswith('.'): num = '0' + num
return num
對嗎?為什么我會出現(xiàn)這種行為?
is that right? why do i get this behaviour?
>>> convert_float(1289.2893, 16)
'509.5094a0'
>>> float.hex(1289.2983)
'0x1.42531758e2196p+10'
附言如何將浮點數(shù)轉換為二進制?
我已經閱讀了該討論,但我沒有得到答案.我的意思是,它僅適用于 0.25、0.125 嗎?而且我不明白必須以相反的順序"這句話......
I've read that discussion, but I don't get the answer.. I mean, does it work only for 0.25, 0.125? and I dont understand the phrase 'must be in reverse order'...
推薦答案
下一個答案,有點理論.
Next answer with a bit of theory.
下面的解釋并不解釋 IEEE 浮點標準,只是關于浮點數(shù)表示的一般概念
每個浮點數(shù)都表示為小數(shù)部分乘以指數(shù)乘以符號.另外還有所謂的指數(shù)偏差,下面會解釋.
Every float number is represented as a fractional part multiplied by an exponent multiplied by a sign. Additionally there is so called bias for exponent, which will be explained bellow.
所以我們有
- 符號位
- 小數(shù)部分數(shù)字
- 指數(shù)部分數(shù)字
帶有 8 位小數(shù)和 8 位指數(shù)的以 2 為底的示例
小數(shù)部分的位告訴我們下面序列中的哪些加數(shù)(要添加的數(shù)字)將包含在表示的數(shù)字值中
Bits in fraction part tell us which summands (numbers to be added) from sequence below are to be included in represented number value
2^-1 + 2^-2 + 2^-3 + 2^-4 + 2^-5 + 2^-6 + 2^-7 + 2^-8
2^-1 + 2^-2 + 2^-3 + 2^-4 + 2^-5 + 2^-6 + 2^-7 + 2^-8
因此,如果您在小數(shù)部分中說 01101101,它會給出
So if you have say 01101101 in fractional part it gives
0*2^-1 + 1*2^-2 + 1*2^-3 + 0*2^-4 + 1*2^-5 + 1*2^-6 + 0*2^-7 + 1*2^-8 = 0.42578125
0*2^-1 + 1*2^-2 + 1*2^-3 + 0*2^-4 + 1*2^-5 + 1*2^-6 + 0*2^-7 + 1*2^-8 = 0.42578125
現(xiàn)在可以以這種方式表示的非零數(shù)字介于2 ** -8 = 0.00390625 和 1 - 2**-8 = 0.99609375
Now non-zero numbers that are representable that way fall between 2 ** -8 = 0.00390625 and 1 - 2**-8 = 0.99609375
這里是指數(shù)部分.指數(shù)允許我們通過將小數(shù)部分乘以指數(shù)來表示非常大的數(shù)字.因此,如果我們有一個 8 位指數(shù),我們可以將得到的分數(shù)乘以 0 到 2^255 之間的數(shù)字.
Here the exponent part comes in. Exponent allows us to represent very big numbers by multiplying the fraction part by exponent. So if we have an 8bit exponent we can multiply the resulting fraction by numbers between 0 and 2^255.
回到上面的例子,讓我們取 11000011 = 195 的指數(shù).
So going back to example above let's take exponent of 11000011 = 195.
我們有 01101101 = 0.42578125 的小數(shù)部分和 11000011 = 195 的指數(shù)部分.它給我們的數(shù)字是 0.42578125 * 2^195,這是一個非常大的數(shù)字.
We have fractional part of 01101101 = 0.42578125 and exponent part 11000011 = 195. It gives us the number 0.42578125 * 2^195, this is really big number.
到目前為止,我們可以表示 2^-8 * 2^0 和 (1-2^-8) * 2^255 之間的非零數(shù)字.這允許非常大的數(shù)字,但不允許非常小的數(shù)字.為了能夠表示小數(shù),我們必須在指數(shù)中包含所謂的偏差.它是一個總是從指數(shù)中減去的數(shù)字,以便表示小數(shù)字.
So far we can represent non-zero numbers between 2^-8 * 2^0 and (1-2^-8) * 2^255. This allows for very big numbers but not for very small numbers. In order to be able to represent small numbers we have to include so called bias in our exponent. It is a number that will be always subtracted from exponent in order to allow for representation of small numbers.
假設偏差為 127.現(xiàn)在所有指數(shù)都減去 127.因此可以表示的數(shù)字介于 2^-8 * 2^(0 - 127) 和 (1-2^-8) * 2^(255 - 127 = 128)
Let's take a bias of 127. Now all exponents are subtracted 127. So numbers that can be represented are between 2^-8 * 2^(0 - 127) and (1-2^-8) * 2^(255 - 127 = 128)
示例數(shù)字現(xiàn)在是 0.42578125 * 2^(195-127 = 68),這仍然很大.
Example number is now 0.42578125 * 2^(195-127 = 68) which is still pretty big.
示例結束
為了更好地理解這一點,請嘗試對分數(shù)和指數(shù)部分使用不同的基數(shù)和大小.一開始不要嘗試奇怪的基礎,因為它只會使必要的事情復雜化.
In order to understand this better try to experiment with different bases and sizes for fractional and exponential part. At beginning don't try with odd bases because it only complicates things necessary.
一旦您掌握了這種表示的工作原理,您應該能夠編寫代碼來獲得以任何基數(shù)、小數(shù)/指數(shù)部分組合表示的任何數(shù)字.
Once you grasp how this representation works you should be able to write code to obtain representation of any number in any base, fractional/exponential part combination.
這篇關于浮點數(shù)轉二進制的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!