[posix_time] time_duration operator/() overload

Hello, I use boost::posix_time a lot in my code and I really do appreciate the library. But there's one thing I was recently missing when comparing time_durations to each other. double time_duration::operator/(time_duration); From my point of view this is a perfectly valid operation (as opposed to operator*(time_duration)) that returns the proportion of the two durations. I need that operation to compute statistics like "n% of the time I did x" or similar. What I'm currently doing is computing the proportions via time_duration::total_*() which works fine, but leads to some coding effort, since I'll have to cast one result to a real number to avoid integer division and some checks to prevent /0. I assume I'm not the first to compare time_durations so I was wondering if there was a reason operator/() is not overloaded for time_duration? Or if there is generally a better way to compare two durations? thanks, Norbert

What I'm currently doing is computing the proportions via
time_duration::total_*()
which works fine, but leads to some coding effort, since I'll have to cast one result to a real number to avoid integer division and some checks to prevent /0.
Well, you can do all this work once - in the overloaded operator :). Note that you also have to handle special cases like pos_infinity, neg_infinity, and some interesting situations like pos_infinity/pos_infinity, pos_infinity/neg_infinity etc.

On 03/05/2010 01:05 PM, Igor R wrote:
What I'm currently doing is computing the proportions via
time_duration::total_*()
which works fine, but leads to some coding effort, since I'll have to cast one result to a real number to avoid integer division and some checks to prevent /0.
Well, you can do all this work once - in the overloaded operator :).
I was just wondering, if this overload was left out on purpose. From your answer I'd guess it was no purpose, so I'll write this one myself the next time I'll need it. thanks, Norbert

----- Original Message -----
From: "Norbert Wenzel"
Hello,
I use boost::posix_time a lot in my code and I really do appreciate the library. But there's one thing I was recently missing when comparing time_durations to each other.
double time_duration::operator/(time_duration);
From my point of view this is a perfectly valid operation (as opposed to operator*(time_duration)) that returns the proportion of the two durations. I need that operation to compute statistics like "n% of the time I did x" or similar.
What I'm currently doing is computing the proportions via
time_duration::total_*()
which works fine, but leads to some coding effort, since I'll have to cast one result to a real number to avoid integer division and some checks to prevent /0.
I assume I'm not the first to compare time_durations so I was wondering if there was a reason operator/() is not overloaded for time_duration? Or if there is generally a better way to compare two durations?
Maybe you are interested by the library on the review schedule Boost.Chrono. The library can be downloaded from http://www.boostpro.com/vault/index.php?action=downloadfile&filename=chrono.zip&directory=System& and the documentation is online on the sandbox http://svn.boost.org/svn/boost/sandbox/chrono/libs/chrono/doc/html/index.htm.... The library provides the operator/() you are looking for, but on a generic duration type. Please let me know if this responds to your needs or whatever is missing from your point of view. Best, Vicente

On Wed, Mar 31, 2010 at 11:16 PM, vicente.botet
----- Original Message ----- From: "Norbert Wenzel"
To: Sent: Friday, March 05, 2010 9:36 AM Subject: [Boost-users] [posix_time] time_duration operator/() overload [...] I assume I'm not the first to compare time_durations so I was wondering if there was a reason operator/() is not overloaded for time_duration? Or if there is generally a better way to compare two durations?
Maybe you are interested by the library on the review schedule Boost.Chrono.
The library can be downloaded from http://www.boostpro.com/vault/index.php?action=downloadfile&filename=chrono.zip&directory=System&and the documentation is online on the sandbox http://svn.boost.org/svn/boost/sandbox/chrono/libs/chrono/doc/html/index.htm... .
The library provides the operator/() you are looking for, but on a generic duration type. Please let me know if this responds to your needs or whatever is missing from your point of view.
From what I read from the docs this seems to fit quite well. I did not have
the time to actually test this in my project so far, but this is a library I'll keep watching and I'll try out, when I find the time. Thanks for your message, Norbert
participants (4)
-
Igor R
-
Norbert Wenzel
-
Norbert Wenzel
-
vicente.botet