[SciPy-User] overflow

Charles R Harris charlesr.harris at gmail.com
Sat Jul 10 18:10:45 EDT 2010


On Sat, Jul 10, 2010 at 3:43 PM, Sebastian Haase <seb.haase at gmail.com>wrote:

> On Sat, Jul 10, 2010 at 11:28 PM, Sturla Molden <sturla at molden.no> wrote:
> > Mr Nilsson skrev:
> >> Is there a way to have numpy clip the values to the maximum value of
> >> whatever dtype is used instead of overflowing?
> >>
> > Subclass the scalar np.uint8, and change how multiplication works to
> > something like this:
> >
> > inline npy_uint8 multiply(npy_uint8 a, npy_uint8 b)
> > {
> >    npy_unit32 tmp = b;
> >    tmp *= a;
> >    return (npy_uint8)((tmp >> 2) ? 0xFF : tmp);
> > }
> >
> This does not sound like an easy solution ...
> First: How to sub-class a basic thing like this  .... this looks like
> it's happening on the C level ... !?
> Second: there are probably quite a lot of functions to take care of ...
>
> Could numexpr be helpful for this - if memory is an issue ?
> If memory is no issue, it probably the easiest solution to just
> (explicitly) convert to float32 and only as last step convert back to
> uint8.
>
>
That's what I would recommend as the minimal work approach if memory isn't
an issue.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20100710/6ba6bc00/attachment.html>


More information about the SciPy-User mailing list