[Numpy-tickets] [NumPy] #728: numpy.r_ incorrectly casts with mixed types
NumPy
numpy-tickets@scipy....
Wed Apr 9 09:39:49 CDT 2008
#728: numpy.r_ incorrectly casts with mixed types
------------------------+---------------------------------------------------
Reporter: bsouthey | Owner: somebody
Type: defect | Status: new
Priority: high | Milestone: 1.0.5
Component: numpy.core | Version: none
Severity: normal | Keywords:
------------------------+---------------------------------------------------
Numpy.r_ appears to use the type of the array argument in determining the
return types. So when arguments have mixed types, there can be a loss of
precision:
Correct when all arguments have the same type:
{{{
import numpy
ra=numpy.r_[-10, numpy.array([2, 3, 4]), 10]
type(ra), type(ra[0]), type(ra[1])
}}}
Provides: (<type 'numpy.ndarray'>, <type 'numpy.int64'>, <type
'numpy.int64'>)
It is also correct if at least one array has the 'correct' type:
{{{
ra=numpy.r_[-10.1, numpy.array([1]), numpy.array([2, 3, 4], dtype=float),
10]
type(ra), type(ra[0]), type(ra[1]), ra[0], ra[4]
}}}
Provides: (<type 'numpy.ndarray'>, <type 'numpy.float64'>, <type
'numpy.float64'>, -10.1, 4.0)
But if mixed types are used:
{{{
import numpy
ra=numpy.r_[-10.1, numpy.array([2, 3, 4]), 10.0]
type(ra), type(ra[0]), type(ra[1]), ra[0], ra[4]
}}}
Provides: (<type 'numpy.ndarray'>, <type 'numpy.int64'>, <type
'numpy.int64'>, -10, 10)[[BR]]
So the float has been down cast to a integer so 10.1 becomes 10.
The workaround is to ensure that all numpy arrays have the same type as
required:
{{{
import numpy
ra=numpy.r_[-10.1, numpy.array([2, 3, 4], dtype=float), 10]
type(ra), type(ra[0]), type(ra[1]), ra[0], ra[4]
}}}
Provides:
(<type 'numpy.ndarray'>, <type 'numpy.float64'>, <type 'numpy.float64'>,
-10.1, 10.0)
--
Ticket URL: <http://scipy.org/scipy/numpy/ticket/728>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.
More information about the Numpy-tickets
mailing list