![]() |
Boost : |
From: Steven Watanabe (steven_at_[hidden])
Date: 2007-02-19 14:14:11
AMDG
Noah Roberts <roberts.noah <at> gmail.com> writes:
> > Then why go to the trouble of providing
> > all the arithmetic functions?
>
> Because they are occasionally useful.
>
> dp = inlet - outlet;
>
> vs.
>
> dp = (inlet.quantity() - outlet.quantity()) * dp.unit();
Well, ok. I personally prefer the latter, But
I can see why some might not want to type more.
I would definitely want
dp = (inlet - outlet) * dp.unit();
>
> >> quantity f() { static quantity const C = 9.43 * psi; ... equation
> >> written with psi constant ... }
> >
> > I don't understand what is wrong with
> >
> > quantity<psi_type> f() {
> > static quantity<psi_type> const C = 9.43 * psi;
> > ... equation written with psi constant ...
> > }
>
> Well, if we are talking about in terms of the current proposal I believe
> that would result in a lot of conversions from psi to pascals (assuming
> si base). Because now C is in some psi system whereas the rest of the
> function uses the SI system. Incredibly inefficient.
How was I supposed to know that the rest of
the function was in SI? Even so,
quantity<SI::pressure> f() {
static quantity<SI::pressure> const C =
quantity_cast<SI::system>(9.43 * psi);
... equation written with psi constant ...
}
OR
quantity<psi_type> f() {
static quantity<SI::pressure> const C =
quantity_cast<SI::system>(9.43 * psi);
... equation written with psi constant ...
}
>
> >
> >>> quantity<pressure> p1(psi);
> >>> quantity<pressure> p2(pascals);
> >>> p1 = whatever;
> >>> p2 = p1;
> >>>
> >>> Now, what should the unit of p2 be?
> >> pascals. The rule is conversion on assignment. This does cause some
> >> need to be careful with such things as std containers that work through
> >> assignment.
> >>
> >
> > Right. I can't see a way to make a std container
> > that holds quantities of different units safe at all.
>
> Most commonly you would not want them to convert to a different unit on
> assignment. You would want the elements in your container to stay a
> certain unit while converting whatever value is being assigned to that
> unit. When that is not what is wanted a wrapper is needed.
>
typedef runtime_quantity<SI::length> length_t;
std::vector<runtime_quantity<length_t> > v;
//fill v
v.insert(v.begin(), ...);
//the units of the elements are now unspecified.
A type that is almost a value type but isn't
quite is asking for trouble.
In Christ,
Steven Watanabe
Boost list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk