[Numpy-discussion] Bug in numpy.fix(): broken for scalar arguments

Darren Dale dsdale24 at gmail.com
Sun Apr 18 10:07:43 EDT 2010


On Sun, Apr 18, 2010 at 9:28 AM, Darren Dale <dsdale24 at gmail.com> wrote:
> On Sun, Apr 18, 2010 at 9:08 AM, Darren Dale <dsdale24 at gmail.com> wrote:
>> On Sat, Apr 17, 2010 at 4:16 PM, Charles R Harris
>> <charlesr.harris at gmail.com> wrote:
>>>
>>>
>>> On Sat, Apr 17, 2010 at 2:01 PM, Eric Firing <efiring at hawaii.edu> wrote:
>>>>
>>>> np.fix() no longer works for scalar arguments:
>>>>
>>>>
>>>> In [1]:import numpy as np
>>>>
>>>> In [2]:np.version.version
>>>> Out[2]:'2.0.0.dev8334'
>>>>
>>>> In [3]:np.fix(3.14)
>>>>
>>>> ---------------------------------------------------------------------------
>>>> TypeError                                 Traceback (most recent call
>>>> last)
>>>>
>>>> /home/efiring/<ipython console> in <module>()
>>>>
>>>> /usr/local/lib/python2.6/dist-packages/numpy/lib/ufunclike.pyc in fix(x,
>>>> y)
>>>>      46     if y is None:
>>>>      47         y = y1
>>>> ---> 48     y[...] = nx.where(x >= 0, y1, y2)
>>>>      49     return y
>>>>      50
>>>>
>>>> TypeError: 'numpy.float64' object does not support item assignment
>>>>
>>>>
>>>
>>> Looks like r8293. Darren?
>>
>> Thanks, I'm looking into it.
>
> The old np.fix behavior is different from np.floor and np.ceil.
> np.fix(3.14) would return array(3.0), while np.floor(3.14) would
> return 3.0. Shall I fix it to conform with the old but inconsistent
> behavior of fix?

I think this is the underlying issue: np.floor(np.array(3.14)) returns
3.0, not array(3.14). The current implementation of fix had already
taken care to ensure that it was working with an array for the input.
What is numpy's policy here? np.fix returned a len-0 ndarray even for
scalar input, floor and ceil return scalars even for len-0 ndarrays.
This inconsistency makes it difficult to make even small modifications
to the numpy codebase.

r8351 includes a one-line change that addresses Eric's report and is
commensurate with the previous behavior of fix.

Darren



More information about the NumPy-Discussion mailing list