Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-10-14 12:35:45


Deane Yang <deane_yang_at_[hidden]> writes:

> I did take a quick look, so I might have gotten the wrong idea. Please
> forgive me if I did. It appeared to me that your approach,
> using a list of integers, is limited to a preselected choice of
> dimensions.

No more than Hugo's.

> This works well in physics, where one knows in advance pretty much
> all dimensions that one is going to need.
>
> On the other hand, Hugo's approach would appear to allow for
> compile-time definition of dimensions:

Exactly as my approach does.

> "It defines
> a quantity as a numeric type and a set of dimensions
>
> template <typename T, typename Dimensions> class quantity;
>
> where dimensions contains a mpl type sequence,

Exactly as my approach does.

> with each
> element holding instances of
>
> template <
> typename DimensionType
> , long Numerator
> , long Denominator=1
> > struct dimension_power
>
> Each DimensionType is then assosciated with a unit."

Tagging things with a DimensionType in that way will cause O(N^2)
compile-time algorithms for computing the multiplication result,
unless you use an MPL associative container, which, IIUC are not
implemented yet. I posted a prototype a couple of weeks ago, in case
you care.

> The other quibble is that I also need fractional powers
> of dimensions.

The fractional power thing is strange, but OK. Anyway, it's
essentially the same approach as mine. What Hugo's explanation is
missing is any demonstration of how to compute the new Dimensions type
which results from multiplication or division.

> To be honest, I haven't looked at Hugo's explanation in detail
> yet, and given my poor grasp of mpl, I think it will take some
> effort.

That's why I suggest you look through the slides carefully. They
provide a gentle introduction to the principles and structure behind
the MPL.

> I don't even know how to add fractions and reduce to lowest
> terms in mpl yet. (Or maybe you don't try to reduce and you just test
> for equality of fractions when you multiply two quantities. That sounds
> a lot easier.)

Well, then you probably want arbitrary-precision compile-time
integers. That's a bit of work to implement.

> Through what little experience I have in programming, I've learned
> to hate using raw numbers like doubles in numerical computations in
> any circumstance, even if is a pure mathematical calculation. It is
> way too easy to get the "units" wrong (and, yes, the concept of
> "units" do sometimes exist even in pure mathematics) and real
> headache to debug. I would like to develop a units library that I
> can use under any circumstances with complete flexibility. I really,
> really like letting the compiler debug my code for me. I don't know
> if it saves me any time, but it sure saves wear and tear on my
> brain.

Sure. Some people love dynamic languages; the rest of us think that
static checks help us think about our code.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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