Skip to content

Commit 0190f59

Browse files
committed
[GR-20824] Comparing zero complex number with zero doesn't work as expected.
[GR-20829] The algorithm for computing div of complex numbers should be changed.
1 parent 07c2fce commit 0190f59

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_complex.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
*ComplexTest.test_overflow
1010
*ComplexTest.test_plus_minus_0j
1111
*ComplexTest.test_repr_str
12+
*ComplexTest.test_truediv

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/complex/ComplexBuiltins.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@
8080
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
8181
import com.oracle.graal.python.runtime.exception.PythonErrorType;
8282
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
83+
import com.oracle.truffle.api.dsl.Cached;
8384
import com.oracle.truffle.api.dsl.Fallback;
8485
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
8586
import com.oracle.truffle.api.dsl.NodeFactory;
8687
import com.oracle.truffle.api.dsl.Specialization;
8788
import com.oracle.truffle.api.dsl.TypeSystemReference;
89+
import com.oracle.truffle.api.profiles.ConditionProfile;
8890

8991
@CoreFunctions(extendClasses = PythonBuiltinClassType.PComplex)
9092
public class ComplexBuiltins extends PythonBuiltins {
@@ -289,11 +291,31 @@ PComplex doComplexPInt(PComplex right, PInt left) {
289291
}
290292

291293
@Specialization
292-
PComplex doComplex(PComplex left, PComplex right) {
293-
double opNormSq = right.getReal() * right.getReal() + right.getImag() * right.getImag();
294-
double realPart = left.getReal() * right.getReal() - left.getImag() * -right.getImag();
295-
double imagPart = left.getReal() * -right.getImag() + left.getImag() * right.getReal();
296-
return factory().createComplex(realPart / opNormSq, imagPart / opNormSq);
294+
PComplex doComplex(PComplex left, PComplex right,
295+
@Cached("createBinaryProfile()") ConditionProfile topConditionProfile,
296+
@Cached("createBinaryProfile()") ConditionProfile zeroDivisionProfile) {
297+
double absRightReal = right.getReal() < 0 ? -right.getReal() : right.getReal();
298+
double absRightImag = right.getImag() < 0 ? -right.getImag() : right.getImag();
299+
double real;
300+
double imag;
301+
if (topConditionProfile.profile(absRightReal >= absRightImag)) {
302+
/* divide tops and bottom by right.real */
303+
if (zeroDivisionProfile.profile(absRightReal == 0.0)) {
304+
throw raise(PythonErrorType.ZeroDivisionError, "complex division by zero");
305+
} else {
306+
double ratio = right.getImag() / right.getReal();
307+
double denom = right.getReal() + right.getImag() * ratio;
308+
real = (left.getReal() + left.getImag() * ratio) / denom;
309+
imag = (left.getImag() - left.getReal() * ratio) / denom;
310+
}
311+
} else {
312+
/* divide tops and bottom by right.imag */
313+
double ratio = right.getReal() / right.getImag();
314+
double denom = right.getReal() * ratio + right.getImag();
315+
real = (left.getReal() * ratio + left.getImag()) / denom;
316+
imag = (left.getImag() * ratio - left.getReal()) / denom;
317+
}
318+
return factory().createComplex(real, imag);
297319
}
298320

299321
@SuppressWarnings("unused")
@@ -533,13 +555,19 @@ PNotImplemented doGeneric(Object left, Object right) {
533555
}
534556

535557
@GenerateNodeFactory
558+
@TypeSystemReference(PythonArithmeticTypes.class)
536559
@Builtin(name = __NE__, minNumOfPositionalArgs = 2)
537560
abstract static class NeNode extends PythonBinaryBuiltinNode {
538561
@Specialization
539562
boolean doComplex(PComplex left, PComplex right) {
540563
return left.notEqual(right);
541564
}
542565

566+
@Specialization
567+
boolean doComplex(PComplex left, long right) {
568+
return left.getImag() != 0 || left.getReal() != right;
569+
}
570+
543571
@SuppressWarnings("unused")
544572
@Fallback
545573
PNotImplemented doGeneric(Object left, Object right) {
@@ -632,7 +660,7 @@ int hash(PComplex self) {
632660
return realHash + PComplex.IMAG_MULTIPLIER * imagHash;
633661
}
634662
}
635-
663+
636664
@GenerateNodeFactory
637665
@Builtin(name = "conjugate", minNumOfPositionalArgs = 1)
638666
abstract static class ConjugateNode extends PythonUnaryBuiltinNode {

0 commit comments

Comments
 (0)