Boost logo

Boost :

From: Janek Kozicki (janek_listy_at_[hidden])
Date: 2006-08-29 14:47:05


Ben Strasser said: (by the date of Tue, 29 Aug 2006 16:23:30 +0000 (UTC))

> The simplest situation would be to have a non unicode unit postfix. So the ideal
> interface would look similar to
>
> typedef unit<length, "m"> meter;

maybe even something like this:

  typedef unit<length, "m" , 10, 0, 1 > meter;
  typedef unit<length, "in", 10,-2, 2.54 > inch;

additional arguments are: power base, power exponent, conversion mutiplier, so we have:
  10^0 * 1 = 1 meter
  10^-2 * 2.54 = 1 inch

With this *user defined* units we have two very nice advantages:

1. we do not force anybody to use any of the predefined unit systems, he
   can define any system he wants. Imperial people can have as well (total
   equivalent of above example):

      typedef unit<length, "m" , 10, 1, 3.937 > meter;
      typedef unit<length, "in", 10, 0, 1 > inch;

2. selecting power base makes it easy to work with units that operate
   with other bases, like 2^n, for example to measure storage capacity of
   a harddrive in MB. (lack of this ability was one of reasons to reject
   Quan's submission to boost (at that time under name 'pqs')).

Also I fully agree that the only IO here is just operator<<, just like
we have it provided for all fundamental types:

int main()
{
  typedef unit<length, "m" , 10, 0, 1 > meter;
  meter l(10);
  cout << "length: " << l << endl; // length: 10 m
}

And nothing more.

operator<< uses string given as a unit name in the typedef, and that's
all. No fancy creating of other strings that name the unit. At least,
when you are going to have a variable that holds torque, you had first
to make a typedef for that, right? And in that typedef there is a string
with unit name already. That strips the library of hundreths
(thousands?) of lines that try to support IO, try to create unit name
strings from their dimensions, run into various problems what exponent
to use in that name, etc, etc...

Much simpler design, and much easier to use.

need to use kilometers or feets? Then you also define it (Imperial example ;)

      typedef unit<length, "km", 10, 3, 3.937 > kilometer;
      typedef unit<length, "f" , 10, 1, 1.2 > feet;

see? All the burden of exponent conversions are removed from the library
(strips away hundreths of lines), and the user feels exactly whats going
on. When someone need to convert from meters to feets he does just that:

      meter l(10);
      feet f;
      /*....*/
      f=l;

alternatively:

      feet f(l);

Note: imperial notation is just an example here, it can be substituted
for any unit system used in the world: astronomers operate in parsecs
and AU (try storing parsecs in memory expressed in meters (an SI
unit!)), physicists operate with time=space and speed of light=1
[unitless], etc....

-- 
Janek Kozicki                                                         |

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk