What class should include "5 kilograms"?

Travis Griggs tgriggs at keyww.com
Thu Nov 12 16:34:57 UTC 1998


stan at stanheckman.com wrote:

> Here's what I found awkward: The coercion method has us cast numbers up
> to a more general number type, then do an operation between two
> objects of the general type. But in the course of that operation, I
> found that I sometimes had to check to see whether one of the objects
> was "really" of the less general type. (We don't want to raise
> anything to the power "3 meters".) So my first iteration of the code
> was turning Floats into DimensionedNumbers with no dimensions, then,
> because I could no longer tell from their type, asking them to iterate
> over their dimensions and check that they were all zero. Yuck.
>
> The current version (http://stanheckman.com/a/98h/glimpsedotter) uses
> a helper class DimensionlessNumber that lives at the same level of the
> coercion hierarchy and knows how to do arithmetic with dimensioned
> numbers. This is not so awful, but not as nice a fit with the coercion
> scheme as Integer, Fraction, and Float manage.

Stan,

Having done this before, and being a little ways out before I finish porting an
older units package to Squeak, let me put the following twist on things. Normal
numbers are also dimensioned numbers (or Measured Values as I will call them). In
the domain of measurement mathematics, if you divide 4 in by 4 in, you get 1 in/in.
What the hell kind of unit is in/in?!?!? Well, it's a valid, non-reduced unit. It
does happen to reduce to the "unitless" unit. But the unitless unit is just as well
expressed with kg*m3/kg*m3, which is quite united. The VERY FIRST unit type you
define should be the unitless one (unit unity :) ). Now if you need a Number to
behave like a Measured Value, just make it do so by sending unit to it. This allows
your number object to masquerade as an MV without having to coerce to a different
object type.

--
Travis Griggs
Key Technology
tgriggs at keyww.com
Member, Fraven Skreiggs Software Collective
 - C for Sinking
 - Java for Drinking
 - Smalltalk for Thinking





More information about the Squeak-dev mailing list