Skip to content

Commit a28b6bc

Browse files
committed
Fixed handling of nan and inf in float.pow()
1 parent f8758a5 commit a28b6bc

File tree

1 file changed

+4
-4
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats

1 file changed

+4
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats/FloatBuiltins.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,9 @@ private double doSpecialCases(double left, double right, BranchProfile negativeR
458458
// v**(+/-)inf is 1.0 if abs(v) == 1, unlike on Java
459459
return 1;
460460
}
461-
if (left == 0 && right < 0) {
461+
if (left == 0 && right < 0 && Double.isFinite(right)) {
462462
negativeRaise.enter();
463-
// 0**w is an error if w is negative, unlike Java
463+
// 0**w is an error if w is finite and negative, unlike Java
464464
throw raise(PythonBuiltinClassType.ZeroDivisionError, ErrorMessages.POW_ZERO_CANNOT_RAISE_TO_NEGATIVE_POWER);
465465
}
466466
return 0;
@@ -480,7 +480,7 @@ private double doOperation(double left, double right, BranchProfile negativeRais
480480
if (doSpecialCases(left, right, negativeRaise) == 1) {
481481
return 1.0;
482482
}
483-
if (left < 0 && (right % 1 != 0)) {
483+
if (left < 0 && Double.isFinite(left) && Double.isFinite(right) && (right % 1 != 0)) {
484484
CompilerDirectives.transferToInterpreterAndInvalidate();
485485
// Negative numbers raised to fractional powers become complex.
486486
throw new UnexpectedResultException(callPow.execute(frame, factory().createComplex(left, 0), factory().createComplex(right, 0), none));
@@ -495,7 +495,7 @@ Object doDDToComplex(VirtualFrame frame, double left, double right, PNone none,
495495
if (doSpecialCases(left, right, negativeRaise) == 1) {
496496
return 1.0;
497497
}
498-
if (left < 0 && (right % 1 != 0)) {
498+
if (left < 0 && Double.isFinite(left) && Double.isFinite(right) && (right % 1 != 0)) {
499499
// Negative numbers raised to fractional powers become complex.
500500
return callPow.execute(frame, factory().createComplex(left, 0), factory().createComplex(right, 0), none);
501501
}

0 commit comments

Comments
 (0)