[chrono] boost::chrono::floor() and negative durations

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

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
#include <cassert> int main() { boost::chrono::nanoseconds const nsec( -1 ); boost::chrono::seconds const sec = boost::chrono::floorboost::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

On Nov 20, 2013, at 12:49 PM, Vicente J. Botet Escriba
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
#include <cassert> int main() { boost::chrono::nanoseconds const nsec( -1 ); boost::chrono::seconds const sec = boost::chrono::floorboost::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
To floor(const duration & d) { + if (d ::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
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
#include <chrono>
using namespace std::chrono;
template

Le 21/11/13 21:23, Howard Hinnant a écrit :
On Nov 20, 2013, at 12:49 PM, Vicente J. Botet Escriba
wrote: 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
#include <cassert> int main() { boost::chrono::nanoseconds const nsec( -1 ); boost::chrono::seconds const sec = boost::chrono::floorboost::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
To floor(const duration & d) { + if (d ::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
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost #include <chrono>
using namespace std::chrono;
template
To floor(const duration & d) { if (d ::zero()) return duration_cast<To>(d)-To(1); else return duration_cast<To>(d); } #include <iostream>
int main() { using namespace std::literals; std::cout << floor<seconds>(-1000ms).count() << '\n'; }
For me outputs:
-2
How about this instead:
template
To floor(const std::chrono::duration & d) { To t = std::chrono::duration_cast<To>(d); if (t > d) --t; return t; }
You are right, as always. I will apply your suggestion. Thanks, Vicente

On Nov 21, 2013, at 4:58 PM, "Vicente J. Botet Escriba"
You are right, as always. I will apply your suggestion.
If I was always right, we wouldn't be discussing this bug. ;-) There may be optimization opportunities in Kris' suggestion, I haven't investigated that. Howard

2013/11/21 Howard Hinnant
On Nov 20, 2013, at 12:49 PM, Vicente J. Botet Escriba < vicente.botet@wanadoo.fr> wrote:
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
#include <cassert> int main() { boost::chrono::nanoseconds const nsec( -1 ); boost::chrono::seconds const sec = boost::chrono::floorboost::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
To floor(const duration & d) { + if (d ::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
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
#include <chrono>
using namespace std::chrono;
template
To floor(const duration & d) { if (d ::zero()) return duration_cast<To>(d)-To(1); else return duration_cast<To>(d); } #include <iostream>
int main() { using namespace std::literals; std::cout << floor<seconds>(-1000ms).count() << '\n'; }
For me outputs:
-2
How about this instead:
template
To floor(const std::chrono::duration & d) { To t = std::chrono::duration_cast<To>(d); if (t > d) --t; return t; } Howard
Ticket #9419 created. I think it's a bit more complicated. Doesn't Howard's version return -2 as well? I'll try to take a closer look how chrono implements floor, ceil and round and suggest something here. But I'm thinking of something like this implementation of integral division [1]. Cheers, Kris [1] http://goo.gl/F8by6T

2013/11/21 Krzysztof Czainski <1czajnik@gmail.com>
2013/11/21 Howard Hinnant
On Nov 20, 2013, at 12:49 PM, Vicente J. Botet Escriba < vicente.botet@wanadoo.fr> wrote:
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
#include <cassert> int main() { boost::chrono::nanoseconds const nsec( -1 ); boost::chrono::seconds const sec = boost::chrono::floorboost::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
To floor(const duration & d) { + if (d ::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
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
#include <chrono>
using namespace std::chrono;
template
To floor(const duration & d) { if (d ::zero()) return duration_cast<To>(d)-To(1); else return duration_cast<To>(d); } #include <iostream>
int main() { using namespace std::literals; std::cout << floor<seconds>(-1000ms).count() << '\n'; }
For me outputs:
-2
How about this instead:
template
To floor(const std::chrono::duration & d) { To t = std::chrono::duration_cast<To>(d); if (t > d) --t; return t; } Howard
Ticket #9419 created.
I think it's a bit more complicated. Doesn't Howard's version return -2 as well?
Oh, I get it now, clever ;) Regards, Kris
participants (3)
-
Howard Hinnant
-
Krzysztof Czainski
-
Vicente J. Botet Escriba