Skip to content

Commit e67e945

Browse files
author
loewis
committed
Fix repr for negative imaginary part. Fixes #1013908.
git-svn-id: http://svn.python.org/projects/python/trunk@37097 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent d233001 commit e67e945

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Lib/test/test_complex.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ def test_abs(self):
286286

287287
def test_repr(self):
288288
self.assertEqual(repr(1+6j), '(1+6j)')
289+
self.assertEqual(repr(1-6j), '(1-6j)')
289290

290291
def test_neg(self):
291292
self.assertEqual(-(1+6j), -1-6j)

Objects/complexobject.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,15 @@ complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision)
279279
strncat(buf, "j", bufsz);
280280
} else {
281281
char re[64], im[64];
282-
282+
char *fmt;
283283
PyOS_snprintf(format, 32, "%%.%ig", precision);
284284
PyOS_ascii_formatd(re, 64, format, v->cval.real);
285285
PyOS_ascii_formatd(im, 64, format, v->cval.imag);
286-
PyOS_snprintf(buf, bufsz, "(%s+%sj)", re, im);
286+
if (v->cval.imag < 0.)
287+
fmt = "(%s%sj)";
288+
else
289+
fmt = "(%s+%sj)";
290+
PyOS_snprintf(buf, bufsz, fmt, re, im);
287291
}
288292
}
289293

0 commit comments

Comments
 (0)