Skip to content

Commit e9dc207

Browse files
committed
[GR-10511] Implement all power and logarithim functions from Math.module.
1 parent 5cf1716 commit e9dc207

File tree

2 files changed

+377
-12
lines changed

2 files changed

+377
-12
lines changed

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,66 @@ def testLog(self):
290290
math.log(10, MyFloat())
291291
self.assertRaises(ValueError, math.log, 0)
292292

293+
def testLog1p(self):
294+
self.assertRaises(TypeError, math.log1p)
295+
for n in [2, 2**90, 2**300]:
296+
self.assertAlmostEqual(math.log1p(n), math.log1p(float(n)))
297+
self.assertRaises(ValueError, math.log1p, -1)
298+
self.assertEqual(math.log1p(INF), INF)
299+
300+
# test of specializations
301+
self.ftest('log1p(MyFloat())', math.log1p(MyFloat()), 0.4700036292457356)
302+
self.assertRaises(TypeError, math.log1p, 'ahoj')
303+
self.ftest('log1p(BIG_INT)', math.log1p(BIG_INT), 71.38013712610532)
304+
305+
#@requires_IEEE_754
306+
def testLog2(self):
307+
self.assertRaises(TypeError, math.log2)
308+
309+
# Check some integer values
310+
self.assertEqual(math.log2(1), 0.0)
311+
self.assertEqual(math.log2(2), 1.0)
312+
self.assertEqual(math.log2(4), 2.0)
313+
314+
# Large integer values
315+
self.assertEqual(math.log2(2**1023), 1023.0)
316+
self.assertEqual(math.log2(2**1024), 1024.0)
317+
self.assertEqual(math.log2(2**2000), 2000.0)
318+
319+
self.assertRaises(ValueError, math.log2, -1.5)
320+
self.assertRaises(ValueError, math.log2, NINF)
321+
self.assertTrue(math.isnan(math.log2(NAN)))
322+
323+
# test of specializations
324+
self.ftest('log2(MyFloat())', math.log2(MyFloat()), -0.7369655941662062)
325+
self.assertRaises(TypeError, math.log2, 'ahoj')
326+
self.ftest('log2(BIG_INT)', math.log2(BIG_INT), 102.97976984980635)
327+
328+
def testLog2Exact(self):
329+
# Check that we get exact equality for log2 of powers of 2.
330+
actual = [math.log2(math.ldexp(1.0, n)) for n in range(-1074, 1024)]
331+
expected = [float(n) for n in range(-1074, 1024)]
332+
self.assertEqual(actual, expected)
333+
334+
def testLog10(self):
335+
self.assertRaises(TypeError, math.log10)
336+
self.ftest('log10(0.1)', math.log10(0.1), -1)
337+
self.ftest('log10(1)', math.log10(1), 0)
338+
self.ftest('log10(10)', math.log10(10), 1)
339+
self.ftest('log10(10**1000)', math.log10(10**1000), 1000.0)
340+
self.assertRaises(ValueError, math.log10, -1.5)
341+
self.assertRaises(ValueError, math.log10, -10**1000)
342+
self.assertRaises(ValueError, math.log10, NINF)
343+
self.assertEqual(math.log(INF), INF)
344+
self.assertTrue(math.isnan(math.log10(NAN)))
345+
346+
# test of specializations
347+
# TODO uncomment when GR-10346 will be fixed
348+
#self.ftest('log10(MyFloat())', math.log10(MyFloat()), -0.22184874961635637)
349+
self.assertRaises(TypeError, math.log10, 'ahoj')
350+
# TODO uncomment when GR-10346 will be fixed
351+
#self.ftest('log10(BIG_INT)', math.log10(BIG_INT), 30.999999671364986)
352+
293353
def testIsfinite(self):
294354
self.assertTrue(math.isfinite(0.0))
295355
self.assertTrue(math.isfinite(-0.0))
@@ -952,6 +1012,36 @@ def test_fmod(self):
9521012
self.assertEqual(math.fmod(2432902008176640000999, 12.12), 10.396369527944033)
9531013
self.assertEqual(math.fmod(-1e-100, 1e100), -1e-100)
9541014

1015+
def testExp(self):
1016+
self.assertRaises(TypeError, math.exp)
1017+
self.ftest('exp(-1)', math.exp(-1), 1/math.e)
1018+
self.ftest('exp(0)', math.exp(0), 1)
1019+
self.ftest('exp(1)', math.exp(1), math.e)
1020+
self.assertEqual(math.exp(INF), INF)
1021+
self.assertEqual(math.exp(NINF), 0.)
1022+
self.assertTrue(math.isnan(math.exp(NAN)))
1023+
self.assertRaises(OverflowError, math.exp, 1000000)
1024+
1025+
# test of specializations
1026+
self.ftest('exp(MyFloat())', math.exp(MyFloat()), 1.8221188003905089)
1027+
self.assertRaises(TypeError, math.exp, 'ahoj')
1028+
self.assertRaises(OverflowError, math.exp, BIG_INT)
1029+
1030+
def testExpm1(self):
1031+
self.assertRaises(TypeError, math.exp)
1032+
self.ftest('expm1(-1)', math.expm1(-1), 1/math.e-1)
1033+
self.ftest('expm1(0)', math.expm1(0), 0)
1034+
self.ftest('expm1(1)', math.expm1(1), math.e-1)
1035+
self.assertEqual(math.expm1(INF), INF)
1036+
self.assertEqual(math.expm1(NINF), -1.)
1037+
self.assertTrue(math.isnan(math.expm1(NAN)))
1038+
self.assertRaises(OverflowError, math.expm1, 1000000)
1039+
1040+
# test of specializations
1041+
self.ftest('expm1(MyFloat())', math.expm1(MyFloat()), 0.8221188003905089)
1042+
self.assertRaises(TypeError, math.expm1, 'ahoj')
1043+
self.assertRaises(OverflowError, math.expm1, BIG_INT)
1044+
9551045
def test_frexp(self):
9561046
self.assertRaises(TypeError, math.frexp)
9571047

@@ -970,6 +1060,11 @@ def testfrexp(name, result, expected):
9701060
self.assertEqual(math.frexp(NINF)[0], NINF)
9711061
self.assertTrue(math.isnan(math.frexp(NAN)[0]))
9721062

1063+
# test of specializations
1064+
testfrexp('frexp(MyFloat())', math.frexp(MyFloat()), (0.6, 0))
1065+
self.assertRaises(TypeError, math.log1p, 'ahoj')
1066+
testfrexp('log1p(BIG_INT)', math.frexp(BIG_INT), (0.9860753853527933, 103))
1067+
9731068
testfrexp('frexp(True)', math.frexp(True), (0.5, 1))
9741069
testfrexp('frexp(False)', math.frexp(False), (0.0, 0))
9751070
testfrexp('frexp(6227020800)', math.frexp(6227020800), (0.7249206304550171, 33))
@@ -986,6 +1081,8 @@ def getY():
9861081

9871082
testfrexp('frexp(X(10))', math.frexp(X(10)), (0.625, 4))
9881083
testfrexp('frexp(Y(11.11))', math.frexp(Y(11.11)), (0.694375, 4))
1084+
testfrexp('frexp(2**1023)', math.frexp(2**1023), (0.5, 1024))
1085+
self.assertRaises(OverflowError, math.frexp, 2**1024)
9891086

9901087
def test_ldexp(self):
9911088
self.assertRaises(TypeError, math.ldexp)

0 commit comments

Comments
 (0)