ratio


1. Scale and Convert Units

#include <ratio>

using namespace std;

int main() {
  using km = ratio<1000, 1>;  // 1 kilometer is 1000 meters
  using mi = ratio<1609, 1>;  // 1 mile is 1609 meters

  cout << convert_to<mi>(1.0, km) << " miles\n";  // Output: 0.621371
  cout << convert_to<km>(1.0, mi) << " kilometers\n";  // Output: 1.60934
  return 0;
}

2. Compute Proportions

#include <ratio>

using namespace std;

int main() {
  using width = ratio<3, 4>;
  using height = ratio<2, 3>;

  cout << (width / height).den << ":" << (width / height).num << endl;  // Output: 3:2
  return 0;
}

3. Parameterize Templates

4. Construct Rational Numbers

5. Solve Linear Equations

6. Calculate Percentages

7. Calculate Averages

8. Calculate Ratios of Differences

9. Calculate Ratios of Products

10. Calculate Ratios of Quotients

11. Calculate Ratios of Powers

12. Calculate Ratios of Inverses

13. Calculate Ratios of Common Denominators

14. Calculate Ratios of Common Numerators

15. Calculate Ratios of Equal Ratios

16. Calculate Ratios of Unequal Ratios

17. Calculate Ratios of Greater Ratios

18. Calculate Ratios of Less Ratios

19. Calculate Ratios of Greater or Equal Ratios

20. Calculate Ratios of Less or Equal Ratios

21. Calculate Ratios of Negated Ratios

22. Calculate Ratios of Multiplied Ratios

23. Calculate Ratios of Divided Ratios

24. Calculate Ratios of Added Ratios

25. Calculate Ratios of Subtracted Ratios

26. Calculate Ratios of Reciprocals

27. Calculate Ratios of Powers

28. Calculate Ratios of Square Roots

29. Calculate Ratios of Logarithms

30. Calculate Ratios of Exponentials

31. Calculate Ratios of Trigonometric Functions

32. Calculate Ratios of Cosine Functions

33. Calculate Ratios of Tangent Functions

34. Calculate Ratios of Arcsine Functions

35. Calculate Ratios of Arccosine Functions

36. Calculate Ratios of Arctangent Functions

37. Calculate Ratios of Hyperbolic Sine Functions

38. Calculate Ratios of Hyperbolic Cosine Functions

39. Calculate Ratios of Hyperbolic Tangent Functions

40. Calculate Ratios of Inverse Hyperbolic Sine Functions

41. Calculate Ratios of Inverse Hyperbolic Cosine Functions

42. Calculate Ratios of Inverse Hyperbolic Tangent Functions

43. Calculate Ratios of Bernoulli Numbers

44. Calculate Ratios of Stirling Numbers of the First Kind

45. Calculate Ratios of Stirling Numbers of the Second Kind

46. Calculate Ratios of Bell Numbers

47. Calculate Ratios of Eulerian Numbers