Created
October 13, 2022 15:45
-
-
Save thecharlieblake/97451bb8c55c8b6ecef975de89367535 to your computer and use it in GitHub Desktop.
A representation of the two's complement format used for INT8.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""A representation of the two's complement format used for INT8.""" | |
from dataclasses import dataclass | |
from functools import total_ordering | |
from typing import Any | |
@dataclass | |
class TwosComplementFormat: | |
"""Can be used to express any two's complement signed integer.""" | |
width: int | |
@property | |
def abs_min(self) -> float: | |
"""Absolute minimum representable value.""" | |
return 1 | |
@property | |
def abs_max(self) -> float: | |
"""Absolute maximum representable value.""" | |
return float(2 ** (self.width - 1) - 1) | |
@dataclass | |
class ScaledTwosComplementFormat(TwosComplementFormat): | |
"""A scaled (multiply all values by `scale`) version of `TwosComplementFormat`.""" | |
scale: int | |
@property | |
def abs_min(self) -> float: | |
return super().abs_min * self.scale | |
@property | |
def abs_max(self) -> float: | |
return super().abs_max * self.scale | |
@dataclass | |
@total_ordering | |
class TwosComplementInstance: | |
"""An instance of a number using a (possibly scaled) `TwosComplementFormat`.""" | |
format: TwosComplementFormat | |
uint: int | |
def __post_init__(self) -> None: | |
assert 0 <= self.uint < 2**self.format.width, self.uint | |
@property | |
def value(self) -> float: | |
"""The numerical value of the bitstring, | |
as defined by the two's complement format. | |
""" | |
if self.uint <= self.format.abs_max: | |
return self.uint | |
return float(self.uint - 2**self.format.width) | |
def __repr__(self) -> str: | |
return str(self.value) | |
def __eq__(self, other: Any) -> bool: | |
return self.value.__eq__(other) | |
def __lt__(self, other: Any) -> bool: | |
return self.value.__lt__(other) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment