Skip to content

Commit e28b358

Browse files
committed
[GR-10583] Math.hypot function is not implemented.
1 parent aec2b0b commit e28b358

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_math.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
NAN = float('nan')
1414
LONG_INT = 6227020800
1515
BIG_INT = 9999992432902008176640000999999
16+
FLOAT_MAX = sys.float_info.max
1617

1718
""" The next three methods are needed for testing factorials
1819
"""
@@ -888,6 +889,28 @@ def testAtanh(self):
888889
self.assertRaises(TypeError, math.atanh, 'ahoj')
889890
self.assertRaises(ValueError, math.atanh, BIG_INT)
890891

892+
def testHypot(self):
893+
self.assertRaises(TypeError, math.hypot)
894+
self.ftest('hypot(0,0)', math.hypot(0,0), 0)
895+
self.ftest('hypot(3,4)', math.hypot(3,4), 5)
896+
self.assertEqual(math.hypot(NAN, INF), INF)
897+
self.assertEqual(math.hypot(INF, NAN), INF)
898+
self.assertEqual(math.hypot(NAN, NINF), INF)
899+
self.assertEqual(math.hypot(NINF, NAN), INF)
900+
self.assertRaises(OverflowError, math.hypot, FLOAT_MAX, FLOAT_MAX)
901+
self.assertTrue(math.isnan(math.hypot(1.0, NAN)))
902+
self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
903+
904+
self.assertEqual(math.hypot(NINF, 1), INF)
905+
self.assertEqual(math.hypot(INF, 1), INF)
906+
self.assertEqual(math.hypot(1, INF), INF)
907+
self.assertEqual(math.hypot(1, NINF), INF)
908+
909+
self.ftest('math.hypot(MyFloat(), MyFloat())', math.hypot(MyFloat(), MyFloat()), 0.848528137423857)
910+
self.ftest('math.hypot(BIG_INT, BIG_INT)', math.hypot(BIG_INT, BIG_INT), 1.4142124922238343e+31)
911+
self.assertRaises(TypeError, math.hypot, 'ahoj', 1)
912+
self.assertRaises(TypeError, math.hypot, 1, 'cau')
913+
891914
def test_fabs(self):
892915
self.assertEqual(math.fabs(-1), 1)
893916
self.assertEqual(math.fabs(0), 0)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,4 +1713,69 @@ public double count(double value) {
17131713
return value * DEG_TO_RAD;
17141714
}
17151715
}
1716+
1717+
@Builtin(name = "hypot", fixedNumOfArguments = 2)
1718+
@TypeSystemReference(PythonArithmeticTypes.class)
1719+
@GenerateNodeFactory
1720+
@ImportStatic(MathGuards.class)
1721+
public abstract static class HypotNode extends PythonBinaryBuiltinNode {
1722+
1723+
@Specialization
1724+
@TruffleBoundary
1725+
public double hypotDD(double x, double y) {
1726+
double result = Math.hypot(x, y);
1727+
if (Double.isInfinite(result) && Double.isFinite(x) && Double.isFinite(y)) {
1728+
throw raise(OverflowError, "math range error");
1729+
}
1730+
return result;
1731+
}
1732+
1733+
@Specialization
1734+
public double hypotDL(double x, long y) {
1735+
return hypotDD(x, y);
1736+
}
1737+
1738+
@Specialization
1739+
public double hypotLD(long x, double y) {
1740+
return hypotDD(x, y);
1741+
}
1742+
1743+
@Specialization
1744+
public double hypotLL(long x, long y) {
1745+
return hypotDD(x, y);
1746+
}
1747+
1748+
@Specialization
1749+
public double hypotDPI(double x, PInt y) {
1750+
return hypotDD(x, y.doubleValue());
1751+
}
1752+
1753+
@Specialization
1754+
public double hypotLPI(long x, PInt y) {
1755+
return hypotDD(x, y.doubleValue());
1756+
}
1757+
1758+
@Specialization
1759+
public double hypotPIPI(PInt x, PInt y) {
1760+
return hypotDD(x.doubleValue(), y.doubleValue());
1761+
}
1762+
1763+
@Specialization
1764+
public double hypotPID(PInt x, double y) {
1765+
return hypotDD(x.doubleValue(), y);
1766+
}
1767+
1768+
@Specialization
1769+
public double hypotPIL(PInt x, long y) {
1770+
return hypotDD(x.doubleValue(), y);
1771+
}
1772+
1773+
@Specialization(guards = "!isNumber(objectX) || !isNumber(objectY)")
1774+
public double hypotOO(Object objectX, Object objectY,
1775+
@Cached("create()") ConvertToFloatNode xConvertNode,
1776+
@Cached("create()") ConvertToFloatNode yConvertNode) {
1777+
return hypotDD(xConvertNode.execute(objectX), yConvertNode.execute(objectY));
1778+
}
1779+
}
1780+
17161781
}

0 commit comments

Comments
 (0)