>>> n = 256 >>> n.to_bytes(length=1, byteorder="big") Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: int too big to convert
classExtendedInteger(numbers.Real): def__init__(self, a, b, p = 2) -> None: self.a = a self.b = b self.p = p self._val = a + (b * math.sqrt(p)) def__repr__(self): returnf"{self.__class__.__name__}({self.a}, {self.b}, {self.p})" def__str__(self): returnf"{self.a} + {self.b}√{self.p}" def__trunc__(self): return int(self._val) def__float__(self): return float(self._val) def__hash__(self): return hash(float(self._val)) def__floor__(self): return math.floot(self._val) def__ceil__(self): return math.ceil(self._val) def__round__(self, ndigits=None): return round(self._val, ndigits=ndigits) def__abs__(self): return abs(self._val) def__floordiv__(self, other): return self._val // other def__rfloordiv__(self, other): return other // self._val def__truediv__(self, other): return self._val / other def__rtruediv__(self, other): return other / self._val def__mod__(self, other): return self._val % other def__rmod__(self, other): return other % self._val def__lt__(self, other): return self._val < other def__le__(self, other): return self._val <= other def__eq__(self, other): return float(self) == float(other) def__neg__(self): return ExtendedInteger(-self.a, -self.b, self.p) def__pos__(self): return ExtendedInteger(+self.a, +self.b, self.p) def__add__(self, other): if isinstance(other, ExtendedInteger): # If both instances have the same p value, # return a new ExtendedInteger instance if self.p == other.p: new_a = self.a + other.a new_b = self.b + other.b return ExtendedInteger(new_a, new_b, self.p) # Otherwise return a float else: return self._val + other._val # If other is integral, add other to self's a value elif isinstance(other, numbers.Integral): new_a = self.a + other return ExtendedInteger(new_a, self.b, self.p) # If other is real, return a float elif isinstance(other, numbers.Real): return self._val + other._val # If other is of unknown type, let other determine # what to do else: returnNotImplemented def__radd__(self, other): # Addition is commutative so defer to __add__ return self.__add__(other) def__mul__(self, other): if isinstance(other, ExtendedInteger): # If both instances have the same p value, # return a new ExtendedInteger instance if self.p == other.p: new_a = (self.a * other.a) + (self.b * other.b * self.p) new_b = (self.a * other.b) + (self.b * other.a) return ExtendedInteger(new_a, new_b, self.p) # Otherwise, return a float else: return self._val * other._val # If other is integral, multiply self's a and b by other elif isinstance(other, numbers.Integral): new_a = self.a * other new_b = self.b * other return ExtendedInteger(new_a, new_b, self.p) # If other is real, return a float elif isinstance(other, numbers.Real): return self._val * other # If other is of unknown type, let other determine # what to do else: returnNotImplemented def__rmul__(self, other): # Multiplication is commutative so defer to __mul__ return self.__mul__(other) def__pow__(self, exponent): return self._val ** exponent def__rpow__(self, base): return base ** self._val