Skip to content

Commit 9bdb8af

Browse files
committed
Print real part for complex even if it's zero
* adapt Lib/test/test_format.py and Lib/test/test_complex.py * adjust doctests
1 parent 93a2a5b commit 9bdb8af

File tree

5 files changed

+38
-19
lines changed

5 files changed

+38
-19
lines changed

Doc/library/functions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ are always available. They are listed here in alphabetical order.
387387
>>> complex('+1.23')
388388
(1.23+0j)
389389
>>> complex('-4.5j')
390-
-4.5j
390+
(0.0-4.5j)
391391
>>> complex('-1.23+4.5j')
392392
(-1.23+4.5j)
393393
>>> complex('\t( -1.23+4.5J )\n')
@@ -397,7 +397,7 @@ are always available. They are listed here in alphabetical order.
397397
>>> complex(1.23)
398398
(1.23+0j)
399399
>>> complex(imag=-4.5)
400-
-4.5j
400+
(0.0-4.5j)
401401
>>> complex(-1.23, 4.5)
402402
(-1.23+4.5j)
403403

Lib/test/test_complex.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -811,9 +811,13 @@ def test(v, expected, test_fn=self.assertEqual):
811811
test(complex(NAN, NAN), "(nan+nanj)")
812812
test(complex(-NAN, -NAN), "(nan+nanj)")
813813

814-
test(complex(0, INF), "infj")
815-
test(complex(0, -INF), "-infj")
816-
test(complex(0, NAN), "nanj")
814+
test(complex(0, INF), "(0.0+infj)")
815+
test(complex(0, -INF), "(0.0-infj)")
816+
test(complex(0, NAN), "(0.0+nanj)")
817+
818+
test(imaginary(INF), "infj")
819+
test(imaginary(-INF), "-infj")
820+
test(imaginary(NAN), "nanj")
817821

818822
self.assertEqual(1-6j,complex(repr(1-6j)))
819823
self.assertEqual(1+6j,complex(repr(1+6j)))
@@ -826,16 +830,22 @@ def test(v, expected, test_fn=self.assertEqual):
826830
test_fn(repr(v), expected)
827831
test_fn(str(v), expected)
828832

829-
test(complex(0., 1.), "1j")
833+
test(complex(0., 1.), "(0.0+1j)")
830834
test(complex(-0., 1.), "(-0.0+1j)")
831-
test(complex(0., -1.), "-1j")
835+
test(complex(0., -1.), "(0.0-1j)")
832836
test(complex(-0., -1.), "(-0.0-1j)")
833837

834-
test(complex(0., 0.), "0j")
835-
test(complex(0., -0.), "-0j")
838+
test(imaginary(+1.), "1j")
839+
test(imaginary(-1.), "-1j")
840+
841+
test(complex(0., 0.), "(0.0+0j)")
842+
test(complex(0., -0.), "(0.0-0j)")
836843
test(complex(-0., 0.), "(-0.0+0j)")
837844
test(complex(-0., -0.), "(-0.0-0j)")
838845

846+
test(imaginary(+0.0), "0j")
847+
test(imaginary(-0.0), "-0j")
848+
839849
def test_pos(self):
840850
self.assertEqual(+(1+6j), 1+6j)
841851
self.assertEqual(+ComplexSubclass(1, 6), 1+6j)
@@ -943,7 +953,8 @@ def test_format(self):
943953
self.assertEqual(format(z, '3'), str(z))
944954

945955
self.assertEqual(format(1+3j, 'g'), '1+3j')
946-
self.assertEqual(format(3j, 'g'), '0+3j')
956+
self.assertEqual(format(0+3j, 'g'), '0.0+3j')
957+
self.assertEqual(format(3j, 'g'), '3j')
947958
self.assertEqual(format(1.5+3.5j, 'g'), '1.5+3.5j')
948959

949960
self.assertEqual(format(1.5+3.5j, '+g'), '+1.5+3.5j')

Lib/test/test_format.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,15 @@ def test_negative_zero(self):
595595
self.assertEqual(f"{-1.:+z.0f}", "-1")
596596
self.assertEqual(f"{-1.:-z.0f}", "-1")
597597

598-
self.assertEqual(f"{0.j:z.1f}", "0.0+0.0j")
599-
self.assertEqual(f"{-0.j:z.1f}", "0.0+0.0j")
600-
self.assertEqual(f"{.01j:z.1f}", "0.0+0.0j")
601-
self.assertEqual(f"{-.01j:z.1f}", "0.0+0.0j")
598+
self.assertEqual(f"{0.0+0.j:z.1f}", "0.0+0.0j")
599+
self.assertEqual(f"{0.0-0.j:z.1f}", "0.0+0.0j")
600+
self.assertEqual(f"{0.0+.01j:z.1f}", "0.0+0.0j")
601+
self.assertEqual(f"{0.0-.01j:z.1f}", "0.0+0.0j")
602+
603+
self.assertEqual(f"{0.j:z.1f}", "0.0j")
604+
self.assertEqual(f"{-0.j:z.1f}", "0.0j")
605+
self.assertEqual(f"{.01j:z.1f}", "0.0j")
606+
self.assertEqual(f"{-.01j:z.1f}", "0.0j")
602607

603608
self.assertEqual(f"{-0.:z>6.1f}", "zz-0.0") # test fill, esp. 'z' fill
604609
self.assertEqual(f"{-0.:z>z6.1f}", "zzz0.0")

Objects/complexobject.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,8 @@ complex_repr(PyComplexObject *v)
422422
const char *lead = "";
423423
const char *tail = "";
424424

425-
if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
426-
/* Real part is +0: just output the imaginary part and do not
427-
include parens. */
425+
if (PyImaginary_Check(v)) {
426+
/* Just output the imaginary part and do not include parens. */
428427
re = "";
429428
im = PyOS_double_to_string(v->cval.imag, format_code,
430429
precision, 0, NULL);

Python/formatter_unicode.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ format_complex_internal(PyObject *value,
12711271
/* Omitted type specifier. Should be like str(self). */
12721272
type = 'r';
12731273
default_precision = 0;
1274-
if (re == 0.0 && copysign(1.0, re) == 1.0)
1274+
if (PyImaginary_Check(value))
12751275
skip_re = 1;
12761276
else
12771277
add_parens = 1;
@@ -1290,10 +1290,14 @@ format_complex_internal(PyObject *value,
12901290
/* Cast "type", because if we're in unicode we need to pass an
12911291
8-bit char. This is safe, because we've restricted what "type"
12921292
can be. */
1293-
if (re == 0.0 && copysign(1.0, re) == -1.0)
1293+
if (re == 0.0) {
1294+
if (PyImaginary_Check(value)) {
1295+
skip_re = 1;
1296+
}
12941297
re_buf = PyOS_double_to_string(re, (char)type, precision,
12951298
flags | Py_DTSF_ADD_DOT_0,
12961299
&re_float_type);
1300+
}
12971301
else
12981302
re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
12991303
&re_float_type);

0 commit comments

Comments
 (0)