# probe: a normalised fraction type with arithmetic and printing # status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt # expect-stdout: 1/2 1/3 # expect-stdout: 5/6 # expect-stdout: 1/6 # expect-stdout: -1/3 def gcd(a: Int, b: Int) -> Int: var x = a if a >= 0 else -a var y = b if b >= 0 else -b while y != 0: var t = x % y x = y y = t return x @fieldwise_init struct Frac(Copyable, Movable, Writable): var num: Int var den: Int @staticmethod def make(n: Int, d: Int) -> Frac: var g = gcd(n, d) var sign = -1 if d < 0 else 1 return Frac(sign * n // g, sign * d // g) def __add__(self, other: Self) -> Self: return Frac.make(self.num * other.den + other.num * self.den, self.den * other.den) def __mul__(self, other: Self) -> Self: return Frac.make(self.num * other.num, self.den * other.den) def write_to(self, mut writer: Some[Writer]): writer.write(self.num, "/", self.den) def main(): var half = Frac.make(1, 2) var third = Frac.make(2, 6) print(half, third) print(half + third) print(half * third) print(Frac.make(3, -9))