[Numpy-discussion] A couple more Numeric incompatibilitiesand a possible bug

David M. Cooke cookedm at physics.mcmaster.ca
Tue Mar 30 12:42:21 EST 2004

On Mon, Mar 29, 2004 at 06:55:57AM -0500, Todd Miller wrote:
> On Sun, 2004-03-28 at 22:46, Gary Ruben wrote:
> > > > >>> cos(1)
> > > > 0.54030227661132813
> > > > 
> > > > gives a different result to cos(1.)
> > <snip>
> > > >  Do others think precision is being lost unnecessarily?
> > > 
> > > No.  Do you have any suggestions?
> > 
> > Yes. My problem is that numarray is not replicating the behaviour of
> > the Python math module, which presumably coerces immediately to the
> > float precision of the underlying platform. This is probably a
> > float64 in Windows. If you don't explicitly specify that float32 is
> > to be used, shouldn't the default be for a rank-0 value to
> > immediately coerce ints to the same precision as the native Python
> > float type on the underlying platform, since you know it will be
> > coerced to that later and in the meantime you've lost precision
> > because you've applied a function to a value of lower precision?
> > 
> I see your point and I'll talk it over with Perry unless he chimes in on
> his own.  I'm ambivalent:  while it would be nice for the numarray
> ufuncs to act as full equivalents to the Python math functions in scalar
> cases,  numarray has to strike a balance between managing storage space
> and maintaining precision and these two goals are in conflict.  I think
> using an array package you have to be at least a little more aware of
> storage concerns.  

I'll throw in a vote for 'cos(1)' coercing 1 to a (equivalent-to-)
Python float. The reason is that I have 'from numarray import *' for my
interactive interpreter, as I use numarray a lot in that.
I would rather not have to remember to always cast my args to ufuncs to
arrays. This isn't a storage space problem, per se., the question is
whether cos(1) and cos(1.0) should return the same answer.

Having cos(1) cast 1 to a Float32 makes numarray much less useful as a
calculator -- mistakes are more likely. I'll probably switch my
interpreter back to Numeric so as not to make mistakes, or I'll write a
wrapper (which I'll post if I do).

Whether cos([1,1,1]) and cos([1.0,1.0,1.0]) are the same I'd say is
different, as there I've taken the conscious decision not to cast to an
array. (And the fact that cos([1,1,1]) will return an array printed out
with 'type=Float32' is a clue to what I've done.)

|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca

More information about the NumPy-Discussion mailing list