有没有相当于octave命令`format bit`的python?

时间:2015-07-21 16:24:42

标签: python

在八度音程中,命令format bit之后的所有数字输出将显示存储在存储器中的数字的本机位表示。例如,

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的二进制表示非常不同。)

2 个答案:

答案 0 :(得分:5)

TL; DR;不,没有Python等价物

使用Python时,不应该需要此信息。像使用八度音程一样设置“输出模式”是不可能的(不修改Python本身)。如果你真的希望所有输出都是默认的其他格式,你可以为它编写自定义函数,如果使用的是Python 3,甚至可以覆盖默认的print函数。

如果您只想查看数字的二进制表示,可以使用bin(number)函数作为整数。对于花车,您可以使用float.hex(number)

如果我们真的想看到内部的代表,那就需要一些工作,一点点黑魔法和ctypes库。使用Python时获取这些数据并不容易(或无论如何可用),但我创建的函数向我们展示了内部表示float的方式:

我们知道Python浮点数中的浮点数是IEEE 754 double precision个浮点数,因此它们使用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

答案 1 :(得分: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