TypeError when using double , longdouble in numpy.dot
Dear NumPy developers, I am trying to solve some scipy.sparse TypeError failures reported in [1] and reduced them to the following example:
import numpy a = numpy.array([[1]])
numpy.dot(a.astype('single'), a.astype('longdouble')) array([[1.0]], dtype=float64)
numpy.dot(a.astype('double'), a.astype('longdouble')) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: array cannot be safely cast to required type
Is this exception expected? Also I noticed this:
numpy.array([1]).astype('longdouble').dtype.num 13 numpy.array([1.0]).astype('longdouble').dtype.num 12
I am using Python 2.6.5 for Windows and numpy 1.4.1 compiled with msvc9, where sizeof(longdouble) == sizeof(double). [1] http://aspn.activestate.com/ASPN/Mail/Message/scipy-user/3875416 -- Christoph
On Thu, Jul 8, 2010 at 6:13 AM, Christoph Gohlke <cgohlke@uci.edu> wrote:
Dear NumPy developers,
I am trying to solve some scipy.sparse TypeError failures reported in [1] and reduced them to the following example:
import numpy a = numpy.array([[1]])
numpy.dot(a.astype('single'), a.astype('longdouble')) array([[1.0]], dtype=float64)
numpy.dot(a.astype('double'), a.astype('longdouble')) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: array cannot be safely cast to required type
Is this exception expected?
No, I don't think so. The error seems to be platform specific - I have the expected result on my macbook.
Also I noticed this:
numpy.array([1]).astype('longdouble').dtype.num 13 numpy.array([1.0]).astype('longdouble').dtype.num 12
This is unexpected. There maybe some untested/buggy codepaths for the windows case (where sizeof(double) == sizeof(long double)). I will try to look into it, but please post an issue on trac so that it does not get lost, David
On 7/7/2010 9:25 PM, David Cournapeau wrote:
On Thu, Jul 8, 2010 at 6:13 AM, Christoph Gohlke<cgohlke@uci.edu> wrote:
Dear NumPy developers,
I am trying to solve some scipy.sparse TypeError failures reported in [1] and reduced them to the following example:
import numpy a = numpy.array([[1]])
numpy.dot(a.astype('single'), a.astype('longdouble')) array([[1.0]], dtype=float64)
numpy.dot(a.astype('double'), a.astype('longdouble')) Traceback (most recent call last): File "<stdin>", line 1, in<module> TypeError: array cannot be safely cast to required type
Is this exception expected?
No, I don't think so. The error seems to be platform specific - I have the expected result on my macbook.
Also I noticed this:
numpy.array([1]).astype('longdouble').dtype.num 13 numpy.array([1.0]).astype('longdouble').dtype.num 12
This is unexpected. There maybe some untested/buggy codepaths for the windows case (where sizeof(double) == sizeof(long double)). I will try to look into it, but please post an issue on trac so that it does not get lost,
Thank you. I opened a ticket: http://projects.scipy.org/numpy/ticket/1539 -- Christoph
On Wed, Jul 7, 2010 at 10:13 PM, Christoph Gohlke <cgohlke@uci.edu> wrote:
Dear NumPy developers,
I am trying to solve some scipy.sparse TypeError failures reported in [1] and reduced them to the following example:
import numpy a = numpy.array([[1]])
numpy.dot(a.astype('single'), a.astype('longdouble')) array([[1.0]], dtype=float64)
numpy.dot(a.astype('double'), a.astype('longdouble')) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: array cannot be safely cast to required type
Is this exception expected?
I think not. On some platforms longdouble is the same as double, on others it is extended precision or quad precision. On your platform this looks like a bug, on my platform it would be correct except there is a fallback version of dot that works with extended precision. Is there a mix of compilers here, or is it msvc all the way down. In [5]: a = array([[1]]) In [6]: dot(a.astype('single'), a.astype('longdouble')) Out[6]: array([[1.0]], dtype=float128) Also I noticed this:
numpy.array([1]).astype('longdouble').dtype.num 13 numpy.array([1.0]).astype('longdouble').dtype.num 12
Yeah, that is probably correct in a strange sort of way since the two types are identical under the hood. On ubuntu I get In [1]: array([1]).astype('longdouble').dtype.num Out[1]: 13 In [2]: array([1.]).astype('longdouble').dtype.num Out[2]: 13 Type numbers aren't a good way to determine precision in a platform independent way.
I am using Python 2.6.5 for Windows and numpy 1.4.1 compiled with msvc9, where sizeof(longdouble) == sizeof(double).
Chuck
On 7/7/2010 9:43 PM, Charles R Harris wrote:
On Wed, Jul 7, 2010 at 10:13 PM, Christoph Gohlke <cgohlke@uci.edu <mailto:cgohlke@uci.edu>> wrote:
Dear NumPy developers,
I am trying to solve some scipy.sparse TypeError failures reported in [1] and reduced them to the following example:
> >> import numpy > >> a = numpy.array([[1]])
> >> numpy.dot(a.astype('single'), a.astype('longdouble')) array([[1.0]], dtype=float64)
> >> numpy.dot(a.astype('double'), a.astype('longdouble')) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: array cannot be safely cast to required type
Is this exception expected?
I think not. On some platforms longdouble is the same as double, on others it is extended precision or quad precision. On your platform this looks like a bug, on my platform it would be correct except there is a fallback version of dot that works with extended precision. Is there a mix of compilers here, or is it msvc all the way down.
Yes, msvc9 all the way down. It fails no matter whether I build with setup.py or setupscons.py. Using mingw build gives the expected results.
In [5]: a = array([[1]])
In [6]: dot(a.astype('single'), a.astype('longdouble')) Out[6]: array([[1.0]], dtype=float128)
Also I noticed this:
> >> numpy.array([1]).astype('longdouble').dtype.num 13 > >> numpy.array([1.0]).astype('longdouble').dtype.num 12
Yeah, that is probably correct in a strange sort of way since the two types are identical under the hood. On ubuntu I get
In [1]: array([1]).astype('longdouble').dtype.num Out[1]: 13
In [2]: array([1.]).astype('longdouble').dtype.num Out[2]: 13
Type numbers aren't a good way to determine precision in a platform independent way.
I should have mentioned that the following example works for me:
a = numpy.array([[1.0]]) numpy.dot(a.astype('double'), a.astype('longdouble')) array([[ 1.]])
-- Christoph
On Wed, Jul 7, 2010 at 10:13 PM, Christoph Gohlke <cgohlke@uci.edu> wrote:
Dear NumPy developers,
I am trying to solve some scipy.sparse TypeError failures reported in [1] and reduced them to the following example:
import numpy a = numpy.array([[1]])
numpy.dot(a.astype('single'), a.astype('longdouble')) array([[1.0]], dtype=float64)
numpy.dot(a.astype('double'), a.astype('longdouble')) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: array cannot be safely cast to required type
Just for laughs, what happens if you reverse the order of the arguments? Type promotion in numpy is not always symmetric. <snip> Chuck
On 7/7/2010 9:59 PM, Charles R Harris wrote:
On Wed, Jul 7, 2010 at 10:13 PM, Christoph Gohlke <cgohlke@uci.edu <mailto:cgohlke@uci.edu>> wrote:
Dear NumPy developers,
I am trying to solve some scipy.sparse TypeError failures reported in [1] and reduced them to the following example:
> >> import numpy > >> a = numpy.array([[1]])
> >> numpy.dot(a.astype('single'), a.astype('longdouble')) array([[1.0]], dtype=float64)
> >> numpy.dot(a.astype('double'), a.astype('longdouble')) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: array cannot be safely cast to required type
Just for laughs, what happens if you reverse the order of the arguments? Type promotion in numpy is not always symmetric.
This works as expected:
numpy.dot(a.astype('longdouble'), a.astype('double')) array([[1.0]], dtype=float64)
-- Christoph
On 7/7/2010 9:13 PM, Christoph Gohlke wrote:
Dear NumPy developers,
I am trying to solve some scipy.sparse TypeError failures reported in [1] and reduced them to the following example:
import numpy a = numpy.array([[1]])
numpy.dot(a.astype('single'), a.astype('longdouble')) array([[1.0]], dtype=float64)
numpy.dot(a.astype('double'), a.astype('longdouble')) Traceback (most recent call last): File "<stdin>", line 1, in<module> TypeError: array cannot be safely cast to required type
Is this exception expected?
Also I noticed this:
numpy.array([1]).astype('longdouble').dtype.num 13 numpy.array([1.0]).astype('longdouble').dtype.num 12
I am using Python 2.6.5 for Windows and numpy 1.4.1 compiled with msvc9, where sizeof(longdouble) == sizeof(double).
[1] http://aspn.activestate.com/ASPN/Mail/Message/scipy-user/3875416
I submitted a patch and testcase at http://projects.scipy.org/numpy/ticket/1539. -- Christoph
participants (3)
-
Charles R Harris
-
Christoph Gohlke
-
David Cournapeau