Units

Last updated at 10:48 am UTC on 11 November 2006

From Stefan Matthias Aust on March 08, 2004 registered Helge Horch's port of Andrew Brault Units package on SqueakMap.The Units package allows you to represent, convert, and perform arithmetic with physical unit values (e.g. distances, velocities, accelerations, etc). Here is some example code:

"Create unit values by sending #units: to a number." 2 units: #inches. ==> 2 inches "You can add, divide, multiply, and subtract unit values." (2 units: #inches) / (3 units: #seconds). ==> (2/3) inches per second "Adding or subtracting units does appropriate conversions." (14 units: #feet) + (10 units: #meters). ==> 46.8083989501312 feet "You can also explicitly convert values." (15 units: #miles) / (1 units: #hours) convertTo: (Unit meters / Unit seconds). ==> 6.7056 meters per second "You can use 'derived' units such as the newton." 3 units: #newtons. ==> 3 newtons "You can expand such a value into base SI units." (3 units: #newtons) baseUnits. ==> 3000 gram meters per square second "To see kilograms rather than grams, factor with respect to kilograms." (3 units: #newtons) factor: Unit kilograms. ==> 3 kilogram meters per square second "Unit values can also be compared (provided they are dimensionally consistent." (1 units: #inches) < (3 units: #centimeters). ==> true "If you would rather have abbreviations, you can send" Unit printAbbreviated: trueTo add your own units, see the classes BaseUnit, DerivedUnit, NamedUnit, and PrefixedUnit, and look through the initialization code. You should duplicate what is done there to add your own units. Remember to send Unit initialize to make your changes take effect.

"Creating additional units is easy:" | microfortnight | microfortnight := PrefixedUnit prefixName: 'micro' unit: (DerivedUnit abbreviation: 'FN' name: 'fortnight' pluralName: 'fortnights' value: (14 units: #days)). (1 units: microfortnight) baseUnits "value asFloat" ==> (756/625) secondsThere is not much formal documentation, but most of the classes in this package have comments. The most important methods are those in Unit and UnitValue. You should look through all those.