please clarify std::ratio using
Unsolved
C++ Gurus
-
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 } //-----------------------------------------------------