numpy numbers converted wrong

robert no-spam at no-spam-no-spam.com
Thu Oct 26 16:06:45 EDT 2006


in Gnuplot (Gnuplot.utils) the input array will be converted to a Numeric float array as shown below. When I insert a numpy array into Gnuplot like that below, numbers 7.44 are cast to 7.0 
Why is this and what should I do ? Is this bug in numpy or in Numeric?


[Dbg]>>> m         #numpy array
array([[  9.78109200e+08,   7.44000000e+00],
       [  9.78454800e+08,   7.44000000e+00],
       [  9.78541200e+08,   8.19000000e+00],
       ..., 
       [  1.16162280e+09,   8.14600000e+01],
       [  1.16170920e+09,   8.10500000e+01],
       [  1.16179560e+09,   8.16800000e+01]])
[Dbg]>>> Numeric.asarray(m, Numeric.Float32)[:10]
array([[ 9.78109184e+008,  7.00000000e+000],
       [ 9.78454784e+008,  7.00000000e+000],
       [ 9.78541184e+008,  8.00000000e+000],
       [ 9.78627584e+008,  8.00000000e+000],
       [ 9.78713984e+008,  8.00000000e+000],
       [ 9.78973184e+008,  8.00000000e+000],
       [ 9.79059584e+008,  8.00000000e+000],
       [ 9.79145984e+008,  8.00000000e+000],
       [ 9.79232384e+008,  9.00000000e+000],
       [ 9.79318784e+008,  8.00000000e+000]],'f')
[Dbg]>>> Numeric.asarray(m, Numeric.Float)[:10]
array([[ 9.78109200e+008,  7.00000000e+000],
       [ 9.78454800e+008,  7.00000000e+000],
       [ 9.78541200e+008,  8.00000000e+000],
       [ 9.78627600e+008,  8.00000000e+000],
       [ 9.78714000e+008,  8.00000000e+000],
       [ 9.78973200e+008,  8.00000000e+000],
       [ 9.79059600e+008,  8.00000000e+000],
       [ 9.79146000e+008,  8.00000000e+000],
       [ 9.79232400e+008,  9.00000000e+000],
       [ 9.79318800e+008,  8.00000000e+000]])
[Dbg]>>> 


and why and what is:

[Dbg]>>> m[0,1]
7.44
[Dbg]>>> type(_)
<type 'numpy.float64'>
[Dbg]>>> 


does this also slow down python math computations? 
should one better stay away from numpy in current stage of numpy development?
I remember, with numarray there were no such problems.

-robert


PS: in Gnuplot.utils:

def float_array(m):
    """Return the argument as a Numeric array of type at least 'Float32'.

    Leave 'Float64' unchanged, but upcast all other types to
    'Float32'.  Allow also for the possibility that the argument is a
    python native type that can be converted to a Numeric array using
    'Numeric.asarray()', but in that case don't worry about
    downcasting to single-precision float.

    """

    try:
        # Try Float32 (this will refuse to downcast)
        return Numeric.asarray(m, Numeric.Float32)
    except TypeError:
        # That failure might have been because the input array was
        # of a wider data type than Float32; try to convert to the
        # largest floating-point type available:
        return Numeric.asarray(m, Numeric.Float)



More information about the Python-list mailing list