Boost logo

Boost :

From: Ross Smith (ross.s_at_[hidden])
Date: 2001-10-01 15:54:04

Dean Foster wrote:
> A units class is described that will have a single integer that
> represents a "hash" of the unit involved. If two units (say foot and
> lbs) are multiplied a hash of the product (foot_lbs) is created to act
> as a signature of this new unit. If two units are added and their
> hashes don't agree--the system complains at compile time. Type safety
> is thus 99.999% assured. (there is a chance that two complex units
> would have the same hash value and so mistakenly look the same.) The
> hash function has the property that any way of generating foot_lbs
> from other units will generate the same computed hash.

What about assigning a different prime number to each dimension, and
representing multiplication of units as multiplication of the associated
primes? This guarantees a unique number for every combination of units.
It also solves the decomposition problem (extracting the basic units and
their exponents from a composite unit), which I suspect may be a
showstopper for your addition-of-hash-values approach.

(Actually the numbers would have to be rationals rather than integers to
allow for negative powers, but compile-time rational arithmetic
shouldn't be too difficult to implement. We already have a compile-time
GCD algorithm.)

The only serious problem I can see is running out of range in a 32-bit
integer when we want large powers of multiple units. Hmmm ...
compile-time unlimited-precision arithmetic, anyone? :-)

Ross Smith .......................................... ross.s_at_[hidden]
Ihug (Auckland, New Zealand) ...................
           Vs lbh pna ernq guvf, lbh'er ivbyngvat gur QZPN

Boost list run by bdawes at, gregod at, cpdaniel at, john at