Skip to content

Commit 64ba570

Browse files
committed
Fix dealing with negative zero in complex constructor
1 parent feb427c commit 64ba570

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,9 @@ PComplex complexFromLong(LazyPythonClass cls, PInt real, @SuppressWarnings("unus
406406
@Specialization(guards = {"isNoValue(imag)", "!isNoValue(number)", "!isString(number)"})
407407
PComplex complexFromObject(VirtualFrame frame, LazyPythonClass cls, Object number, @SuppressWarnings("unused") PNone imag) {
408408
PComplex value = getComplexNumberFromObject(frame, number);
409+
if (value == null) {
410+
return createComplex(cls, getFirstArgCoerceToDouble().execute(frame, number), 0.0);
411+
}
409412
return createComplex(cls, value);
410413
}
411414

@@ -427,31 +430,46 @@ PComplex complexFromDoubleComplex(LazyPythonClass cls, double one, PComplex two)
427430
@Specialization(guards = "!isString(one)")
428431
PComplex complexFromComplexLong(VirtualFrame frame, LazyPythonClass cls, Object one, long two) {
429432
PComplex value = getComplexNumberFromObject(frame, one);
433+
if (value == null) {
434+
return createComplex(cls, getFirstArgCoerceToDouble().execute(frame, one), two);
435+
}
430436
return createComplex(cls, value.getReal(), value.getImag() + two);
431437
}
432438

433439
@Specialization(guards = "!isString(one)")
434440
PComplex complexFromComplexDouble(VirtualFrame frame, LazyPythonClass cls, Object one, double two) {
435441
PComplex value = getComplexNumberFromObject(frame, one);
442+
if (value == null) {
443+
return createComplex(cls, getFirstArgCoerceToDouble().execute(frame, one), two);
444+
}
436445
return createComplex(cls, value.getReal(), value.getImag() + two);
437446
}
438447

439448
@Specialization(guards = "!isString(one)")
440449
PComplex complexFromComplexPInt(VirtualFrame frame, LazyPythonClass cls, Object one, PInt two) {
441450
PComplex value = getComplexNumberFromObject(frame, one);
451+
if (value == null) {
452+
return createComplex(cls, getFirstArgCoerceToDouble().execute(frame, one), two.doubleValue());
453+
}
442454
return createComplex(cls, value.getReal(), value.getImag() + two.doubleValue());
443455
}
444456

445457
@Specialization(guards = "!isString(one)")
446458
PComplex complexFromComplexComplex(VirtualFrame frame, LazyPythonClass cls, Object one, PComplex two) {
447459
PComplex value = getComplexNumberFromObject(frame, one);
460+
if (value == null) {
461+
return createComplex(cls, getFirstArgCoerceToDouble().execute(frame, one) - two.getImag(), two.getReal());
462+
}
448463
return createComplex(cls, value.getReal() - two.getImag(), value.getImag() + two.getReal());
449464
}
450465

451466
@Specialization(guards = {"!isString(one)", "!isNoValue(two)", "!isPComplex(two)"})
452467
PComplex complexFromComplexObject(VirtualFrame frame, LazyPythonClass cls, Object one, Object two) {
453468
PComplex oneValue = getComplexNumberFromObject(frame, one);
454469
double twoValue = getSecondArgCoerceToDouble().execute(frame, two);
470+
if (oneValue == null) {
471+
return createComplex(cls, getFirstArgCoerceToDouble().execute(frame, one), twoValue);
472+
}
455473
return createComplex(cls, oneValue.getReal(), oneValue.getImag() + twoValue);
456474
}
457475

@@ -528,7 +546,7 @@ private PComplex getComplexNumberFromObject(VirtualFrame frame, Object object) {
528546
// the class extending PComplex but doesn't have __complex__ method
529547
return (PComplex) object;
530548
}
531-
return factory().createComplex(getFirstArgCoerceToDouble().execute(frame, object), 0.0);
549+
return null;
532550
}
533551
}
534552

@@ -1347,7 +1365,7 @@ Object createInt(LazyPythonClass cls, long arg, @SuppressWarnings("unused") PNon
13471365
}
13481366

13491367
@Specialization(guards = "isNoValue(base)")
1350-
Object createInt(VirtualFrame frame, LazyPythonClass cls, double arg, @SuppressWarnings("unused") PNone base,
1368+
Object createInt(LazyPythonClass cls, double arg, @SuppressWarnings("unused") PNone base,
13511369
@Cached("createFloatInt()") FloatBuiltins.IntNode floatToIntNode) {
13521370
Object result = floatToIntNode.executeWithDouble(arg);
13531371
return createInt(cls, result);

0 commit comments

Comments
 (0)