Determine the part of chrono durations

αλεχολυτ

I would like to compute which part of one duration contains in another duration. There is standard implementation for integers (6), but it gives me 0 (due to integer divison) for the following example:

auto s = 1s;
auto ms = 200ms;
std::cout << ms / s; // 0, but I want 0.2 here

Is there a more elegant and generic way to compute a such value instead of the following ugly solution?

#include <chrono>
#include <iostream>

int main()
{
    using namespace std::chrono_literals;    

    auto s = 1s;
    auto ms = 200ms;

    const auto part = 1. * ms.count() / std::chrono::duration_cast<decltype(ms)>(s).count();

    std::cout << part << '\n';
}

One smart way is to use 1. / (s / ms);, but it's not fit for any duration types.

Caleth

Use a double based duration.

#include <chrono>
#include <iostream>

int main()
{
    using namespace std::chrono_literals;    
    using my_seconds = std::chrono::duration<double>;

    my_seconds s = 1s;
    my_seconds ms = 200ms;

    std::cout << ms / s << '\n';
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to determine if a String is not a part of an array and count occurrences?

How to not round durations

What is the best way to form custom std::chrono::durations and std::ratios?

Regex for ISO 8601 durations

Determine if PHP files is Running as Part of a `phar` archive

How to format std::chrono durations?

Determine if instance is a part of some AutoScaling Group in AWS

How to determine if a module name is part of python standard library

C++ Chrono determine whether day is a weekend?

Difference between durations

Determine which part of an interval overlaps a given weekday in a given time zone

Differentiating std::chrono durations that have the same type and ratio?

Converting between integer std::chrono::durations

How to determine the last part of URL with jq?

dividing and multiplying std::chrono::durations

Is there library function to determine which part of a string is number?

How can I determine if a string is part of an enum?

How to determine if a product is part of a hidden collection in Shopify liquid templates?

How to determine what part of an AJAX application is slowing things down?

How to determine which part of the pattern has match in Regex

How to determine a logic part with python's beautiful soup when crawling

Determine number of non-Sundays in a part of a month

Python 3 determine which part of "or" operation was true

Why won't smaller chrono::durations subtract?

How to determine if whether a disk was part of RAID 5 or RAID 1?

Regex to find time durations

Is there a way to determine part of speech patterns in a dataset of sentences?

Converting trip durations to parking durations

A formula to determine if value is part of set

TOP Ranking

HotTag

Archive