Skip to content

Commit cb87fa3

Browse files
authored
Merge pull request RustPython#3228 from chrismoradi/fix-complex-repr-tests
Fix repr for complex numbers involving NaN
2 parents 03a6798 + 911de97 commit cb87fa3

3 files changed

Lines changed: 5 additions & 6 deletions

File tree

Lib/test/test_cmath.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ def test_constants(self):
156156
self.assertAlmostEqual(cmath.e, e_expected, places=9,
157157
msg="cmath.e is {}; should be {}".format(cmath.e, e_expected))
158158

159-
# TODO: RUSTPYTHON
160-
@unittest.expectedFailure
161159
def test_infinity_and_nan_constants(self):
162160
self.assertEqual(cmath.inf.real, math.inf)
163161
self.assertEqual(cmath.inf.imag, 0.0)

Lib/test/test_complex.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,6 @@ def test_abs(self):
460460
for num in nums:
461461
self.assertAlmostEqual((num.real**2 + num.imag**2) ** 0.5, abs(num))
462462

463-
# TODO: RUSTPYTHON
464-
@unittest.expectedFailure
465463
def test_repr_str(self):
466464
def test(v, expected, test_fn=self.assertEqual):
467465
test_fn(repr(v), expected)

vm/src/builtins/complex.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,13 @@ impl PyComplex {
335335
#[pymethod(magic)]
336336
fn repr(&self) -> String {
337337
let Complex64 { re, im } = self.value;
338+
// converting to lowercase to format "NaN" as "nan"
338339
if re == 0.0 {
339-
format!("{}j", im)
340+
format!("{}j", im).to_lowercase()
341+
} else if im.is_nan() {
342+
format!("({}+{:+}j)", re, im).to_lowercase()
340343
} else {
341-
format!("({}{:+}j)", re, im)
344+
format!("({}{:+}j)", re, im).to_lowercase()
342345
}
343346
}
344347

0 commit comments

Comments
 (0)