[PYTHON MATRIX-SIG] Coercion problems solved?

Perry A. Stoll pas@xis.xerox.com
Mon, 10 Mar 1997 11:10:27 PST


On  8 Mar, Alan Watson wrote:

>     import UserArray
>     class array32(UserArray.UserArray):
>         def __init__(self, sequence): 
>             UserArray.UserArray.__init__(self, sequence, typecode='f')
>         def __coerce__(self,other): 
>             return (self, UserArray.UserArray(other, typecode='f'))
> 
> This allows me to implement my own `principle of least
> surprise' for coercion, namely that array elements are always 32-bit
> floats. 

Quick question: does anyone know a modern machine which doesn't have a
C float as 32 bits?

FWIW, the following small change guarantees that you will _always_ get a
32 bit float (provided one is available, of course ;):

    import UserArray,Numeric
    class array32(UserArray.UserArray):
        def __init__(self, sequence): 
            UserArray.UserArray.__init__(self, sequence, typecode=Numeric.Float32)
        def __coerce__(self,other): 
            return (self, UserArray.UserArray(other, typecode=Numeric.Float32))

These constants (defined in Precision.py) are mentioned in the docs
distributed with NumPy.

-Perry
 


_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________