From 4765d0ac7d259cce042e550d2259a67cde908893 Mon Sep 17 00:00:00 2001 From: Arnav Kulshreshtha Date: Fri, 20 Feb 2026 18:19:57 +0530 Subject: [PATCH] Fix intermediate integer overflow in math.lcm and math.hypot --- src/runtime/math.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/runtime/math.py b/src/runtime/math.py index 8993ce1223..39be5b9abc 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -490,17 +490,15 @@ def lcm(a: i32, b: i32) -> i32: """ Returns least common multiple of `a` and `b` """ - a_: i32 - b_: i32 - a_ = a - b_ = b + a_: i32 = a + b_: i32 = b if a_ < 0: a_ = -a_ if b_ < 0: b_ = -b_ - if a_*b_ == 0: + if a_ == 0 or b_ == 0: return 0 - return i32((a_*b_)//gcd(a_, b_)) + return i32((a_ // gcd(a_, b_)) * b_) def copysign(x: f64, y: f64) -> f64: @@ -517,7 +515,9 @@ def hypot(x: i32, y: i32) -> f64: """ Returns the hypotenuse of the right triangle with sides `x` and `y`. """ - return sqrt(f64(1.0)*f64(x**2 + y**2)) + xf: f64 = f64(x) + yf: f64 = f64(y) + return sqrt(xf**2 + yf**2) @overload def trunc(x: f64) -> i64: