Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Difference Between floor and duration_cast?

So in c++11 the Chrono Library provides, duration_cast:

Computations are done in the widest type available and converted, as if by static_cast, to the result type only when finished

And c++17's floor:

Returns the greatest duration t representable in ToDuration that is less or equal to d

So for all x will the result of these 2 calls be equal:

  1. chrono::duration_cast<chrono::seconds>(x)
  2. chrono::floor<chrono::seconds>(x)
like image 927
Jonathan Mee Avatar asked Jan 26 '18 19:01

Jonathan Mee


People also ask

What is Duration_cast?

std::chrono::duration_cast Converts the value of dtn into some other duration type, taking into account differences in their periods. The function does not use implicit conversions.

What is the purpose of duration cast?

duration_cast performs a mathematical calculation to translate between two durations of different measurements. A static_cast can only be done between classes in a related hierarchy. A duration_cast translates between two classes that have absolutely no relationship, whatsoever, with each other.

What is std :: Chrono :: seconds?

std::chrono::seconds Instantiation of duration to represent seconds.


1 Answers

As far as I can tell, same as the difference between static_cast and std::floor: Negatives are rounded down instead of truncated toward zero.

#include <iostream>
#include <chrono>
using namespace std::chrono_literals;

int main() {
    std::cout << "duration_cast:" << std::endl;
    std::cout << "1.4s: " << std::chrono::duration_cast<std::chrono::seconds>(1400ms).count() << std::endl;
    std::cout << "1.5s: " << std::chrono::duration_cast<std::chrono::seconds>(1500ms).count() << std::endl;
    std::cout << "1.6s: " << std::chrono::duration_cast<std::chrono::seconds>(1600ms).count() << std::endl;
    std::cout << "-1.4s: " << std::chrono::duration_cast<std::chrono::seconds>(-1400ms).count() << std::endl;
    std::cout << "-1.5s: " << std::chrono::duration_cast<std::chrono::seconds>(-1500ms).count() << std::endl;
    std::cout << "-1.6s: " << std::chrono::duration_cast<std::chrono::seconds>(-1600ms).count() << std::endl;

    std::cout << "floor:" << std::endl;
    std::cout << "1.4s: " << std::chrono::floor<std::chrono::seconds>(1400ms).count() << std::endl;
    std::cout << "1.5s: " << std::chrono::floor<std::chrono::seconds>(1500ms).count() << std::endl;
    std::cout << "1.6s: " << std::chrono::floor<std::chrono::seconds>(1600ms).count() << std::endl;
    std::cout << "-1.4s: " << std::chrono::floor<std::chrono::seconds>(-1400ms).count() << std::endl;
    std::cout << "-1.5s: " << std::chrono::floor<std::chrono::seconds>(-1500ms).count() << std::endl;
    std::cout << "-1.6s: " << std::chrono::floor<std::chrono::seconds>(-1600ms).count() << std::endl;
    return 0;
}

.

duration_cast:
1.4s: 1
1.5s: 1
1.6s: 1
-1.4s: -1
-1.5s: -1
-1.6s: -1
floor:
1.4s: 1
1.5s: 1
1.6s: 1
-1.4s: -2
-1.5s: -2
-1.6s: -2

https://wandbox.org/permlink/SsmpRz6RkvbL6Sru

like image 53
0x5453 Avatar answered Sep 28 '22 02:09

0x5453