From ffae9e5393d11f73c6994e3e87beb9def108ea5f Mon Sep 17 00:00:00 2001 From: sedaozkaya <155262567+sedaozkaya@users.noreply.github.com> Date: Sat, 20 Dec 2025 20:37:29 +0300 Subject: [PATCH] Add HalfPrecision class with IEEE 754 half-precision support --- Week06/halfprecision_sedasengul_ozkaya.py | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Week06/halfprecision_sedasengul_ozkaya.py diff --git a/Week06/halfprecision_sedasengul_ozkaya.py b/Week06/halfprecision_sedasengul_ozkaya.py new file mode 100644 index 0000000..6cdb778 --- /dev/null +++ b/Week06/halfprecision_sedasengul_ozkaya.py @@ -0,0 +1,46 @@ +import math + +class HalfPrecision: + def __init__(self, number): + if type(number) is not float: + raise TypeError("Input must be a float") + self.number = number + + def __str__(self): + x = self.number + sign = "1" if math.copysign(1.0, x) < 0 else "0" + x = abs(x) + + if x == 0.0: + return sign + "00000" + "0000000000" + + if math.isinf(x): + return sign + "11111" + "0000000000" + + m, e = math.frexp(x) + e -= 1 + m *= 2 + bias = 15 + exp = e + bias + + # Normalized + if exp > 0: + if exp >= 31: + return sign + "11111" + "0000000000" + + frac = m - 1 + mant = int(frac * 1024 + 0.5) + + if mant == 1024: + mant = 0 + exp += 1 + if exp >= 31: + return sign + "11111" + "0000000000" + + return sign + format(exp, "05b") + format(mant, "010b") + + # Denormal + mant = int(x / (2 ** -14) * 1024 + 0.5) + mant = min(mant, 1023) + + return sign + "00000" + format(mant, "010b")