"Strong typing vs. strong testing"
Thomas A. Russ
tar at sevak.isi.edu
Wed Sep 29 19:54:53 CEST 2010
George Neuner <gneuner2 at comcast.net> writes:
> On Tue, 28 Sep 2010 12:15:07 -0700, Keith Thompson <kst-u at mib.org>
> >He didn't say it was. Internal calculations are done in SI units (in
> >this case, m^3/sec); on output, the internal units can be converted to
> >whatever is convenient.
> That's true. But it is a situation where the conversion to SI units
> loses precision and therefore probably shouldn't be done.
I suppose that one has to choose between two fundamental designs for any
computational system of units. One can either store the results
internally in a canonical form, which generally means an internal
representation in SI units. Then all calculations are performed using
the interal units representation and conversion happens only on input or
Or one can store the values in their original input form, and perform
conversions on the fly during calculations. For calculations one will
still need to have some canonical representation for cases where the
result value doesn't have a preferred unit provided. For internal
calculations this will often be the case.
Now whether one will necessarily have a loss of precision depends on
whether the conversion factors are exact or approximations. As long as
the factors are exact, one can have the internal representation be exact
as well. One method would be to use something like the Commmon Lisp
rational numbers or the Gnu mp library.
And a representation where one preserves the "preferred" unit for
display purposes based on the original data as entered is also nice.
Roman Cunis' Common Lisp library does that, and with the use of rational
numbers for storing values and conversion factors allows one to do nice
things like make sure that
30mph * 3h = 90mi
even when the internal representation is in SI units (m/s, s, m).
Thomas A. Russ, USC/Information Sciences Institute
More information about the Python-list