Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1778,3 +1778,48 @@ def test_objects(self):
self.assertIsClose(MyFloat(), MyFloat(), rel_tol=MyFloat())

self.assertIsNotClose(MyFloat(), 10)


class MathLogTests(unittest.TestCase):

def test_log_base_none(self):
class PositiveFloat:
def __float__(self):
return 10.0

self.assertRaises(TypeError, math.log, 10, None)
self.assertRaises(TypeError, math.log, 10.0, None)
self.assertRaises(TypeError, math.log, 2**70, None)
self.assertRaises(TypeError, math.log, PositiveFloat(), None)
self.assertRaises(ValueError, math.log, -1, None)
self.assertEqual(math.log(1), 0.0)
self.assertEqual(math.log(1.0), 0.0)
self.assertTrue(48.52 < math.log(2**70) < 48.53)

def test_log_value_checked_before_base(self):
class NegativeFloat:
def __float__(self):
return -1.0

self.assertRaises(ValueError, math.log, -1, object())
self.assertRaises(ValueError, math.log, 0, object())
self.assertRaises(ValueError, math.log, -1.0, object())
self.assertRaises(ValueError, math.log, -2**70, object())
self.assertRaises(ValueError, math.log, NegativeFloat(), object())
self.assertRaises(TypeError, math.log, float('nan'), object())

def test_log_zero_with_base(self):
class IntSubclass(int):
pass

self.assertRaises(ValueError, math.log, 0, 2)
self.assertRaises(ValueError, math.log, 0.0, 2.0)
self.assertRaises(ValueError, math.log, -0.0, 2)
self.assertRaises(ValueError, math.log, False, 2)
self.assertRaises(ValueError, math.log, 0, 1)
self.assertRaises(ValueError, math.log, 0, IntSubclass(2))
self.assertRaises(ValueError, math.log, 0.0, IntSubclass(2))
self.assertRaises(ValueError, math.log, IntSubclass(0))
self.assertRaises(ValueError, math.log, IntSubclass(0), 2.0)
self.assertRaises(ValueError, math.log, IntSubclass(0), 2)
self.assertRaises(ValueError, math.log, IntSubclass(0), IntSubclass(2))
Original file line number Diff line number Diff line change
Expand Up @@ -1675,15 +1675,15 @@ private static double countBase(BigInteger base, Node inliningTarget, InlinedCon
return logBase;
}

@Specialization
@Specialization(guards = "isNoValue(novalue)")
static double log(long value, PNone novalue,
@Bind Node inliningTarget,
@Shared @Cached InlinedConditionProfile doNotFit,
@Shared @Cached PRaiseNode raiseNode) {
return logDN(value, novalue, inliningTarget, doNotFit, raiseNode);
}

@Specialization
@Specialization(guards = "isNoValue(novalue)")
static double logDN(double value, @SuppressWarnings("unused") PNone novalue,
@Bind Node inliningTarget,
@Shared @Cached InlinedConditionProfile doNotFit,
Expand All @@ -1692,14 +1692,14 @@ static double logDN(double value, @SuppressWarnings("unused") PNone novalue,
return Math.log(value);
}

@Specialization
@Specialization(guards = "isNoValue(novalue)")
@TruffleBoundary
static double logPIN(PInt value, @SuppressWarnings("unused") PNone novalue,
@Bind Node inliningTarget,
@Shared @Cached InlinedConditionProfile doNotFit,
@Shared @Cached PRaiseNode raiseNode) {
BigInteger bValue = value.getValue();
raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) < 0, raiseNode);
raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) <= 0, raiseNode);
return logBigInteger(bValue);
}

