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
41 changes: 41 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import sys
import unittest
from math import atan2
import math

def test_create():
c = 4 + 4j
Expand Down Expand Up @@ -91,6 +92,46 @@ def test_sub():
assert (1.5 - c2) == complex(-0.5, -2)


def test_mixed_real_complex_zero_signs():
z = complex(0.0, -0.0)

def add_int_local():
x = 0
return z + x, x + z

def add_float_local():
x = 0.0
return z + x, x + z

def add_bool_local():
x = False
return z + x, x + z

for left, right in (add_int_local(), add_float_local(), add_bool_local()):
assert left == right
assert math.copysign(1.0, left.imag) == 1.0
assert math.copysign(1.0, right.imag) == 1.0

def sub_int_local():
x = 0
return x - z, z - x

def sub_float_local():
x = 0.0
return x - z, z - x

def sub_bool_local():
x = False
return x - z, z - x

# Subtraction is not commutative, but sign handling should follow CPython.
for left, right in (sub_int_local(), sub_float_local(), sub_bool_local()):
assert left == 0j
assert right == -0j
assert math.copysign(1.0, left.imag) == 1.0
assert math.copysign(1.0, right.imag) == -1.0


def test_div():
c2 = 2+2j
assert (c2 / 2) == complex(1, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,13 +966,25 @@ abstract static class AddNode extends BinaryOpBuiltinNode {
@Specialization
static PComplex doInt(PComplex left, int right,
@Bind PythonLanguage language) {
return PFactory.createComplex(language, left.getReal() + right, left.getImag());
return PFactory.createComplex(language, left.getReal() + right, left.getImag() + 0.0);
}

@Specialization
static PComplex doDouble(PComplex left, double right,
@Bind PythonLanguage language) {
return PFactory.createComplex(language, left.getReal() + right, left.getImag());
return PFactory.createComplex(language, left.getReal() + right, left.getImag() + 0.0);
}

@Specialization
static PComplex doInt(int left, PComplex right,
@Bind PythonLanguage language) {
return PFactory.createComplex(language, left + right.getReal(), 0.0 + right.getImag());
}

@Specialization
static PComplex doDouble(double left, PComplex right,
@Bind PythonLanguage language) {
return PFactory.createComplex(language, left + right.getReal(), 0.0 + right.getImag());
}

@Specialization
Expand Down Expand Up @@ -1082,13 +1094,25 @@ static ComplexValue multiply(ComplexValue left, ComplexValue right) {
abstract static class SubNode extends BinaryOpBuiltinNode {
static PComplex doComplex(PComplex left, double right,
@Bind PythonLanguage language) {
return PFactory.createComplex(language, left.getReal() - right, left.getImag());
return PFactory.createComplex(language, left.getReal() - right, left.getImag() - 0.0);
}

@Specialization
static PComplex doComplex(PComplex left, int right,
@Bind PythonLanguage language) {
return PFactory.createComplex(language, left.getReal() - right, left.getImag());
return PFactory.createComplex(language, left.getReal() - right, left.getImag() - 0.0);
}

@Specialization
static PComplex doComplex(int left, PComplex right,
@Bind PythonLanguage language) {
return PFactory.createComplex(language, left - right.getReal(), 0.0 - right.getImag());
}

@Specialization
static PComplex doComplex(double left, PComplex right,
@Bind PythonLanguage language) {
return PFactory.createComplex(language, left - right.getReal(), 0.0 - right.getImag());
}

@Specialization
Expand Down
Loading