Skip to content

Commit 4765d0a

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

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/runtime/math.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,17 +490,15 @@ def lcm(a: i32, b: i32) -> i32:
490490
"""
491491
Returns least common multiple of `a` and `b`
492492
"""
493-
a_: i32
494-
b_: i32
495-
a_ = a
496-
b_ = b
493+
a_: i32 = a
494+
b_: i32 = b
497495
if a_ < 0:
498496
a_ = -a_
499497
if b_ < 0:
500498
b_ = -b_
501-
if a_*b_ == 0:
499+
if a_ == 0 or b_ == 0:
502500
return 0
503-
return i32((a_*b_)//gcd(a_, b_))
501+
return i32((a_ // gcd(a_, b_)) * b_)
504502

505503

506504
def copysign(x: f64, y: f64) -> f64:
@@ -517,7 +515,9 @@ def hypot(x: i32, y: i32) -> f64:
517515
"""
518516
Returns the hypotenuse of the right triangle with sides `x` and `y`.
519517
"""
520-
return sqrt(f64(1.0)*f64(x**2 + y**2))
518+
xf: f64 = f64(x)
519+
yf: f64 = f64(y)
520+
return sqrt(xf**2 + yf**2)
521521

522522
@overload
523523
def trunc(x: f64) -> i64:

0 commit comments

Comments
 (0)