2015-07-21 81 views
6

在倍頻程中,命令format bit後面的所有數字輸出將顯示存儲在內存中的數字的本地位表示。例如,是否有與八度命令`格式位'相當的python?

octave:1> format bit 
octave:2> 0.5 
ans = 0011111111100000000000000000000000000000000000000000000000000000 
octave:7> 2 
ans = 0100000000000000000000000000000000000000000000000000000000000000 
octave:3> format 
octave:4> 0.5 
ans = 0.50000 

是否有在python等效的命令顯示所有在其天然位表示(因此輸出如下所示)的數量?

>>> "equivalent of the octave format bit command" 
>>> 0.5 
0011111111100000000000000000000000000000000000000000000000000000 

(這是從二進制表示非常不同0.5。)

+1

我懷疑它; Python不鼓勵知道或關心事物在內存中的存儲位置和方式。你想用它做什麼? – jonrsharpe

+0

相關:[Python中的float的二進制表示形式(位不是十六進制)](http://stackoverflow.com/q/16444726/953482)。這只是爲了漂浮。 – Kevin

+0

@凱文感謝您的鏈接,但它不是我所期待的。 @jonsharpe我發現它非常方便,在教授數值分析類時,使用八度中的「格式位」來解釋浮點表示法,歸一化與非規格化浮點數等。但是我想開始使用python作爲類,因此查詢。 –

回答

5

TL; DR;不,沒有Python等效物

當你使用Python時,你不應該需要這些信息。像使用八度一樣設置「輸出模式」是不可能的(不需要修改Python本身)。如果你真的想要所有輸出的格式都不是默認的,你可以編寫自定義函數,甚至可以覆蓋默認的print函數,如果你使用的是Python 3.

如果你只是想看到數字的二進制表示,可以使用bin(number)函數來計算整數。對於花車,您可以使用float.hex(number)

如果我們真的想看到內部的表現,它需要一些工作,一點點黑魔法和​​庫。使用Python獲取這些數據並不容易(或者無法使用),但是我創建了一個函數,它向我們展示了float在內部是如何表示的:

我們知道浮點數是浮點數,因此它們使用浮點數8個字節(64位)的內存。

import ctypes 

def show_float(x): 
    asdouble = ctypes.c_double(x) 
    xpointer = ctypes.addressof(asdouble) 
    xdata = ctypes.string_at(xpointer, 8) 
    print "".join([bin(ord(i))[2:] for i in xdata]) 

x = 3.14 
show_float(x) # prints 1111110000101111010111010001101110001111010011000000 

整數更難,因爲表示不是在所有實施一樣的。你可以找到一個例子here

+0

我只需要這個功能來向學生展示機器表示,所以一個函數很好地服務於我的目的(當然比「修改Python」更好)。使用你的想法,我想出了完全顯示出我需要的功能(張貼在下面),所以謝謝! –

+0

無關的問題:什麼是TL; DR;? (對不起,我是stackoverflow的新手。) –

+0

TL; DR; '太久了;沒有讀' –

1

根據@Hannes Karppila的回答,這裏有兩個函數,它們以二進制和十六進制格式顯示任意數字的機器表示。它使用與答案基本相同的邏輯,但用零填充輸出以顯示每個字節的「正確」長度。

import ctypes 
import decimal 
def print_as_octave_bit_hex(x): 
    ''' 
    This function prints the binary representation as it would 
    be printed using the format 'bit' and 'hex' in octave 
    ''' 
    asdouble = ctypes.c_double(x) 
    xpointer = ctypes.addressof(asdouble) 
    xdata = ctypes.string_at(xpointer, 8) 
    xbin= [(bin(i)[2:].zfill(8)) for i in xdata[-1::-1]] 
    print(x, "=", x.hex(), "=", decimal.Decimal(x)) 
    print("representation in format 'bit' and 'hex' of octave") 
    print("with spaces separating each byte") 
    print(" ".join([i.zfill(8) for i in xbin]), "=", 
     " ".join([hex(int(i,2))[2:].zfill(2) for i in xbin])) 
    print("without spaces separating the bytes") 
    print("".join([i.zfill(8) for i in xbin]), "=", 
     "".join([hex(int(i,2))[2:].zfill(2) for i in xbin])) 

def print_as_octave_native_bit_hex(x): 
    ''' 
    This function prints the binary representation as it would 
    be printed using the format 'native-bit' and 'native-hex' in octave 
    ''' 
    asdouble = ctypes.c_double(x) 
    xpointer = ctypes.addressof(asdouble) 
    xdata = ctypes.string_at(xpointer, 8) 
    xbin = [(bin(i)[2:].zfill(8)) for i in xdata] 
    print(x, "=", x.hex(), "=", decimal.Decimal(x)) 
    print("representation in format 'native-bit' and 'native-hex' of octave") 
    print("with spaces separating each byte") 
    print(" ".join([(i.zfill(8))[-1::-1] for i in xbin]), "=", 
     " ".join([hex(int(i,2))[2:].zfill(2) for i in xbin])) 
    print("without spaces separating the bytes") 
    print("".join([(i.zfill(8))[-1::-1] for i in xbin]), "=", 
     "".join([hex(int(i,2))[2:].zfill(2) for i in xbin])) 

x=1.1+2.2 
print_as_octave_bit_hex(x) 
print(" ") 
print_as_octave_native_bit_hex(x) 
3.3000000000000003 = 0x1.a666666666667p+1 = 3.300000000000000266453525910037569701671600341796875 
representation in format 'bit' and 'hex' of octave 
with spaces separating each byte 
01000000 00001010 01100110 01100110 01100110 01100110 01100110 01100111 = 40 0a 66 66 66 66 66 67 
without spaces separating the bytes 
0100000000001010011001100110011001100110011001100110011001100111 = 400a666666666667 

3.3000000000000003 = 0x1.a666666666667p+1 = 3.300000000000000266453525910037569701671600341796875 
representation in format 'native-bit' and 'native-hex' of octave 
with spaces separating each byte 
11100110 01100110 01100110 01100110 01100110 01100110 01010000 00000010 = 67 66 66 66 66 66 0a 40 
without spaces separating the bytes 
1110011001100110011001100110011001100110011001100101000000000010 = 6766666666660a40 
+0

謝謝,他們是很好的補充。 –