Skip to content

Commit ef9f41c

Browse files
committed
[GR-10511] Implement all power and logarithim functions from Math.module.
PullRequest: graalpython/92
2 parents f467da7 + 63b004f commit ef9f41c

File tree

2 files changed

+421
-592
lines changed

2 files changed

+421
-592
lines changed

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

Lines changed: 110 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))
@@ -384,6 +444,19 @@ def __ceil__(self):
384444
self.assertEqual(math.ceil(O()), 'cau')
385445
self.assertEqual(math.ceil(999.1), 1000)
386446

447+
self.assertEqual(math.ceil(MyFloat()),1)
448+
449+
class F1():
450+
def __float__(self):
451+
return 1.1
452+
def __ceil__(self):
453+
return 44
454+
self.assertEqual(math.ceil(F1()), 44)
455+
456+
class F2():
457+
def __float__(self):
458+
return 1.1
459+
self.assertEqual(math.ceil(F2()), 2)
387460

388461
def test_basic_copysign(self):
389462
self.assertEqual(math.copysign(3, -0), 3.0)
@@ -952,6 +1025,36 @@ def test_fmod(self):
9521025
self.assertEqual(math.fmod(2432902008176640000999, 12.12), 10.396369527944033)
9531026
self.assertEqual(math.fmod(-1e-100, 1e100), -1e-100)
9541027

1028+
def testExp(self):
1029+
self.assertRaises(TypeError, math.exp)
1030+
self.ftest('exp(-1)', math.exp(-1), 1/math.e)
1031+
self.ftest('exp(0)', math.exp(0), 1)
1032+
self.ftest('exp(1)', math.exp(1), math.e)
1033+
self.assertEqual(math.exp(INF), INF)
1034+
self.assertEqual(math.exp(NINF), 0.)
1035+
self.assertTrue(math.isnan(math.exp(NAN)))
1036+
self.assertRaises(OverflowError, math.exp, 1000000)
1037+
1038+
# test of specializations
1039+
self.ftest('exp(MyFloat())', math.exp(MyFloat()), 1.8221188003905089)
1040+
self.assertRaises(TypeError, math.exp, 'ahoj')
1041+
self.assertRaises(OverflowError, math.exp, BIG_INT)
1042+
1043+
def testExpm1(self):
1044+
self.assertRaises(TypeError, math.exp)
1045+
self.ftest('expm1(-1)', math.expm1(-1), 1/math.e-1)
1046+
self.ftest('expm1(0)', math.expm1(0), 0)
1047+
self.ftest('expm1(1)', math.expm1(1), math.e-1)
1048+
self.assertEqual(math.expm1(INF), INF)
1049+
self.assertEqual(math.expm1(NINF), -1.)
1050+
self.assertTrue(math.isnan(math.expm1(NAN)))
1051+
self.assertRaises(OverflowError, math.expm1, 1000000)
1052+
1053+
# test of specializations
1054+
self.ftest('expm1(MyFloat())', math.expm1(MyFloat()), 0.8221188003905089)
1055+
self.assertRaises(TypeError, math.expm1, 'ahoj')
1056+
self.assertRaises(OverflowError, math.expm1, BIG_INT)
1057+
9551058
def test_frexp(self):
9561059
self.assertRaises(TypeError, math.frexp)
9571060

@@ -970,6 +1073,11 @@ def testfrexp(name, result, expected):
9701073
self.assertEqual(math.frexp(NINF)[0], NINF)
9711074
self.assertTrue(math.isnan(math.frexp(NAN)[0]))
9721075

1076+
# test of specializations
1077+
testfrexp('frexp(MyFloat())', math.frexp(MyFloat()), (0.6, 0))
1078+
self.assertRaises(TypeError, math.log1p, 'ahoj')
1079+
testfrexp('log1p(BIG_INT)', math.frexp(BIG_INT), (0.9860753853527933, 103))
1080+
9731081
testfrexp('frexp(True)', math.frexp(True), (0.5, 1))
9741082
testfrexp('frexp(False)', math.frexp(False), (0.0, 0))
9751083
testfrexp('frexp(6227020800)', math.frexp(6227020800), (0.7249206304550171, 33))
@@ -986,6 +1094,8 @@ def getY():
9861094

9871095
testfrexp('frexp(X(10))', math.frexp(X(10)), (0.625, 4))
9881096
testfrexp('frexp(Y(11.11))', math.frexp(Y(11.11)), (0.694375, 4))
1097+
testfrexp('frexp(2**1023)', math.frexp(2**1023), (0.5, 1024))
1098+
self.assertRaises(OverflowError, math.frexp, 2**1024)
9891099

9901100
def test_ldexp(self):
9911101
self.assertRaises(TypeError, math.ldexp)

0 commit comments

Comments
 (0)