Skip to content

Commit d88919c

Browse files
committed
Implement math.hypot; modify tests
1 parent d1aecc4 commit d88919c

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

integration_tests/test_math.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow,
2-
ldexp, fabs, gcd, lcm, copysign, hypot)
2+
ldexp, fabs, gcd, lcm)
33
from ltypes import i32, f64
44

55

@@ -84,24 +84,6 @@ def test_lcm():
8484
assert i == 84
8585

8686

87-
def test_copysign():
88-
f: f64
89-
f = copysign(-8.56, 97.21)
90-
assert f == 8.56
91-
f = copysign(-43.0, -76.67)
92-
assert f == -43.0
93-
94-
95-
def test_hypot():
96-
f: f64
97-
f = hypot(3, 4)
98-
assert f == 5.0
99-
f = hypot(-3, 4)
100-
assert f == 5.0
101-
f = hypot(6, 6)
102-
assert f == 8.485281374238571
103-
104-
10587
test_factorial_1()
10688
test_comb()
10789
test_isqrt()
@@ -114,5 +96,3 @@ def test_hypot():
11496
test_ldexp()
11597
test_gcd()
11698
test_lcm()
117-
test_copysign()
118-
test_hypot()

integration_tests/test_math_02.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from math import (sin, cos, tan, pi, sqrt, log, log10, log2, erf, erfc, gamma,
22
lgamma, asin, acos, atan, atan2, asinh, acosh, atanh,
3-
tanh, sinh, cosh)
3+
tanh, sinh, cosh, hypot, copysign)
44

55
def test_trig():
66
# TODO: importing from `math` doesn't work here yet:
@@ -46,7 +46,20 @@ def test_hyperbolic():
4646
assert abs(cosh(1.0) - 0) < eps
4747
assert abs(tanh(0.0) - 0) < eps
4848

49+
def test_copysign():
50+
eps: f64 = 1e-12
51+
assert abs(copysign(-8.56, 97.21) - 8.56) < eps
52+
assert abs(copysign(-43.0, -76.67) - (-43.0)) < eps
53+
54+
def test_hypot():
55+
eps: f64 = 1e-12
56+
assert abs(hypot(3, 4) - 5.0) < eps
57+
assert abs(hypot(-3, 4) - 5.0) < eps
58+
assert abs(hypot(6, 6) - 8.48528137423857) < eps
59+
4960
test_trig()
5061
test_sqrt()
5162
test_log()
5263
test_special()
64+
test_copysign()
65+
test_hypot()

src/runtime/math.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ def copysign(x: f64, y: f64) -> f64:
151151
return -fabs(x)
152152

153153

154+
def hypot(x: i32, y: i32) -> f64:
155+
"""
156+
Returns the hypotenuse of the right triangle with sides `x` and `y`.
157+
"""
158+
return sqrt(1.0*(x**2 + y**2))
159+
160+
154161
def sqrt(x: f64) -> f64:
155162
return x**(1/2)
156163

0 commit comments

Comments
 (0)