Boost logo

Boost :

From: Noah Roberts (roberts.noah_at_[hidden])
Date: 2007-01-19 19:19:52


Matthias Schabel wrote:

> // currency unit systems
> struct currency_system { };
>
> struct currency_tag : public ordinal<1> { };
>
> typedef dimension< boost::mpl::list< dim<
> currency_tag,static_rational<1> > > >::type currency_type;
>
> typedef unit<currency_system,currency_type> currency;
>
> class us_dollar
> { ... };
>
> class canadian_dollar
> { ... };
>
> quantity<currency,us_dollar> usd(us_dollar(date1));
> quantity<currency,canadian_dollar> cd(canadian_dollar(date2));
>
> This way the value_type takes care of all the conversions (say to and
> from constant US dollars fixed
> at a certain date or however you like) and all the units library does
> is keep track of the fact that the
> quantity is a currency. By doing this, everything is completely
> decoupled. If implicit conversion of
> currencies is supported, this is transparent through quantity...

Would something like this work for creating a quantity that's unit can
change during runtime and allow support for multiple units all
converting to a particular "system" (as you specify the meaning)?

For instance, I work on fluid flow analysis programs and have started
working on a unit library based on ch. 3 in the MPL book. One thing I
need is for user entry of arbitrary (and sometimes user defined) units.
  So an entry of pressure might be in psi or inches of mercury; it also
might be in gage (with some user defined reference atmospheric pressure)
or absolute. These values need to be stored in those units (otherwise
you run afoul of float migration) and are the ultimate entry data
parameters for all the system's equations. How does your library meet
this need?

Another issue we have is that equations are more often than not
referenced from some source written in arbitrary units. It would be
nice to be able to reflect this in code so that if some source says "*
9.32 psi a" this could be done in the code itself with minimal to 0
impact on runtime performance. Does your library meet this need?

I envision something akin to:

typedef quantity<SI::pressure> length_qty;

X f() { static length_qty const p9 = 9.32 * psi; }


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