# [Numpy-discussion] fixed-point arithmetic

David Cournapeau cournape@gmail....
Mon Sep 21 18:11:20 CDT 2009

```On Tue, Sep 22, 2009 at 12:57 AM, Neal Becker <ndbecker2@gmail.com> wrote:
> David Cournapeau wrote:
>
>> On Mon, Sep 21, 2009 at 9:00 PM, Neal Becker <ndbecker2@gmail.com> wrote:
>>>
>>> numpy arrays of fpi should support all numeric operations.  Also mixed
>>> fpi/integer operations.
>>>
>>> I'm not sure how to go about implementing this.  At first, I was thinking
>>> to just subclass numpy array.  But, I don't think this provides fpi
>>> scalars, and their associated operations.
>>
>> Using dtype seems more straightforward. I would first try to see how
>> far you could go using a pure python object as a dtype. For example
>> (on python 2.6):
>>
>> from decimal import Decimal
>> import numpy as np
>> a = np.array([1, 2, 3], Decimal)
>> b = np.array([2, 3, 4], Decimal)
>> a + b
>>
>> works as expected. A lot of things won't work (e.g. most transcendent
>> functions, which would require a specific implementation anyway), but
>> arithmetic, etc... would work.
>>
>> Then, you could think about implementing the class in cython. If speed
>> is an issue, then implementing your own dtype seems the way to go - I
>> don't know exactly what kind of speed increase you could hope from
>> going the object -> dtype, though.
>>
>
> We don't want to create arrays of fixed-pt objects.  That would be very
> wasteful.

Maybe, but that would be a good way to prototype the thing.

>  What I have in mind is that integer_bits, frac_bits are
> attributes of the entire arrays, not the individual elements.  The array
> elements are just plain integers.

That's not really how numpy arrays are designed: type-specific info
should be in the dtype, not the array class. As Robert mentioned, the
recently added datetime dtype shows an example on how to do it.

David
```

More information about the NumPy-Discussion mailing list