squeak.org
[*]  Links

[*]  Syntax Coloring

[*]  Cream Font

[*]  Units

[*]  XML Parser

[*]  PalmOS® tools

Andrew Brault's Units Package for Squeak 2.8/2.9a

Code updated 2000-10-03 (for Squeak 2.9).

The Units package allows you to represent, convert, and perform arithmetic with physical unit values (e.g. distances, velocities, accelerations, etc).

There 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.

To 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.

Note that you can send  Unit printAbbreviated: true  or  Unit printAbbreviated: false  to control how units print.

Download

Download File Size
Change set Units.3.cs 51 KB
Examples as seen below Units.Examples 2 KB
All of the above, zipped Units.3.zip 12 KB

License

The Dolphin package was licensed under a giftware license that can be viewed here. For the Squeak port of the package, Andrew kindly gave permission to change the license to the Squeak license. The change set now reflects this.

A copy of his message is provided below:

Date: Thu, 4 Mar 1999 13:29:30 -0500 (EST)
From: Andrew Brault <ajb@...>
To: Helge Horch <Helge.Horch@...>
Subject: Re: [ANN] Units for Squeak

On Thu, 4 Mar 1999, Helge Horch wrote:
> Folks,
>
> I have finished porting Andrew Braults Units package for Squeak. The
> current version is available at

Very cool!  Thanks!
BTW you have my permission to remove the "giftware"
notice from the Squeak port and add whatever licensing
terms are appropriate for Squeak.

-ajb

Examples

Here is some example code (results in italics):
'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

"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) seconds

System Modification Details

I have converted Andrew's code from Dolphin .pac format to a Squeak changeset, and adapted the mixed arithmetic code to Squeak's coercion mechanism (#adaptToXXX:andSend:). The ArithmeticValue class is gone (UnitValue now sits under Magnitude), as is the InconsistentUnitsError class (I'm sending #error: directly).

All faults and bugs are probably mine, so I highly welcome feedback and bug reports. The changeset is provided as-is, use this software at your own risk. No warranty of fitness for any purpose is claimed, implicitly or otherwise. There are a few minor changes to the base classes:

  • both SmallInteger>>/ and Float>>/ now send #isZero instead of = 0
  • Number>>units: is used to make UnitValues
The code should also file in into earlier Squeak releases, provided that one
  • replaces #anySatisfy: with #contains:
  • adds Number>>isZero back
  • replaces the exception references in SmallInteger>>/ and Float>>/ with self error: 'division by zero'
as things were in the original port to Squeak 2.3b.


Copyright © 1999-2000 by Helge Horch. All rights reserved.
LAST AWK RUN: hh 03oct00, contact: heho@gmx.de