I have created a Python class for representing a BASIC-Float:
Code: Select all
#!/usr/bin/env python
import math
import decimal
def unsigned8(x):
""" convert a signed 8-Bit value into a unsigned value """
if x < 0:
x = x + 0x0100 # 0x100 == 2**8 == 256
return x
class FloatingPoint(object):
"""
exponent: 1Byte = 8 Bits (most significant bit is the sign 1=positive 0=negative)
mantissa/fraction: 4Bytes = 32 Bits
sign of mantissa: 1Byte = 8 Bits (0x00 positive, 0xff negative)
"""
def __init__(self, value):
self.value = decimal.Decimal(value)
self.mantissa, self.exponent = math.frexp(value)
self.exponent_byte = unsigned8(self.exponent-128)
if self.mantissa>=0:
self.mantissa_sign = 0x00
else:
self.mantissa_sign = 0xff
self.mantissa_bytes = self.mantissa2bytes(self.mantissa)
def mantissa2bytes(self, value, bytes=4):
value=decimal.Decimal(abs(value))
result = []
pos = 0
for byte_no in xrange(bytes):
current_byte = 0
for bit_no in reversed(xrange(1,8)):
pos +=1
bit_value = decimal.Decimal(1.0) / decimal.Decimal(2) ** decimal.Decimal(pos)
if value >= bit_value:
value -= bit_value
current_byte += 2**bit_no
result.append(current_byte)
return result
def get_bytes(self):
return [self.exponent_byte]+self.mantissa_bytes+[self.mantissa_sign]
def print_values(self):
print "Float value was: %s" % self.value
print "\texponent......: dez.: %s hex: $%02x" % (self.exponent,self.exponent)
print "\texponent byte.: dez.: %s hex: $%02x" % (
self.exponent_byte,self.exponent_byte
)
print "\tmantissa value: dez.: %s" % (self.mantissa)
print "\tmantissa bytes: dez.: %s hex: %s" % (
repr(self.mantissa_bytes),
", ".join(["$%02x" % i for i in self.mantissa_bytes])
)
print "\tmatissa-sign..: hex: $%02x" % self.mantissa_sign
bytes = self.get_bytes()
print "\tbinary........: hex: %s" % (
", ".join(["$%02x" % i for i in bytes])
)
print "\texponent | mantissa | mantissa-sign"
print "\t" + " ".join(
['{0:08b}'.format(i) for i in bytes]
)
print
def __repr__(self):
return "<BinaryFloatingPoint %f: %s>" % (
self.value, ", ".join(["$%02x" % i for i in self.get_bytes()])
)
FloatingPoint(54).print_values()
FloatingPoint(-54).print_values()
FloatingPoint(5.5).print_values()
FloatingPoint(-5.5).print_values()
FloatingPoint(0).print_values()
FloatingPoint(10.14**38).print_values()
FloatingPoint(10.14**-38).print_values()
with syntax highlighting ->
https://gist.github.com/jedie/94f8d310bb88fdb0d188
Example output is:
Code: Select all
Float value was: 54
exponent......: dez.: 6 hex: $06
exponent byte.: dez.: 134 hex: $86
mantissa value: dez.: 0.84375
mantissa bytes: dez.: [216, 0, 0, 0] hex: $d8, $00, $00, $00
matissa-sign..: hex: $00
binary........: hex: $86, $d8, $00, $00, $00, $00
exponent | mantissa | mantissa-sign
10000110 11011000 00000000 00000000 00000000 00000000
Float value was: -54
exponent......: dez.: 6 hex: $06
exponent byte.: dez.: 134 hex: $86
mantissa value: dez.: -0.84375
mantissa bytes: dez.: [216, 0, 0, 0] hex: $d8, $00, $00, $00
matissa-sign..: hex: $ff
binary........: hex: $86, $d8, $00, $00, $00, $ff
exponent | mantissa | mantissa-sign
10000110 11011000 00000000 00000000 00000000 11111111
Float value was: 5.5
exponent......: dez.: 3 hex: $03
exponent byte.: dez.: 131 hex: $83
mantissa value: dez.: 0.6875
mantissa bytes: dez.: [176, 0, 0, 0] hex: $b0, $00, $00, $00
matissa-sign..: hex: $00
binary........: hex: $83, $b0, $00, $00, $00, $00
exponent | mantissa | mantissa-sign
10000011 10110000 00000000 00000000 00000000 00000000
Float value was: -5.5
exponent......: dez.: 3 hex: $03
exponent byte.: dez.: 131 hex: $83
mantissa value: dez.: -0.6875
mantissa bytes: dez.: [176, 0, 0, 0] hex: $b0, $00, $00, $00
matissa-sign..: hex: $ff
binary........: hex: $83, $b0, $00, $00, $00, $ff
exponent | mantissa | mantissa-sign
10000011 10110000 00000000 00000000 00000000 11111111
Float value was: 0
exponent......: dez.: 0 hex: $00
exponent byte.: dez.: 128 hex: $80
mantissa value: dez.: 0.0
mantissa bytes: dez.: [0, 0, 0, 0] hex: $00, $00, $00, $00
matissa-sign..: hex: $00
binary........: hex: $80, $00, $00, $00, $00, $00
exponent | mantissa | mantissa-sign
10000000 00000000 00000000 00000000 00000000 00000000
Float value was: 169606421016759270908588940533105164288
exponent......: dez.: 127 hex: $7f
exponent byte.: dez.: 255 hex: $ff
mantissa value: dez.: 0.996856948842
mantissa bytes: dez.: [254, 152, 128, 138] hex: $fe, $98, $80, $8a
matissa-sign..: hex: $00
binary........: hex: $ff, $fe, $98, $80, $8a, $00
exponent | mantissa | mantissa-sign
11111111 11111110 10011000 10000000 10001010 00000000
Float value was: 5.8960031937775952846575658526371605536051129075884808536027556193767507219429130357164845760358320471417048480589073733426630496978759765625E-39
exponent......: dez.: -126 hex: $-7e
exponent byte.: dez.: 2 hex: $02
mantissa value: dez.: 0.501576480538
mantissa bytes: dez.: [128, 50, 212, 30] hex: $80, $32, $d4, $1e
matissa-sign..: hex: $00
binary........: hex: $02, $80, $32, $d4, $1e, $00
exponent | mantissa | mantissa-sign
00000010 10000000 00110010 11010100 00011110 00000000
Don't know if this all is correct.
JeeK wrote:for space, e.g. storing values in variables or variable arrays: the sign is encoded in the first leading bit of the mantissa which is always 1, because the mantissa is normalized (starts always with 0.1xxxxx).
Then it looks simmilar to the C64 example here:
http://www.c64-wiki.com/index.php/Float ... on_example
But here the Exponent is calculated with: 152 -
129 = 23
But Inside Dragon says an page 232:
128 must be subtracted.