[Python-checkins] CVS: python/dist/src/Objects complexobject.c,2.33,2.34 floatobject.c,2.80,2.81

Tim Peters tim_one@usw-pr-cvs1.sourceforge.net
2001年3月11日 00:37:33 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv3566/python/dist/src/Objects
Modified Files:
	complexobject.c floatobject.c 
Log Message:
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
apparently forgot to play along. Make complex act like float.
Index: complexobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/complexobject.c,v
retrieving revision 2.33
retrieving revision 2.34
diff -C2 -r2.33 -r2.34
*** complexobject.c	2001年01月18日 01:12:39	2.33
--- complexobject.c	2001年03月11日 08:37:29	2.34
***************
*** 10,14 ****
--- 10,29 ----
 #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 */
 
***************
*** 174,178 ****
 		cv.imag = 0.;
 		return cv;
! 	} 
 }
 
--- 189,193 ----
 		cv.imag = 0.;
 		return cv;
! 	}
 }
 
***************
*** 185,202 ****
 
 static void
! complex_buf_repr(char *buf, PyComplexObject *v)
 {
 	if (v->cval.real == 0.)
! 		sprintf(buf, "%.12gj", v->cval.imag);
 	else
! 		sprintf(buf, "(%.12g%+.12gj)", v->cval.real, v->cval.imag);
 }
 
 static int
 complex_print(PyComplexObject *v, FILE *fp, int flags)
- /* flags -- not used but required by interface */
 {
 	char buf[100];
! 	complex_buf_repr(buf, v);
 	fputs(buf, fp);
 	return 0;
--- 200,218 ----
 
 static void
! complex_to_buf(char *buf, PyComplexObject *v, int precision)
 {
 	if (v->cval.real == 0.)
! 		sprintf(buf, "%.*gj", precision, v->cval.imag);
 	else
! 		sprintf(buf, "(%.*g%+.*gj)", precision, v->cval.real,
! 					 precision, v->cval.imag);
 }
 
 static int
 complex_print(PyComplexObject *v, FILE *fp, int flags)
 {
 	char buf[100];
! 	complex_to_buf(buf, v,
! 		 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
 	fputs(buf, fp);
 	return 0;
***************
*** 206,211 ****
 complex_repr(PyComplexObject *v)
 {
 	char buf[100];
! 	complex_buf_repr(buf, v);
 	return PyString_FromString(buf);
 }
--- 222,235 ----
 complex_repr(PyComplexObject *v)
 {
+ 	char buf[100];
+ 	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);
 }
***************
*** 542,546 ****
 	(hashfunc)complex_hash, 		/* tp_hash */
 	0,					/* tp_call */
! 	0,					/* tp_str */
 	0,					/* tp_getattro */
 	0,					/* tp_setattro */
--- 566,570 ----
 	(hashfunc)complex_hash, 		/* tp_hash */
 	0,					/* tp_call */
! 	(reprfunc)complex_str,			/* tp_str */
 	0,					/* tp_getattro */
 	0,					/* tp_setattro */
Index: floatobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/floatobject.c,v
retrieving revision 2.80
retrieving revision 2.81
diff -C2 -r2.80 -r2.81
*** floatobject.c	2001年03月06日 12:14:54	2.80
--- floatobject.c	2001年03月11日 08:37:29	2.81
***************
*** 315,319 ****
 static int
 float_print(PyFloatObject *v, FILE *fp, int flags)
- /* flags -- not used but required by interface */
 {
 	char buf[100];
--- 315,318 ----

AltStyle によって変換されたページ (->オリジナル) /