[Numpy-discussion] float64 / int comparison different from float / int comparison
Matthew Brett
matthew.brett@gmail....
Tue Nov 1 19:55:36 CDT 2011
Hi,
On Tue, Nov 1, 2011 at 8:39 AM, Chris.Barker <Chris.Barker@noaa.gov> wrote:
> On 10/31/11 6:38 PM, Stéfan van der Walt wrote:
>> On Mon, Oct 31, 2011 at 6:25 PM, Matthew Brett<matthew.brett@gmail.com> wrote:
>>> Oh, dear, I'm suffering now:
>
>>> In [12]: res> 2**31-1
>>> Out[12]: array([False], dtype=bool)
>
>> I'm seeing:
> ...
>
>> Your result seems very strange, because the numpy scalars should
>> perform exactly the same inside and outside an array.
>
> I get what Stéfan gets:
>
> In [32]: res = np.array((2**31,), dtype=np.float32)
>
> In [33]: res > 2**31-1
> Out[33]: array([ True], dtype=bool)
>
> In [34]: res[0] > 2**31-1
> Out[34]: True
>
> In [35]: res[0].dtype
> Out[35]: dtype('float32')
>
>
> In [36]: np.__version__
> Out[36]: '1.6.1'
>
> (OS-X, Intel, Python2.7)
>
>
> Something is very odd with your build!
Well - numpy 1.4.1 on Debian squeeze. I get the same as you with
current numpy trunk.
Stefan and I explored the issue a bit further and concluded that, in
numpy trunk, the current behavior is explicable by upcasting to
float64 during the comparison:
In [86]: np.array(2**63, dtype=np.float) > 2**63 - 1
Out[86]: False
In [87]: np.array(2**31, dtype=np.float) > 2**31 - 1
Out[87]: True
because 2**31 and 2**31-1 are both exactly representable in float64,
but 2**31-1 is not exactly representable in float32.
Maybe this:
In [88]: np.promote_types('f4', int)
Out[88]: dtype('float64')
tells us this information. The command is not available for numpy 1.4.1.
I suppose it's possible that the upcasting rules were different in
1.4.1 and that is the cause of the different behavior.
Best,
Matthew
More information about the NumPy-Discussion
mailing list