In []:
Some markdown content.
In [2]:
_Rational = namedtuple("_Rational", "num denom") _Rational = functools.total_ordering(_Rational) def rational(num, denom=1): return _Rational(num, denom).simplify()
In [3]:
def _(self): if self.denom == 1: return str(self.num) return f"{self.num} / {self.denom}" _Rational.__repr__ = _
In [4]:
def _(self): gcd = math.gcd(self.num, self.denom) return _Rational(self.num // gcd, self.denom // gcd) _Rational.simplify = _
In [5]:
rational(4, 2) rational(4, 2).simplify()
Out [5]:
2
Out [5]:
2