Opened 9 years ago

Closed 9 years ago

#9419 closed Bugs (fixed)

boost::chrono::floor()/round() and negative durations is wrong

Reported by: 1czajnik@… Owned by: viboes
Milestone: Boost 1.56.0 Component: chrono
Version: Boost 1.54.0 Severity: Problem
Keywords: Cc:

Description

The assert in the following program 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 );
}

Regards, Kris

Attachments (1)

chrono_rounding.cpp (4.4 KB ) - added by 1czajnik@… 9 years ago.
unit test demonstrating the problem

Download all attachments as: .zip

Change History (5)

by 1czajnik@…, 9 years ago

Attachment: chrono_rounding.cpp added

unit test demonstrating the problem

comment:1 by 1czajnik@…, 9 years ago

The attached unit test demonstrates the problem, as well as a problem with round<>, which I think doesn't follow the documented rule "tie towards even" for negative values.

There are also overflow tests, and they all pass ;-)

My output of this test is:

Running 4 test cases...
../chrono_rounding.cpp(28): error in "floor_": check seconds(-2) == floor<seconds>( milliseconds(-1999) ) failed [-2 seconds != -1 second]
../chrono_rounding.cpp(29): error in "floor_": check seconds(-2) == floor<seconds>( milliseconds(-1001) ) failed [-2 seconds != -1 second]
../chrono_rounding.cpp(31): error in "floor_": check seconds(-1) == floor<seconds>( milliseconds(-999) ) failed [-1 second != 0 seconds]
../chrono_rounding.cpp(32): error in "floor_": check seconds(-1) == floor<seconds>( milliseconds(-1) ) failed [-1 second != 0 seconds]
../chrono_rounding.cpp(63): error in "round_": check seconds(-2) == round<seconds>( milliseconds(-1500) ) failed [-2 seconds != -1 second]

*** 5 failures detected in test suite "Master Test Suite"

HTH, Kris

in reply to:  1 comment:2 by viboes, 9 years ago

Status: newassigned
Summary: boost::chrono::floor() and negative durations is wrongboost::chrono::floor()/round() and negative durations is wrong

Replying to 1czajnik@…:

The attached unit test demonstrates the problem, as well as a problem with round<>, which I think doesn't follow the documented rule "tie towards even" for negative values.

There are also overflow tests, and they all pass ;-)

My output of this test is:

Running 4 test cases...
../chrono_rounding.cpp(28): error in "floor_": check seconds(-2) == floor<seconds>( milliseconds(-1999) ) failed [-2 seconds != -1 second]
../chrono_rounding.cpp(29): error in "floor_": check seconds(-2) == floor<seconds>( milliseconds(-1001) ) failed [-2 seconds != -1 second]
../chrono_rounding.cpp(31): error in "floor_": check seconds(-1) == floor<seconds>( milliseconds(-999) ) failed [-1 second != 0 seconds]
../chrono_rounding.cpp(32): error in "floor_": check seconds(-1) == floor<seconds>( milliseconds(-1) ) failed [-1 second != 0 seconds]
../chrono_rounding.cpp(63): error in "round_": check seconds(-2) == round<seconds>( milliseconds(-1500) ) failed [-2 seconds != -1 second]

*** 5 failures detected in test suite "Master Test Suite"

HTH, Kris

Thanks Kris for the report.

I would take care of it. Clearly we missed the negative numbers :(

Best, Vicente

comment:3 by viboes, 9 years ago

Milestone: To Be DeterminedBoost 1.56.0

comment:4 by viboes, 9 years ago

Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.