[PYTHON MATRIX-SIG] Coercion problems solved?
Perry A. Stoll
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
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 ;):
def __init__(self, sequence):
UserArray.UserArray.__init__(self, sequence, typecode=Numeric.Float32)
return (self, UserArray.UserArray(other, typecode=Numeric.Float32))
These constants (defined in Precision.py) are mentioned in the docs
distributed with NumPy.
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: firstname.lastname@example.org
administrivia to: email@example.com