Skip to content

Commit 50471cc

Browse files
committed
Fix intermediate integer overflow in math.lcm and math.hypot
1 parent 6b00cba commit 50471cc

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/runtime/math.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,9 @@ def lcm(a: i32, b: i32) -> i32:
498498
a_ = -a_
499499
if b_ < 0:
500500
b_ = -b_
501-
if a_*b_ == 0:
501+
if a_ == 0 or b_ == 0:
502502
return 0
503-
return i32((a_*b_)//gcd(a_, b_))
503+
return i32((a_ // gcd(a_, b_)) * b_)
504504

505505

506506
def copysign(x: f64, y: f64) -> f64:
@@ -517,7 +517,11 @@ def hypot(x: i32, y: i32) -> f64:
517517
"""
518518
Returns the hypotenuse of the right triangle with sides `x` and `y`.
519519
"""
520-
return sqrt(f64(1.0)*f64(x**2 + y**2))
520+
xf: f64
521+
yf: f64
522+
xf = f64(x)
523+
yf = f64(y)
524+
return sqrt(xf**2.0 + yf**2.0)
521525

522526
@overload
523527
def trunc(x: f64) -> i64:

0 commit comments

Comments
 (0)