Expand Down Expand Up @@ -1736,7 +1736,7 @@ static double logDD(double value, double base,
@Shared @Cached InlinedConditionProfile doNotFit,
@Shared @Cached InlinedConditionProfile divByZero,
@Shared @Cached PRaiseNode raiseNode) {
raiseMathError(inliningTarget, doNotFit, value < 0 || base <= 0, raiseNode);
raiseMathError(inliningTarget, doNotFit, value <= 0 || base <= 0, raiseNode);
double logBase = countBase(base, inliningTarget, divByZero, raiseNode);
return Math.log(value) / logBase;
}
Expand All @@ -1749,7 +1749,7 @@ static double logDPI(double value, PInt base,
@Shared @Cached InlinedConditionProfile divByZero,
@Shared @Cached PRaiseNode raiseNode) {
BigInteger bBase = base.getValue();
raiseMathError(inliningTarget, doNotFit, value < 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode);
raiseMathError(inliningTarget, doNotFit, value <= 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode);
double logBase = countBase(bBase, inliningTarget, divByZero, raiseNode);
return Math.log(value) / logBase;
}
Expand All @@ -1771,7 +1771,7 @@ static double logPID(PInt value, double base,
@Shared @Cached InlinedConditionProfile divByZero,
@Shared @Cached PRaiseNode raiseNode) {
BigInteger bValue = value.getValue();
raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) < 0 || base <= 0, raiseNode);
raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) <= 0 || base <= 0, raiseNode);
double logBase = countBase(base, inliningTarget, divByZero, raiseNode);
return logBigInteger(bValue) / logBase;
}
Expand All @@ -1784,7 +1784,7 @@ static double logLPI(long value, PInt base,
@Shared @Cached InlinedConditionProfile divByZero,
@Shared @Cached PRaiseNode raiseNode) {
BigInteger bBase = base.getValue();
raiseMathError(inliningTarget, doNotFit, value < 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode);
raiseMathError(inliningTarget, doNotFit, value <= 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode);
double logBase = countBase(bBase, inliningTarget, divByZero, raiseNode);
return Math.log(value) / logBase;
}
Expand All @@ -1798,12 +1798,12 @@ static double logPIPI(PInt value, PInt base,
@Shared @Cached PRaiseNode raiseNode) {
BigInteger bValue = value.getValue();
BigInteger bBase = base.getValue();
raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) < 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode);
raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) <= 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode);
double logBase = countBase(bBase, inliningTarget, divByZero, raiseNode);
return logBigInteger(bValue) / logBase;
}

@Specialization(guards = "!isNumber(value)")
@Specialization(guards = {"!isNumber(value)", "isNoValue(novalue)"})
double logO(VirtualFrame frame, Object value, PNone novalue,
@Bind Node inliningTarget,
@Shared @Cached PyFloatAsDoubleNode asDoubleNode) {
Expand All @@ -1813,28 +1813,41 @@ static double logPIPI(PInt value, PInt base,
@Specialization(guards = {"!isNumber(value)", "!isNoValue(base)"})
double logOO(VirtualFrame frame, Object value, Object base,
@Bind Node inliningTarget,
@Shared @Cached PyFloatAsDoubleNode asDoubleNode) {
return executeRecursiveLogNode(frame, asDoubleNode.execute(frame, inliningTarget, value), asDoubleNode.execute(frame, inliningTarget, base));
@Shared @Cached PyFloatAsDoubleNode asDoubleNode,
@Shared @Cached InlinedConditionProfile doNotFit,
@Shared @Cached PRaiseNode raiseNode) {
double x = asDoubleNode.execute(frame, inliningTarget, value);
raiseMathError(inliningTarget, doNotFit, x <= 0, raiseNode);
return executeRecursiveLogNode(frame, x, asDoubleNode.execute(frame, inliningTarget, base));
}

@Specialization(guards = {"!isNumber(base)"})
@Specialization(guards = {"!isNumber(base)", "!isNoValue(base)"})
double logLO(VirtualFrame frame, long value, Object base,
@Bind Node inliningTarget,
@Shared @Cached PyFloatAsDoubleNode asDoubleNode) {
@Shared @Cached PyFloatAsDoubleNode asDoubleNode,
@Shared @Cached InlinedConditionProfile doNotFit,
@Shared @Cached PRaiseNode raiseNode) {
raiseMathError(inliningTarget, doNotFit, value <= 0, raiseNode);
return executeRecursiveLogNode(frame, value, asDoubleNode.execute(frame, inliningTarget, base));
}

@Specialization(guards = {"!isNumber(base)"})
@Specialization(guards = {"!isNumber(base)", "!isNoValue(base)"})
double logDO(VirtualFrame frame, double value, Object base,
@Bind Node inliningTarget,
@Shared @Cached PyFloatAsDoubleNode asDoubleNode) {
@Shared @Cached PyFloatAsDoubleNode asDoubleNode,
@Shared @Cached InlinedConditionProfile doNotFit,
@Shared @Cached PRaiseNode raiseNode) {
raiseMathError(inliningTarget, doNotFit, value <= 0, raiseNode);
return executeRecursiveLogNode(frame, value, asDoubleNode.execute(frame, inliningTarget, base));
}

@Specialization(guards = {"!isNumber(base)"})
@Specialization(guards = {"!isNumber(base)", "!isNoValue(base)"})
double logPIO(VirtualFrame frame, PInt value, Object base,
@Bind Node inliningTarget,
@Shared @Cached PyFloatAsDoubleNode asDoubleNode) {
@Shared @Cached PyFloatAsDoubleNode asDoubleNode,
@Shared @Cached InlinedConditionProfile doNotFit,
@Shared @Cached PRaiseNode raiseNode) {
raiseMathError(inliningTarget, doNotFit, value.isZeroOrNegative(), raiseNode);
return executeRecursiveLogNode(frame, value, asDoubleNode.execute(frame, inliningTarget, base));
}

Expand Down