|
Boost Users : |
Subject: Re: [Boost-users] [units] Missing quantity operator++, operator%
From: Daryle Walker (darylew_at_[hidden])
Date: 2012-06-07 03:25:50
> Date: Wed, 6 Jun 2012 21:33:14 -0600
> From: Nathan Crookston
> I'm in the process of switching my homegrown units system over to
> boost units. Some operators which I'm missing are the modulus and the
> pre and post increment operators.
>
> Looking through the archives I note an original review[1] which
> mentioned the absence of those operators. One response claimed that
> quantity<si::length, int> t = 5. ...; t++; was nonsensical as writing
> 5 m + 1, and the reviewer eventually agreed, citing possible confusion
> if the quantity type converted the initializer. Matthias Schabel
> seemed to respond favorably:
>
> 'I debated this for a while - the only problem is that there will be
> some value types with heterogeneous operator+ / operator- for which q
> + 1 is not the same type as q, but I guess that it is just OK for it
> to just give a compile error in those cases.'
If you look at "++" and "--" as "x + 1" and "x - 1", then they don't makesense. Put if you look at them as successor and predecessor, then theywould make sense for any type where such ordering can be established.So, the operators should be supported, but call the inner type's versionof those operators instead of causing a compile error (unless the innertype doesn't have those operators).
We already have an example: built-in (data) pointers. Using "++" (or "--")goes to the next (previous) element in the array segment.
> And Steven Watanabe said 'Will do.'>
> I haven't found anything regarding the modulus operator. My question
> is, would a patch adding those operators be accepted?
Hopefully.
For any type that defines addition as an associative T x T -> T mappingfunction, then we can define multiplication with a non-negative integerscalar as:
n * t == t * n == t + t + ... + t {n times}.
With this definition, we can define integral-division as:
If there's an integer M such that M * divisor == dividend, then return M. Otherwise find two integers K and L where |K - L| == 1 such that K * divisor < dividend and L * divisor > dividend, then choose one of K or L as your answer. (Which one is a standard you have to establish.)
and its related modulus operation as:
If N is the quotient from the integral-division above, then the remainder is the value such that dividend == N * divisor + remainder.
(I'm assuming that addition is also commutative.) Note that quotients areunit-less scalars, while remainders have the same units as the main type(used for the divisor and dividend).
> [1] http://lists.boost.org/Archives/boost/2007/03/119001.php
Daryle W.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net