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 format std::chrono durations?

dividing and multiplying std::chrono::durations

Converting between integer std::chrono::durations

Why won't smaller chrono::durations subtract?

Dividing two chrono::durations to get fraction

Determine transitions and durations across rows in a data frame

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

Use of variadic argument ellipsis in a type trait for chrono durations

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

C++ Chrono determine whether day is a weekend?

How to determine cell value based on comparing other cells to find the fastest and slowest durations?

A formula to determine if value is part of set

Converting trip durations to parking durations

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

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

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

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

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

How to determine the last part of URL with jq?

Use cell value to determine a range in part of formula

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

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

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

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

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

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

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

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

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