[SciPy-User] overflow

Sebastian Haase seb.haase at gmail.com
Sat Jul 10 17:43:07 EDT 2010


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.

-Sebastian Haase



More information about the SciPy-User mailing list