diff options
author | Stefan van der Walt <stefan@sun.ac.za> | 2007-07-27 14:36:28 +0000 |
---|---|---|
committer | Stefan van der Walt <stefan@sun.ac.za> | 2007-07-27 14:36:28 +0000 |
commit | 2994141453421f306bcd891d559b31b554f220db (patch) | |
tree | 798e3be10369eb1c7e4b41c50ce782c937ed34de | |
parent | e4ccde2aa3ee6b6edb5b22ad5c0dcda430dac8e1 (diff) | |
download | numpy-2994141453421f306bcd891d559b31b554f220db.tar.gz |
Convert large integer scalars to long instead of to int [patch by
Xavier]. Closes ticket #549.
-rw-r--r-- | numpy/core/src/scalarmathmodule.c.src | 34 | ||||
-rw-r--r-- | numpy/core/tests/test_scalarmath.py | 11 |
2 files changed, 40 insertions, 5 deletions
diff --git a/numpy/core/src/scalarmathmodule.c.src b/numpy/core/src/scalarmathmodule.c.src index 00f7c6ed6..0310b3567 100644 --- a/numpy/core/src/scalarmathmodule.c.src +++ b/numpy/core/src/scalarmathmodule.c.src @@ -805,11 +805,35 @@ static int /**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 * +@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@_@which@(PyObject *obj) diff --git a/numpy/core/tests/test_scalarmath.py b/numpy/core/tests/test_scalarmath.py index ecb695c5c..bb4974b12 100644 --- a/numpy/core/tests/test_scalarmath.py +++ b/numpy/core/tests/test_scalarmath.py @@ -51,5 +51,16 @@ class test_power(NumpyTestCase): 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() |