[Numpy-discussion] Is this a bug, and if so, who's?

Travis Oliphant oliphant at enthought.com
Thu Apr 22 11:09:06 EDT 2010


On Apr 21, 2010, at 10:47 AM, Ken Basye wrote:

> Folks,
>   Apologies for asking here, but I ran across this problem yesterday
> and probably need to file a bug.  The problem is I don't know if  
> this is
> a Numpy bug, a Python bug, or both.  Here's an illustration, platform
> information follows.

It's a bug in your implementation of class A.

The __float__ method is supposed to return a Python float.    In A,  
you are just returning the 'y' field which is initialized to whatever  
you passed in (which in the last example is an np.float64.   An  
np.float64 is a subclass of Python's float, but it is not Python's  
float.

So, the explicit conversion is proper.


-Travis


>   TIA,
>       Ken
>
>
> #############################################
> import collections
> import numpy as np
>
> class A (collections.namedtuple('ANT', ('x', 'y'))):
>    def __float__(self):
>        return self.y
>
> # Same as A, but explicitly convert y to a float in __float__()  -  
> this
> works around the assert fail
> class B (collections.namedtuple('BNT', ('x', 'y'))):
>    def __float__(self):
>        return float(self.y)
>
> a0 = A(1.0, 2.0)
> f0 = np.float64(a0)
> print f0
>
> a1 = A(float(1.0), float(2.0))
> f1 = np.float64(a1)
> print f1
>
> b1 = B(np.float64(1.0), np.float64(2.0))
> f2 = np.float64(b1)
> print f2
>
> a2 = A(np.float64(1.0), np.float64(2.0))
> # On some platforms, the next line will trigger an
> assert:
>
> # python: Objects/floatobject.c:1674: float_subtype_new: Assertion
> `((((PyObject*)(tmp))->ob_type) == &PyFloat_Type)' failed.
> f3 = np.float64(a2)
> print f3
> #############################################
>
> Platform info:
>
> Python 2.6.5 (r265:79063, Apr 14 2010, 13:32:56)
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
>
>>>> numpy.__version__
> '1.3.0'
>
> ~--$ uname -srvmpio
> Linux 2.6.18-164.el5 #1 SMP Thu Sep 3 03:28:30 EDT 2009 x86_64 x86_64
> x86_64 GNU/Linux
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

--
Travis Oliphant
Enthought Inc.
1-512-536-1057
http://www.enthought.com
oliphant at enthought.com








More information about the NumPy-Discussion mailing list