[Numpy-svn] r5395 - trunk/numpy/core/src

numpy-svn at scipy.org numpy-svn at scipy.org
Sat Jul 12 17:45:11 EDT 2008


Author: charris
Date: 2008-07-12 16:45:08 -0500 (Sat, 12 Jul 2008)
New Revision: 5395

Modified:
   trunk/numpy/core/src/scalartypes.inc.src
Log:
Make printing of complex numbers match Python.


Modified: trunk/numpy/core/src/scalartypes.inc.src
===================================================================
--- trunk/numpy/core/src/scalartypes.inc.src	2008-07-12 06:26:40 UTC (rev 5394)
+++ trunk/numpy/core/src/scalartypes.inc.src	2008-07-12 21:45:08 UTC (rev 5395)
@@ -553,30 +553,45 @@
 /**begin repeat
  * #name=float, double, longdouble#
  * #NAME=FLOAT, DOUBLE, LONGDOUBLE#
- * #PREFIX=NPY_, NPY_, NPY_#
- *
- * Fixme: buflen isn't checked when appending .0
  */
+
+#define FMT "%.*" NPY_ at NAME@_FMT
+#define CFMT1 "%.*" NPY_ at NAME@_FMT "j"
+#define CFMT2 "(%.*" NPY_ at NAME@_FMT "%+.*" NPY_ at NAME@_FMT "j)"
+
 static void
-format_ at name@(char *buf, size_t buflen, @name@ val,
-                  unsigned int precision)
+format_ at name@(char *buf, size_t buflen, @name@ val, unsigned int prec)
 {
-    char *cp;
+    int cnt, i;
 
-    PyOS_snprintf(buf, buflen, "%.*" @PREFIX@@NAME at _FMT, precision, val);
-    cp = buf;
-    if (*cp == '-')
-        cp++;
-    for (; *cp != '\0'; cp++) {
-        if (!isdigit(Py_CHARMASK(*cp)))
+    cnt = PyOS_snprintf(buf, buflen, FMT, prec, val);
+
+    /* If nothing but digits after sign, append ".0" */
+    for (i = (val < 0) ? 1 : 0; i < cnt; ++i) {
+        if (!isdigit(Py_CHARMASK(buf[i]))) {
             break;
+        }
     }
-    if (*cp == '\0') {
-        *cp++ = '.';
-        *cp++ = '0';
-        *cp++ = '\0';
+    if (i == cnt && buflen >= cnt + 3) {
+        strcpy(&buf[cnt],".0");
     }
 }
+
+static void
+format_c at name@(char *buf, size_t buflen, c at name@ val, unsigned int prec)
+{
+    if (val.real == 0.0) {
+        PyOS_snprintf(buf, buflen, CFMT1, prec, val.imag);
+    }
+    else {
+        PyOS_snprintf(buf, buflen, CFMT2, prec, val.real, prec, val.imag);
+    }
+}
+
+#undef FMT
+#undef CFMT1
+#undef CFMT2
+
 /**end repeat**/
 
 /* over-ride repr and str of array-scalar strings and unicode to
@@ -639,29 +654,31 @@
  * #kind = str, repr#
  * #KIND = STR, REPR#
  */
+
+#define PREC @NAME at PREC_@KIND@
+
 static PyObject *
 @name at type_@kind@(PyObject *self)
 {
     char buf[100];
     @name@ val = ((Py at Name@ScalarObject *)self)->obval;
 
-    format_ at name@(buf, sizeof(buf), val, @NAME at PREC_@KIND@);
+    format_ at name@(buf, sizeof(buf), val, PREC);
     return PyString_FromString(buf);
 }
 
 static PyObject *
 c at name@type_ at kind@(PyObject *self)
 {
-    char buf1[100];
-    char buf2[100];
-    char buf3[202];
+    char buf[202];
     c at name@ val = ((PyC at Name@ScalarObject *)self)->obval;
 
-    format_ at name@(buf1, sizeof(buf1), val.real, @NAME at PREC_@KIND@);
-    format_ at name@(buf2, sizeof(buf2), val.imag, @NAME at PREC_@KIND@);
-    snprintf(buf3, sizeof(buf3), "(%s+%sj)", buf1, buf2);
-    return PyString_FromString(buf3);
+    format_c at name@(buf, sizeof(buf), val, PREC);
+    return PyString_FromString(buf);
 }
+
+#undef PREC
+
 /**end repeat1**/
 /**end repeat**/
 




More information about the Numpy-svn mailing list