Boost logo

Boost :

Subject: Re: [boost] [chrono] boost::chrono::floor() and negative durations
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2013-11-20 12:49:52


Le 20/11/13 13:45, Krzysztof Czainski a écrit :
> Hello,
>
> The function boost::chrono::floor() is documented: "This function round
> down the given parameter." Therefore I expect the assert in the following
> program to pass, but it fails:
>
> #include <boost/chrono/floor.hpp>
> #include <cassert>
>
> int main()
> {
> boost::chrono::nanoseconds const nsec( -1 );
> boost::chrono::seconds const sec =
> boost::chrono::floor<boost::chrono::seconds>(nsec);
> assert( sec.count() == -1 );
> }
>
> Is this intended behavior? If yes, could the docs be updated to clarify
> this?
>
>
Hi,

no, there is a bug with negative numbers.

      template <class To, class Rep, class Period>
      To floor(const duration<Rep, Period>& d)
      {
+ if (d<duration<Rep, Period>::zero())
+ return duration_cast<To>(d)-To(1);
+ else
          return duration_cast<To>(d);
      }

Please, coul dyou create a ticket so that I don't forget it.

Thanks,
Vicente


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