Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix intermediate integer overflow in math.lcm and math.hypot
  • Loading branch information
ekMartian committed Feb 20, 2026
commit 50471ccd4438a5e4aa36860b9c6226af2bc59c7c
10 changes: 7 additions & 3 deletions src/runtime/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,9 @@ def lcm(a: i32, b: i32) -> i32:
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:
Expand All @@ -517,7 +517,11 @@ 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
yf: f64
xf = f64(x)
yf = f64(y)
return sqrt(xf**2.0 + yf**2.0)

@overload
def trunc(x: f64) -> i64:
Expand Down
Loading