[Numpy-svn] r3906 - in trunk/numpy/core: src tests
numpy-svn at scipy.org
numpy-svn at scipy.org
Fri Jul 27 10:36:51 EDT 2007
Author: stefan
Date: 2007-07-27 09:36:28 -0500 (Fri, 27 Jul 2007)
New Revision: 3906
Modified:
trunk/numpy/core/src/scalarmathmodule.c.src
trunk/numpy/core/tests/test_scalarmath.py
Log:
Convert large integer scalars to long instead of to int [patch by
Xavier]. Closes ticket #549.
Modified: trunk/numpy/core/src/scalarmathmodule.c.src
===================================================================
--- trunk/numpy/core/src/scalarmathmodule.c.src 2007-07-27 13:57:12 UTC (rev 3905)
+++ trunk/numpy/core/src/scalarmathmodule.c.src 2007-07-27 14:36:28 UTC (rev 3906)
@@ -805,13 +805,37 @@
/**end repeat**/
/**begin repeat
- #name=(byte,ubyte,short,ushort,int,uint,long,ulong,longlong,ulonglong,float,double,longdouble,cfloat,cdouble,clongdouble)*3#
- #Name=(Byte,UByte,Short,UShort,Int,UInt,Long,ULong,LongLong,ULongLong,Float,Double,LongDouble,CFloat,CDouble,CLongDouble)*3#
- #cmplx=(,,,,,,,,,,,,,.real,.real,.real)*3#
- #which=int*16,long*16,float*16#
- #func=PyInt_FromLong*16,(PyLong_FromLongLong, PyLong_FromUnsignedLongLong)*5,PyLong_FromDouble*6,PyFloat_FromDouble*16#
+ #name=byte,ubyte,short,ushort,int,uint,long,ulong,longlong,ulonglong,float,double,longdouble,cfloat,cdouble,clongdouble#
+ #Name=Byte,UByte,Short,UShort,Int,UInt,Long,ULong,LongLong,ULongLong,Float,Double,LongDouble,CFloat,CDouble,CLongDouble#
+ #cmplx=,,,,,,,,,,,,,.real,.real,.real#
+ #sign=(signed,unsigned)*5,,,,,,#
+ #ctype=long*8,PY_LONG_LONG*2,double*6#
+ #realtyp=0*10,1*6#
+ #func=(PyLong_FromLong,PyLong_FromUnsignedLong)*4,PyLong_FromLongLong,PyLong_FromUnsignedLongLong,PyLong_FromDouble*6#
**/
static PyObject *
+ at name@_int(PyObject *obj)
+{
+ @sign@ @ctype@ x= PyArrayScalar_VAL(obj, @Name@)@cmplx@;
+#if @realtyp@
+ double ix;
+ modf(x, &ix);
+ x = ix;
+#endif
+ if(LONG_MIN < x && x < LONG_MAX)
+ return PyInt_FromLong(x);
+ return @func@(x);
+}
+/**end repeat**/
+
+/**begin repeat
+ #name=(byte,ubyte,short,ushort,int,uint,long,ulong,longlong,ulonglong,float,double,longdouble,cfloat,cdouble,clongdouble)*2#
+ #Name=(Byte,UByte,Short,UShort,Int,UInt,Long,ULong,LongLong,ULongLong,Float,Double,LongDouble,CFloat,CDouble,CLongDouble)*2#
+ #cmplx=(,,,,,,,,,,,,,.real,.real,.real)*2#
+ #which=long*16,float*16#
+ #func=(PyLong_FromLongLong, PyLong_FromUnsignedLongLong)*5,PyLong_FromDouble*6,PyFloat_FromDouble*16#
+**/
+static PyObject *
@name at _@which@(PyObject *obj)
{
return @func@((PyArrayScalar_VAL(obj, @Name@))@cmplx@);
Modified: trunk/numpy/core/tests/test_scalarmath.py
===================================================================
--- trunk/numpy/core/tests/test_scalarmath.py 2007-07-27 13:57:12 UTC (rev 3905)
+++ trunk/numpy/core/tests/test_scalarmath.py 2007-07-27 14:36:28 UTC (rev 3906)
@@ -51,5 +51,16 @@
b = a ** 4
assert b == 6765201, "error with %r: got %r" % (t,b)
+class test_conversion(NumpyTestCase):
+ def test_int_from_long(self):
+ l = [1e6, 1e12, 1e18, -1e6, -1e12, -1e18]
+ li = [10**6, 10**12, 10**18, -10**6, -10**12, -10**18]
+ for T in [None,N.float64,N.int64]:
+ a = N.array(l,dtype=T)
+ assert_equal(map(int,a), li)
+
+ a = N.array(l[:3],dtype=N.uint64)
+ assert_equal(map(int,a), li[:3])
+
if __name__ == "__main__":
NumpyTest().run()
More information about the Numpy-svn
mailing list