[Numpy-svn] r3110 - in trunk/numpy: . core core/code_generators core/src

numpy-svn at scipy.org numpy-svn at scipy.org
Mon Sep 4 17:52:21 EDT 2006


Author: oliphant
Date: 2006-09-04 16:52:13 -0500 (Mon, 04 Sep 2006)
New Revision: 3110

Modified:
   trunk/numpy/__init__.py
   trunk/numpy/core/code_generators/generate_umath.py
   trunk/numpy/core/fromnumeric.py
   trunk/numpy/core/src/multiarraymodule.c
   trunk/numpy/core/src/umathmodule.c.src
Log:
Fix rounding of integers with decimal < 0

Modified: trunk/numpy/__init__.py
===================================================================
--- trunk/numpy/__init__.py	2006-09-04 19:31:30 UTC (rev 3109)
+++ trunk/numpy/__init__.py	2006-09-04 21:52:13 UTC (rev 3110)
@@ -3,14 +3,15 @@
 ==========
 
 You can support the development of NumPy and SciPy by purchasing
-"Guide to NumPy" at
+the book "Guide to NumPy" at
 
   http://www.trelgol.com
 
-It is being distributed for a fee for only a limited time to
-cover some of the costs of development.
+It is being distributed for a fee for only a few years to
+cover some of the costs of development.  After the restriction period
+it will also be freely available. 
 
-Documentation is also available in the docstrings and at
+Additional documentation is available in the docstrings and at
 
 http://www.scipy.org.
 """

Modified: trunk/numpy/core/code_generators/generate_umath.py
===================================================================
--- trunk/numpy/core/code_generators/generate_umath.py	2006-09-04 19:31:30 UTC (rev 3109)
+++ trunk/numpy/core/code_generators/generate_umath.py	2006-09-04 21:52:13 UTC (rev 3110)
@@ -432,7 +432,7 @@
 'rint' :
     Ufunc(1, 1, None,
           'round x elementwise to the nearest integer, round halfway cases away from zero',
-          TD(flts, f='rint'),
+          TD(inexact, f='rint'),
           TD(M, f='rint'),
           ),
 'arctan2' :

Modified: trunk/numpy/core/fromnumeric.py
===================================================================
--- trunk/numpy/core/fromnumeric.py	2006-09-04 19:31:30 UTC (rev 3109)
+++ trunk/numpy/core/fromnumeric.py	2006-09-04 21:52:13 UTC (rev 3110)
@@ -556,9 +556,10 @@
         Reference to out, where None specifies a copy of the original array a.
 
     Round to the specified number of decimals. When 'decimals' is negative it
-    specifies the number of positions to the left of the decimal point. The real
-    and imaginary parts of complex numbers are rounded separately. Nothing is
-    done if the array is not of float type.
+    specifies the number of positions to the left of the decimal point. The
+    real and imaginary parts of complex numbers are rounded separately.
+    Nothing is done if the array is not of float type and 'decimals' is greater
+    than or equal to 0.
 
     The keyword 'out' may be used to specify a different array to hold the
     result rather than the default copy of 'a'. If the type of the array

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2006-09-04 19:31:30 UTC (rev 3109)
+++ trunk/numpy/core/src/multiarraymodule.c	2006-09-04 21:52:13 UTC (rev 3110)
@@ -219,6 +219,8 @@
 PyArray_Round(PyArrayObject *a, int decimals, PyArrayObject *out)
 {
 	PyObject *f, *ret=NULL, *tmp, *op1, *op2;
+        int ret_int=0;
+        PyArray_Descr *my_descr;
         if (out && (PyArray_SIZE(out) != PyArray_SIZE(a))) {
                 PyErr_SetString(PyExc_ValueError,
                                 "invalid output shape");
@@ -284,17 +286,24 @@
                         return PyObject_CallFunction(n_ops.rint, "O", a);
                 }
                 op1 = n_ops.multiply;
-                op2 = n_ops.divide;
+                op2 = n_ops.true_divide;
         }
         else {
-                op1 = n_ops.divide;
+                op1 = n_ops.true_divide;
                 op2 = n_ops.multiply;
                 decimals = -decimals;
         }
         if (!out) {
-                Py_INCREF(a->descr);
+                if (PyArray_ISINTEGER(a)) {
+                        ret_int = 1;
+                        my_descr = PyArray_DescrFromType(NPY_DOUBLE);
+                }
+                else {
+                        Py_INCREF(a->descr);
+                        my_descr = a->descr;
+                }
                 out = (PyArrayObject *)PyArray_Empty(a->nd, a->dimensions,
-                                                     a->descr,
+                                                     my_descr,
                                                      PyArray_ISFORTRAN(a));
                 if (out == NULL) return NULL;
         }
@@ -313,6 +322,13 @@
  finish:
 	Py_DECREF(f);
         Py_DECREF(out);
+        if (ret_int) {
+                Py_INCREF(a->descr);
+                tmp = PyArray_CastToType((PyArrayObject *)ret, 
+                                         a->descr, PyArray_ISFORTRAN(a));
+                Py_DECREF(ret);
+                return tmp;
+        }
 	return ret;
 
 }

Modified: trunk/numpy/core/src/umathmodule.c.src
===================================================================
--- trunk/numpy/core/src/umathmodule.c.src	2006-09-04 19:31:30 UTC (rev 3109)
+++ trunk/numpy/core/src/umathmodule.c.src	2006-09-04 21:52:13 UTC (rev 3110)
@@ -268,6 +268,8 @@
 
 
 
+
+
 /* Define isnan, isinf, isfinite, signbit if needed */
 /* Use fpclassify if possible */
 /* isnan, isinf --
@@ -618,6 +620,13 @@
 }
 
 static void
+nc_rint at c@(c at typ@ *x, c at typ@ *r)
+{
+        r->real = rint at c@(x->real);
+        r->imag = rint at c@(x->imag);
+}
+
+static void
 nc_log at c@(c at typ@ *x, c at typ@ *r)
 {
 	@typ@ l = hypot at c@(x->real,x->imag);




More information about the Numpy-svn mailing list