Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def test_is_integer(self):
# for doubles this big, all representable values are integers...
assert (2**52 + 0.5).is_integer()

def test_pow_overflow(self):
self.assertRaises(OverflowError, pow, 10.0, 400)
self.assertRaises(OverflowError, pow, 10.0, 400.0)
self.assertRaises(OverflowError, pow, -1e308, 3.0)
self.assertEqual(pow(INF, 2.0), INF)
self.assertEqual(pow(2.0, INF), INF)

def test_rounding(self):
assert round(1.123, 0) == 1
assert round(1.123, 1) == 1.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
* Copyright (c) 2017, 2026, Oracle and/or its affiliates.
* Copyright (c) 2014, Regents of the University of California
*
* All rights reserved.
Expand Down Expand Up @@ -504,7 +504,15 @@ private static double doOperation(Node inliningTarget, double left, double right
if (doSpecialCases(inliningTarget, left, right, raiseNode) == 1) {
return 1.0;
}
return Math.pow(left, right);
return doPow(inliningTarget, left, right, raiseNode);
}

private static double doPow(Node inliningTarget, double left, double right, PRaiseNode raiseNode) {
double result = Math.pow(left, right);
if (Double.isInfinite(result) && Double.isFinite(left) && Double.isFinite(right)) {
throw raiseNode.raise(inliningTarget, OverflowError, ErrorMessages.NUMERICAL_RESULT_OUT_OF_RANGE);
}
return result;
}

@Specialization(rewriteOn = UnexpectedResultException.class)
Expand All @@ -522,7 +530,7 @@ static double doDD(VirtualFrame frame, double left, double right, @SuppressWarni
PythonLanguage language = PythonLanguage.get(inliningTarget);
throw new UnexpectedResultException(powerNode.execute(frame, PFactory.createComplex(language, left, 0), PFactory.createComplex(language, right, 0), none));
}
return Math.pow(left, right);
return doPow(inliningTarget, left, right, raiseNode);
}

@Specialization(replaces = "doDD")
Expand All @@ -539,7 +547,7 @@ static Object doDDToComplex(VirtualFrame frame, double left, double right, PNone
PythonLanguage language = PythonLanguage.get(inliningTarget);
return powerNode.execute(frame, PFactory.createComplex(language, left, 0), PFactory.createComplex(language, right, 0), none);
}
return Math.pow(left, right);
return doPow(inliningTarget, left, right, raiseNode);
}

@Specialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ public abstract class ErrorMessages {
public static final TruffleString INPUTS_ARE_NOT_THE_SAME_LENGTH = tsLiteral("Inputs are not the same length");
public static final TruffleString MATH_DOMAIN_ERROR = tsLiteral("math domain error");
public static final TruffleString MATH_RANGE_ERROR = tsLiteral("math range error");
public static final TruffleString NUMERICAL_RESULT_OUT_OF_RANGE = tsLiteral("Numerical result out of range");
public static final TruffleString MAX_MARSHAL_STACK_DEPTH = tsLiteral("Maximum marshal stack depth");
public static final TruffleString M = tsLiteral("%m");
public static final TruffleString MEMORYVIEW_INVALID_SLICE_KEY = tsLiteral("memoryview: invalid slice key");
Expand Down
Loading