Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. please clarify std::ratio using
Forum Updated to NodeBB v4.3 + New Features

please clarify std::ratio using

Scheduled Pinned Locked Moved Unsolved C++ Gurus
1 Posts 1 Posters 202 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qAlexKo
    wrote on last edited by qAlexKo
    #1

    I study std::ratio from STL
    In test1: 1 inch + 1 cm == sum in mm
    In test 2 I want to do a function which does the same, but using arbitrary numbers of inches and centimeters. I did it but somehow awkward -- I have not used std::ratio_add. How to do it more correctly, using std::ratio_add?

    #include <iostream>
    #include <ratio>
    
    //sum inches and centimeters
    void test1()
    {
        std::cout << "-----test1-------";
        using inch = std::ratio<254, 10>;
        using cm = std::ratio<10, 1>;
        using sum = std::ratio_add<inch, cm>;
        std::cout << "t3: " << (double(sum::num) / sum::den) << '\n';
    }
    
    //The func must sum in millimeters X inches + Y centimeters
    void test2(int how_many_inches, int how_many_centimeters)
    {
        std::cout << "-----test2-------";
        using inch = std::ratio<254, 10>;
        using cm = std::ratio<10, 1>;
        using sum = std::ratio_add<inch, cm>;   //no use
        double inch_mm = (double(inch::num) / inch::den) * how_many_inches;
        double cm_mm = (double(cm::num) / cm::den) * how_many_centimeters;
        double sumXiYc = inch_mm + cm_mm;
        std::cout << "sumXiYc: " << sumXiYc << '\n';
    }
    
    //---------------------------------------
    int main()
    {
     test1();       //OK
     test2(10, 5);  //I try to sum 10 inches + 5 centimeters
    }
    //-----------------------------------------------------
    
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved