From 4baaed9d3c1b3ee623a856c8ca2127e71484e69f Mon Sep 17 00:00:00 2001 From: 7400 <> Date: Tue, 14 Nov 2023 06:58:51 -0800 Subject: [PATCH] WIP --- test/TestIEEE754.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 test/TestIEEE754.py diff --git a/test/TestIEEE754.py b/test/TestIEEE754.py new file mode 100755 index 0000000..44c5236 --- /dev/null +++ b/test/TestIEEE754.py @@ -0,0 +1,28 @@ +#!/usr/bin/python3 + +import struct +from fractions import Fraction + +def to_float_binary(value): + # Use 'struct.pack' to pack the value into bytes using IEEE 754 floating-point format + # '>f' specifies big-endian single-precision float. Change to '>d' for double precision. + packed = struct.pack('>f', value) + # Convert the bytes to an integer and then to a binary string + return ''.join(f'{byte:08b}' for byte in packed) + +def to_float_bits(number) : + float_bytes = struct.pack('>f', number) + return struct.unpack('>I', float_bytes)[0], float_bytes + +def fixedPoint(n, shift) : + if shift >= 0 : + return Fraction(n) * (1<