[Numpy-discussion] help! type 'float64scalar' is not type 'float'

Robert Kern robert.kern at gmail.com
Wed Aug 2 23:43:44 CDT 2006


Sebastian Haase wrote:
> Hi!
> I just finished maybe a total of 5 hours tracking down a nasty bug.
> So I thought I would share this:
> I'm keeping a version of (old) SciPy's 'plt' module around.
> (I know about matplotlib - anyway - ...)
> I changed the code some time ago from Numeric to numarray - no problem.
> Now I switched to numpy ... and suddenly the zooming does not work 
> anymore: it always zooms to "full view".
> 
> Finally I traced the problem down to a utility function:
> "is_number" - it is simply implemented as
> def is_number(val):
>      return (type(val) in [type(0.0),type(0)])
> 
> As I said - now I finally saw that I always got
> False since the type of my number (0.025) is
>   <type 'float64scalar'>
> and that's neither <type 'float'> nor <type 'int'>
> 
> OK - how should this have been done right ?

It depends on how is_number() is actually used. Probably the best thing to do 
would be to take a step back and reorganize whatever is calling it to not 
require specific types.

Quick-and-dirty: use isinstance() instead since float64scalar inherits from 
float. However, float32scalar does not, so this is not a real solution, just a 
hack to get you on your merry way.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco





More information about the Numpy-discussion mailing list