When 1.6 boosted the # of digits produced by repr(float), repr(complex)

apparently forgot to play along.  Make complex act like float.
This commit is contained in:
Tim Peters 2001-03-11 08:37:29 +00:00
parent b3d6be9d93
commit 7069512bd0
2 changed files with 32 additions and 9 deletions

View File

@ -9,6 +9,21 @@
#include "Python.h" #include "Python.h"
/* Precisions used by repr() and str(), respectively.
The repr() precision (17 significant decimal digits) is the minimal number
that is guaranteed to have enough precision so that if the number is read
back in the exact same binary value is recreated. This is true for IEEE
floating point by design, and also happens to work for all other modern
hardware.
The str() precision is chosen so that in most cases, the rounding noise
created by various operations is suppressed, while giving plenty of
precision for practical use.
*/
#define PREC_REPR 17
#define PREC_STR 12
/* elementary operations on complex numbers */ /* elementary operations on complex numbers */
@ -173,7 +188,7 @@ PyComplex_AsCComplex(PyObject *op)
cv.real = PyFloat_AsDouble(op); cv.real = PyFloat_AsDouble(op);
cv.imag = 0.; cv.imag = 0.;
return cv; return cv;
} }
} }
static void static void
@ -184,20 +199,21 @@ complex_dealloc(PyObject *op)
static void static void
complex_buf_repr(char *buf, PyComplexObject *v) complex_to_buf(char *buf, PyComplexObject *v, int precision)
{ {
if (v->cval.real == 0.) if (v->cval.real == 0.)
sprintf(buf, "%.12gj", v->cval.imag); sprintf(buf, "%.*gj", precision, v->cval.imag);
else else
sprintf(buf, "(%.12g%+.12gj)", v->cval.real, v->cval.imag); sprintf(buf, "(%.*g%+.*gj)", precision, v->cval.real,
precision, v->cval.imag);
} }
static int static int
complex_print(PyComplexObject *v, FILE *fp, int flags) complex_print(PyComplexObject *v, FILE *fp, int flags)
/* flags -- not used but required by interface */
{ {
char buf[100]; char buf[100];
complex_buf_repr(buf, v); complex_to_buf(buf, v,
(flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
fputs(buf, fp); fputs(buf, fp);
return 0; return 0;
} }
@ -206,7 +222,15 @@ static PyObject *
complex_repr(PyComplexObject *v) complex_repr(PyComplexObject *v)
{ {
char buf[100]; char buf[100];
complex_buf_repr(buf, v); complex_to_buf(buf, v, PREC_REPR);
return PyString_FromString(buf);
}
static PyObject *
complex_str(PyComplexObject *v)
{
char buf[100];
complex_to_buf(buf, v, PREC_STR);
return PyString_FromString(buf); return PyString_FromString(buf);
} }
@ -541,7 +565,7 @@ PyTypeObject PyComplex_Type = {
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
(hashfunc)complex_hash, /* tp_hash */ (hashfunc)complex_hash, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ (reprfunc)complex_str, /* tp_str */
0, /* tp_getattro */ 0, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */

View File

@ -314,7 +314,6 @@ PyFloat_AsString(char *buf, PyFloatObject *v)
/* ARGSUSED */ /* ARGSUSED */
static int static int
float_print(PyFloatObject *v, FILE *fp, int flags) float_print(PyFloatObject *v, FILE *fp, int flags)
/* flags -- not used but required by interface */
{ {
char buf[100]; char buf[100];
PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR); PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);