Wie auf dieser Webiste gesehen: https://www.h-schmidt.net/FloatConverter/IEEE754.htmlPython Floats auf 32 Bit begrenzte Anzahl
Ich habe durch fünf oder so zum Thema Stapelüberlauf aussieht, aber sie sind nicht ganz das, was ich suche. Zum Beispiel:
import struct
getBin = lambda x: x > 0 and str(bin(x))[2:] or "-" + str(bin(x))[3:]
def floatToBinary64(value):
val = struct.unpack('Q', struct.pack('d', value))[0]
return getBin(val)
def binaryToFloat(value):
hx = hex(int(value, 2))
return struct.unpack("d", struct.pack("q", int(hx, 16)))[0]
# floats are represented by IEEE 754 floating-point format which are
# 64 bits long (not 32 bits)
# float to binary
binstr = floatToBinary64(NUMBER)
print('Binary equivalent of .1:')
print(binstr + '\n')
# binary to float
fl = binaryToFloat(binstr)
print('Decimal equivalent of ' + binstr)
print(fl)
Ich weiß, dass dies ziemlich nah an dem ist, was ich suche. Wenn Sie .1 in diesen Code schreiben, wo Nummer ist, listet es als .1 auf, während es auf der Website gibt es mir: 0.10000000149011612.
Wenn jemand helfen kann, wäre das toll!
Welche Website gibt Ihnen diese Antwort? EDIT: Entschuldigung, es ist ganz oben ... duh – BretD
Warum sagt der Titel 32? –
Ich bin immer noch verwirrt über die Frage ... Also die Website sagt 32-Bit-Floats, Python verwendet 64-Bit-Floats, und Sie sind verwirrt, warum die Werte unterschiedlich sind? – BretD