[Python-checkins] CVS: python/dist/src/Objects floatobject.c,2.52,2.53
Guido van Rossum
guido@cnri.reston.va.us
Thu, 23 Dec 1999 14:00:31 -0500 (EST)
Update of /projects/cvsroot/python/dist/src/Objects
In directory eric:/projects/python/develop/guido/src/Objects
Modified Files:
floatobject.c
Log Message:
Implement the other easy thing: repr() of a float now uses %.17g,
while str() uses %.12g as before.
Index: floatobject.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Objects/floatobject.c,v
retrieving revision 2.52
retrieving revision 2.53
diff -C2 -r2.52 -r2.53
*** floatobject.c 1999/10/12 19:54:48 2.52
--- floatobject.c 1999/12/23 19:00:28 2.53
***************
*** 244,250 ****
void
! PyFloat_AsString(buf, v)
char *buf;
PyFloatObject *v;
{
register char *cp;
--- 244,251 ----
void
! PyFloat_AsStringEx(buf, v, precision)
char *buf;
PyFloatObject *v;
+ int precision;
{
register char *cp;
***************
*** 254,258 ****
However, %g may print the number as an integer;
in such cases, we append ".0" to the string. */
! sprintf(buf, "%.12g", v->ob_fval);
cp = buf;
if (*cp == '-')
--- 255,259 ----
However, %g may print the number as an integer;
in such cases, we append ".0" to the string. */
! sprintf(buf, "%.*g", precision, v->ob_fval);
cp = buf;
if (*cp == '-')
***************
*** 271,274 ****
--- 272,300 ----
}
+ /* 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
+
+ void
+ PyFloat_AsString(buf, v)
+ char *buf;
+ PyFloatObject *v;
+ {
+ PyFloat_AsStringEx(buf, v, PREC_STR);
+ }
+
/* ARGSUSED */
static int
***************
*** 279,283 ****
{
char buf[100];
! PyFloat_AsString(buf, v);
fputs(buf, fp);
return 0;
--- 305,309 ----
{
char buf[100];
! PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
fputs(buf, fp);
return 0;
***************
*** 288,293 ****
PyFloatObject *v;
{
char buf[100];
! PyFloat_AsString(buf, v);
return PyString_FromString(buf);
}
--- 314,328 ----
PyFloatObject *v;
{
+ char buf[100];
+ PyFloat_AsStringEx(buf, v, PREC_REPR);
+ return PyString_FromString(buf);
+ }
+
+ static PyObject *
+ float_str(v)
+ PyFloatObject *v;
+ {
char buf[100];
! PyFloat_AsStringEx(buf, v, PREC_STR);
return PyString_FromString(buf);
}
***************
*** 674,682 ****
0, /*tp_setattr*/
(cmpfunc)float_compare, /*tp_compare*/
! (reprfunc)float_repr, /*tp_repr*/
&float_as_number, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
! (hashfunc)float_hash, /*tp_hash*/
};
--- 709,719 ----
0, /*tp_setattr*/
(cmpfunc)float_compare, /*tp_compare*/
! (reprfunc)float_repr, /*tp_repr*/
&float_as_number, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
! (hashfunc)float_hash, /*tp_hash*/
! 0, /*tp_call*/
! (reprfunc)float_str, /*tp_str*/
};