
Boost : 
From: Andy Little (andy_at_[hidden])
Date: 20040109 01:13:16
"Reece Dunn" <msclrhd_at_[hidden]> wrote in message
news:BAY7F563YFTpozl4wV00015a01_at_hotmail.com...
> Andy Little wrote:
>
> >Documentation and link to a download, for my lib can be found here. :
> >http://www.servocomm.freeserve.co.uk/Cpp/physical_quantity/index.html
>
> Cool. I'll look into the library.
>
> As far as I can see, there are several areas that a quantities library
must
> deal with. The basic concepts are:
[ some definitions ]
Some of the terms above may be similar to mine:
http://www.servocomm.freeserve.co.uk/Cpp/physical_quantity/PhysicalQuantities1.html
> Prefixes  A set of powers that express the size magnitude of the value
to
> a base unit (e.g. micro; milli; tera; giga for base 10 and bit; byte;
kibi;
> tebi for binary). NOTE: these could (and should) be expressed within a
> constants framework.
I represent these in my type (variable) by a number(template param),
which represents the power of 10, in the SI system.
The technique could be applied in hex or binary too.
e.g.
m > 0 0 = log10(1)
km > 3 3 = log10(1000)
mm > 3 3 = log10(.001)
The advantage of this is that a lot of maths can be performed at compile
time.
e.g.
10 mm * 10 km > 100 m2 // ie 100 square meters
In this example the only Runtime multiplication is 10 * 10.
The return type of the calculation is in metres.
The value of the unit of the return type is arrived at
by adding the logs of the units at compile time:
Using the values of the powers from the above example:
3 + 3 = 0
This approach is quite powerful, where a sequential calculation is
performed:
A a;
B b;
C c;
D d;
E e;
a = b * (c / d) * e;
note: the units of 'a' may not have the same units as the temporary rvalue,
but where the units differ only 1 scaling is required for the entire calc.
This has been found to increase both speed and accuracy of calculations
In a templated function a temporary may be required which does not require
scaling.
using the metaprogramming paradigm with the example above:
template <typename A, typename B, typename C, typename D, typename E>
return_type func ()
{
typedef typename pq_divide< C, D>::type div_cd;
typedef typename pq_multiply<B,div_cd>:: type mux_b_cd;
typename pq_multiply<mux_b_cd, E>::type a
= ( b * (c / d) ) * e; // brackets help clarity
...
return return_type(result);
}
Here only the internal values are used in calculation and init of a.
There is no runtime scaling at all.
[ examples ]
> The units system should allow something like:
> si::speed s = 7 * si::m / si::s;
> si::acceleration g = 9.2 * si::m / si::s_2; // si::s_2 = si::s * si::s
I have Severe Problems with the above syntax.
In my syntax I would declare a vector of accelerations in m.s2 like this:
std::vector<q_acceleration::m_divs2> vect(1000);
The type above is compiletime complete and can therefore be used as a
template argument,
There may be an equivalent declaration method using your syntax,
but I would like someone to show me how the above declaration should be
done, using it.
regards
Andy Little
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk