Skip to content

Commit db12274

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 737293b commit db12274

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
@@ -389,7 +389,7 @@ are always available. They are listed here in alphabetical order.
389389
>>> complex('+1.23')
390390
(1.23+0j)
391391
>>> complex('-4.5j')
392-
-4.5j
392+
(0.0-4.5j)
393393
>>> complex('-1.23+4.5j')
394394
(-1.23+4.5j)
395395
>>> complex('\t( -1.23+4.5J )\n')
@@ -399,7 +399,7 @@ are always available. They are listed here in alphabetical order.
399399
>>> complex(1.23)
400400
(1.23+0j)
401401
>>> complex(imag=-4.5)
402-
-4.5j
402+
(0.0-4.5j)
403403
>>> complex(-1.23, 4.5)
404404
(-1.23+4.5j)
405405

Lib/test/test_complex.py

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

822-
test(complex(0, INF), "infj")
823-
test(complex(0, -INF), "-infj")
824-
test(complex(0, NAN), "nanj")
822+
test(complex(0, INF), "(0.0+infj)")
823+
test(complex(0, -INF), "(0.0-infj)")
824+
test(complex(0, NAN), "(0.0+nanj)")
825+
826+
test(imaginary(INF), "infj")
827+
test(imaginary(-INF), "-infj")
828+
test(imaginary(NAN), "nanj")
825829

826830
self.assertEqual(1-6j,complex(repr(1-6j)))
827831
self.assertEqual(1+6j,complex(repr(1+6j)))
@@ -834,16 +838,22 @@ def test(v, expected, test_fn=self.assertEqual):
834838
test_fn(repr(v), expected)
835839
test_fn(str(v), expected)
836840

837-
test(complex(0., 1.), "1j")
841+
test(complex(0., 1.), "(0.0+1j)")
838842
test(complex(-0., 1.), "(-0.0+1j)")
839-
test(complex(0., -1.), "-1j")
843+
test(complex(0., -1.), "(0.0-1j)")
840844
test(complex(-0., -1.), "(-0.0-1j)")
841845

842-
test(complex(0., 0.), "0j")
843-
test(complex(0., -0.), "-0j")
846+
test(imaginary(+1.), "1j")
847+
test(imaginary(-1.), "-1j")
848+
849+
test(complex(0., 0.), "(0.0+0j)")
850+
test(complex(0., -0.), "(0.0-0j)")
844851
test(complex(-0., 0.), "(-0.0+0j)")
845852
test(complex(-0., -0.), "(-0.0-0j)")
846853

854+
test(imaginary(+0.0), "0j")
855+
test(imaginary(-0.0), "-0j")
856+
847857
def test_pos(self):
848858
self.assertEqual(+(1+6j), 1+6j)
849859
self.assertEqual(+ComplexSubclass(1, 6), 1+6j)
@@ -950,7 +960,8 @@ def test_format(self):
950960
self.assertEqual(format(z, '3'), str(z))
951961

952962
self.assertEqual(format(1+3j, 'g'), '1+3j')
953-
self.assertEqual(format(3j, 'g'), '0+3j')
963+
self.assertEqual(format(0+3j, 'g'), '0.0+3j')
964+
self.assertEqual(format(3j, 'g'), '3j')
954965
self.assertEqual(format(1.5+3.5j, 'g'), '1.5+3.5j')
955966

956967
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
@@ -463,9 +463,8 @@ complex_repr(PyComplexObject *v)
463463
const char *lead = "";
464464
const char *tail = "";
465465

466-
if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
467-
/* Real part is +0: just output the imaginary part and do not
468-
include parens. */
466+
if (PyImaginary_Check(v)) {
467+
/* Just output the imaginary part and do not include parens. */
469468
re = "";
470469
im = PyOS_double_to_string(v->cval.imag, format_code,
471470
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)