[Matrix-SIG] Numeric Nits--a slightly different proposal

Michael Haggerty mhagger@blizzard.harvard.edu
18 Jul 1999 00:31:36 -0000


> >An expression involving two incompatible
> >forced objects would raise an exception.
> 
> I can't agree with that, though.  I'm afraid that will result
> in more ugly, hard-to-read code with lots of explicit casts.
> I believe that an operation between two forced objects should
> obey the normal promotion rules, with the lower forced type
> being promoted to the higher forced type.

The reason I would disallow operations between incompatible forced
objects is the principle of least surprise.  ``Forcing'' an array
should be a guarantee to the user that whatever happens to the data in
the array (short of an explicit cast), the result will be of the
requested type.  An operation between incompatible forced arrays
cannot satisfy that guarantee, and therefore should be disallowed.

Forced arrays will probably be used primarily for huge arrays such as
image files.  There will only be a few around at a time and they are
likely to be of the same type anyway, so I don't expect many explicit
casts to be necessary even for strict forced arrays.  Meanwhile the
impact of implicit casts of such arrays is by assumption large and
therefore *should* have to be thought about by the programmer.
Imagine a subroutine to which both your gigantic ForcedInt8 array and
a small ForcedFloat64 are accidentally passed; they get combined
somehow and boom you have a gigantic ForcedFloat64 array that doesn't
fit into memory.  In a case like that it would be helpful to have an
exception thrown so that the user recognizes the problem and can
easily track it down and correct it.

Craig Schardt <craig@yttria.mse.ufl.edu> says:
> I suggest that the Forced be the reverse of the normal hierarchy:
>
>          ForcedFloat64 -> ForcedFloat32 -> ForcedInt32 -> ForcedInt16

Yes, I can see the rationale for this idea.  But as you admit, this
promotion order would be surprising.  The most important virtue of
``strict forcing'' is that it is unsurprising and easy to explain.

By the way, there is a way to break the ``result type equals forced
type'' guarantee: reduce the array to a scalar, which would be
converted to a python scalar, whereupon the `forced' type attribute
would be lost.  But I think this is of only academic interest since a
forced scalar is not very useful.

Michael

--
Michael Haggerty
mhagger@blizzard.harvard.edu