[Numpy-discussion] Possible bug: uint64 + int gives float64

2010-06-13 Thread Pearu Peterson
Hi,
I just noticed some weird behavior in operations with uint64 and int,
heres an example:

 numpy.uint64(3)+1
4.0
 type(numpy.uint64(3)+1)
type 'numpy.float64'

Pearu
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Possible bug: uint64 + int gives float64

2010-06-13 Thread Nadav Horesh
int can be larger than numpy.int64 therefore it should be coerced to float64 
(or float96/float128)

  Nadav

-Original Message-
From: numpy-discussion-boun...@scipy.org on behalf of Pearu Peterson
Sent: Sun 13-Jun-10 12:08
To: Discussion of Numerical Python
Subject: [Numpy-discussion] Possible bug: uint64 + int gives float64
 
Hi,
I just noticed some weird behavior in operations with uint64 and int,
heres an example:

 numpy.uint64(3)+1
4.0
 type(numpy.uint64(3)+1)
type 'numpy.float64'

Pearu
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

winmail.dat___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Possible bug: uint64 + int gives float64

2010-06-13 Thread Pearu Peterson
On Sun, Jun 13, 2010 at 4:45 PM, Nadav Horesh nad...@visionsense.com wrote:
 int can be larger than numpy.int64 therefore it should be coerced to float64 
 (or float96/float128)

Ok, I see. The results type is defined by the types of operands, not
by their values. I guess
this has been discussed earlier but with small operands this feature
may be unexpected.
For example, with the same rule the result of int64 + int should be
float64 while currently
it is int64.

Pearu
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Possible bug: uint64 + int gives float64

2010-06-13 Thread Charles R Harris
On Sun, Jun 13, 2010 at 9:20 AM, Pearu Peterson pearu.peter...@gmail.comwrote:

 On Sun, Jun 13, 2010 at 4:45 PM, Nadav Horesh nad...@visionsense.com
 wrote:
  int can be larger than numpy.int64 therefore it should be coerced to
 float64 (or float96/float128)

 Ok, I see. The results type is defined by the types of operands, not
 by their values. I guess
 this has been discussed earlier but with small operands this feature
 may be unexpected.
 For example, with the same rule the result of int64 + int should be
 float64 while currently
 it is int64.


It's the combination of unsigned with signed that causes the promotion. The
int64 type can't hold the largest values in uint64. Strictly speaking,
doubles can't hold either of the 64 bit integer types without loss of
precision but at least the degradation is more gradual. Another possibility
here would be to raise an error/warning because no suitable integer type is
available, that would be perhaps less surprising than the promotion to
float. IIRC, there is also a case where numeric arrays get promoted to
objects, but I don't see my old table of such things floating about at the
moment.

Chuck
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Possible bug: uint64 + int gives float64

2010-06-13 Thread Sturla Molden
Den 13.06.2010 18:19, skrev Charles R Harris:

 It's the combination of unsigned with signed that causes the 
 promotion. The int64 type can't hold the largest values in uint64. 
 Strictly speaking, doubles can't hold either of the 64 bit integer 
 types without loss of precision but at least the degradation is more 
 gradual. Another possibility here would be to raise an error/warning 
 because no suitable integer type is available, that would be perhaps 
 less surprising than the promotion to float.

As I see it, the 'correct' solution would be coercion to an arbitrarily 
long integer, such as 'long' in Python 2 and 'int' in Python 3.

Sturla

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion