[Numpy-discussion] CASTABLE flag

Timothy Hochberg tim.hochberg@ieee....
Mon Jan 7 17:34:05 CST 2008


On Jan 7, 2008 2:00 PM, Charles R Harris <charlesr.harris@gmail.com> wrote:

> Hi,
>
> On Jan 7, 2008 1:16 PM, Timothy Hochberg <tim.hochberg@ieee.org> wrote:
>
> >
> > Another possible approach is to treat downcasting similar to underflow.
> > That is give it it's own flag in the errstate and people can set it to
> > ignore, warn or raise on downcasting as desired. One could potentially have
> > two flags, one for downcasting across kinds (float->int, int->bool) and one
> > for downcasting within kinds (float64->float32). In this case, I personally
> > would set the first to raise and the second to ignore and would suggest that
> > as the default.
> >
> > IMO:
> >
> >    1. It's a no brainer to raise and exception when assigning a
> >    complex value to a float or integer target. Using "Z.real" or "
> >    Z.imag" is clearer and has the same performance.
> >    2. I'm fairly dubious about assigning float to ints as is. First
> >    off it looks like a bug magnet to me due to accidentally assigning a
> >    floating point value to a target that one believes to be float but is in
> >    fact integer. Second, C-style rounding is pretty evil; it's not always
> >    consistent across platforms, so relying on it for anything other than
> >    truncating already integral values is asking for trouble.
> >    3. Downcasting within kinds seems much less hazardous than
> >    downcasting across kinds, although I'd still be happy to be able regulate it
> >    with errstate.
> >
> > Maybe a combination of a typecast function and the errstate would work
> well. The typecast function would provide a clear local override of the
> default errstate flags, while the user would have the option to specify what
> sort of behavior they care about in general.
>

Note that using the 'with' statement, you can have reasonably lightweight
local control using errstate. For example, assuming the hypothetical
dowcasting flag was named 'downcast':

 with errstate(downcast='ignore'):
      anint[:] = afloat

That would be local enough for me although it may not be to everyone's
taste.

If we were to go the "a[:] = typecast(b)" route, I have a hankering for some
more fine grained control of the rounding. Something like "a[:] =
lazy_floor(b)", "a[:] = lazy_truncate(b)" or "a[:] = lazy_ceil(b)", only
with better names. However, I admit that it's not obvious how to implement
that.

Yet another approach, is to add an 'out' argument to asstype such as already
exists for many of the other methods. Then "a.astype(int, out=b)", will
efficiently stick an integerized version of a into b.

FWIW, I suspect that the usefulness of casting in avoiding of temporaries is
probably overstated. If you really want to avoid temporaries, you either
need to program in a very tedious fashion, suitable only for very localized
hot spots, or you need to use something like numexpr. If someone comes back
with some measurements from real code that show a big time hit, I'll concede
the case, but so far it all sounds like guessing and my guess is that it'll
rarely make a difference. (And, the cases where it does make a difference
will be places your already doing crazy things to optimize the snot out of
the code and an extra astype or such won't matter)

-- 
.  __
.   |-\
.
.  tim.hochberg@ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080107/8bd68429/attachment.html 


More information about the Numpy-discussion mailing list