久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

浮點(diǎn)數(shù)轉(zhuǎn)二進(jìn)制

Float to binary(浮點(diǎn)數(shù)轉(zhuǎn)二進(jìn)制)
本文介紹了浮點(diǎn)數(shù)轉(zhuǎn)二進(jìn)制的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試將浮點(diǎn)數(shù)轉(zhuǎn)換為二進(jìn)制表示;我怎樣才能做到這一點(diǎn)?但是,我的目標(biāo)是不受 2m 的限制,因此我希望可以輕松擴(kuò)展到任何基礎(chǔ) (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.

到目前為止,我有一個(gè)簡(jiǎn)單的整數(shù)實(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

對(duì)嗎?為什么我會(huì)出現(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'

附言如何將浮點(diǎn)數(shù)轉(zhuǎn)換為二進(jìn)制?

我已經(jīng)閱讀了該討論,但我沒有得到答案.我的意思是,它僅適用于 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'...

推薦答案

下一個(gè)答案,有點(diǎn)理論.

Next answer with a bit of theory.

下面的解釋并不解釋 IEEE 浮點(diǎn)標(biāo)準(zhǔn),只是關(guān)于浮點(diǎn)數(shù)表示的一般概念

每個(gè)浮點(diǎn)數(shù)都表示為小數(shù)部分乘以指數(shù)乘以符號(hào).另外還有所謂的指數(shù)偏差,下面會(huì)解釋.

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.

所以我們有

  1. 符號(hào)位
  2. 小數(shù)部分?jǐn)?shù)字
  3. 指數(shù)部分?jǐn)?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,它會(huì)給出

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ù)字.因此,如果我們有一個(gè) 8 位指數(shù),我們可以將得到的分?jǐn)?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.

回到上面的例子,讓我們?nèi)?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,這是一個(gè)非常大的數(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ù)中包含所謂的偏差.它是一個(gè)總是從指數(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.

假設(shè)偏差為 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.

示例結(jié)束

為了更好地理解這一點(diǎn),請(qǐng)嘗試對(duì)分?jǐn)?shù)和指數(shù)部分使用不同的基數(shù)和大小.一開始不要嘗試奇怪的基礎(chǔ),因?yàn)樗粫?huì)使必要的事情復(fù)雜化.

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.

一旦您掌握了這種表示的工作原理,您應(yīng)該能夠編寫代碼來獲得以任何基數(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.

這篇關(guān)于浮點(diǎn)數(shù)轉(zhuǎn)二進(jìn)制的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 99免费视频 | 久久精品久久久 | 一区二区三区国产精品 | 韩日有码 | 国产亚洲成av人片在线观看桃 | 国产伦精品一区二区三区高清 | 日韩精品一区二区三区视频播放 | 国产精品伦理一区二区三区 | 午夜av毛片| 久久av一区二区三区 | 精品成人一区二区 | 久久精品综合 | www.狠狠干| 国产精品2区 | 久久精品小视频 | 欧美三区| 亚洲精品免费看 | 激情一区二区三区 | 免费视频99 | 国产精品欧美一区二区三区不卡 | 国产在线一区二区三区 | 免费黄色日本 | 久久香焦| 9色视频在线| 国产精品入口久久 | 中文字幕国产在线 | 日本欧美在线观看视频 | 亚洲在线| 欧美视频精品 | 国产婷婷色综合av蜜臀av | 一区二区三区四区在线播放 | 九九av| 一级欧美 | 成人av电影天堂 | 欧美日韩精品久久久免费观看 | 日本精品久久 | 欧美日韩亚洲一区 | 91久久久久 | 精品二区| 成人午夜免费在线视频 | 日本激情视频在线播放 |