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

numpy-svn at scipy.org numpy-svn at scipy.org
Tue Apr 24 17:56:10 EDT 2007


Author: oliphant
Date: 2007-04-24 16:56:06 -0500 (Tue, 24 Apr 2007)
New Revision: 3727

Modified:
   trunk/numpy/core/src/umathmodule.c.src
Log:
Restore invariant of (x == (x/y)*y + (x%y)) by making integer division with mixed-sign operands match Python.

Modified: trunk/numpy/core/src/umathmodule.c.src
===================================================================
--- trunk/numpy/core/src/umathmodule.c.src	2007-04-24 19:15:33 UTC (rev 3726)
+++ trunk/numpy/core/src/umathmodule.c.src	2007-04-24 21:56:06 UTC (rev 3727)
@@ -1039,9 +1039,8 @@
 
 
 /**begin repeat
-#TYP=BYTE,UBYTE,SHORT,USHORT,INT,UINT,LONG,ULONG,LONGLONG,ULONGLONG#
-#typ=char, ubyte, short, ushort, int, uint, long, ulong, longlong, ulonglong#
-#otyp=float*4, double*6#
+#TYP=UBYTE,USHORT,UINT,ULONG,ULONGLONG#
+#typ=ubyte, ushort, uint, ulong, ulonglong#
 */
 static void
 @TYP at _divide(char **args, intp *dimensions, intp *steps, void *func)
@@ -1058,7 +1057,42 @@
 		}
 	}
 }
+/**end repeat**/
+
+
+/**begin repeat
+#TYP=BYTE,SHORT,INT,LONG,LONGLONG#
+#typ=char, short, int, long, longlong#
+*/
 static void
+ at TYP@_divide(char **args, intp *dimensions, intp *steps, void *func)
+{
+	register intp i, is1=steps[0],is2=steps[1],os=steps[2],n=dimensions[0];
+	char *i1=args[0], *i2=args[1], *op=args[2];
+        @typ@ x, y, tmp;
+	for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) {
+                y = *((@typ@ *)i2);
+		if (y == 0) {
+			generate_divbyzero_error();
+			*((@typ@ *)op)=0;
+		}
+		else {
+                        x = *((@typ@ *)i1);
+                        tmp = x / y;
+                        if (((x > 0) != (y > 0)) && (x % y != 0)) tmp--;
+                        *((@typ@ *)op)= tmp;
+		}
+	}
+}
+/**end repeat**/
+
+
+/**begin repeat
+#TYP=BYTE,UBYTE,SHORT,USHORT,INT,UINT,LONG,ULONG,LONGLONG,ULONGLONG#
+#typ=char, ubyte, short, ushort, int, uint, long, ulong, longlong, ulonglong#
+#otyp=float*4, double*6#
+*/
+static void
 @TYP at _true_divide(char **args, intp *dimensions, intp *steps, void *func)
 {
 	register intp i, is1=steps[0],is2=steps[1],os=steps[2],n=dimensions[0];
@@ -1077,6 +1111,8 @@
 #define @TYP at _floor_divide @TYP at _divide
 /**end repeat**/
 
+
+
 /**begin repeat
 #TYP=FLOAT,DOUBLE,LONGDOUBLE#
 #typ=float,double,longdouble#




More information about the Numpy-svn mailing list