C++ ratio

http://www.cplusplus.com/reference/ratio/ratio/
ratio提供了一个分数
但是
只能是常量,所以一般是用chrono 的 duration中

// ratio example
#include 
#include 
using namespace std;
int main ()
{
  typedef ratio<1,3> one_third;
  typedef ratio<2,4> two_fourths;
  cout << "one_third= " << one_third::num << "/" << one_third::den << endl;
  cout << "two_fourths= " << two_fourths::num << "/" << two_fourths::den << endl;
  typedef ratio_add sum;
  cout << "sum= " << sum::num << "/" << sum::den;
  cout << " (which is: " << ( double(sum::num) / sum::den ) << ")" << endl;
  cout << "1 kilogram has " << ( kilo::num / kilo::den ) << " grams";
  cout << endl;
  return 0;
}

运行结果

one_third= 1/3
two_fourths= 1/2
sum= 5/6 (which is: 0.833333)
1 kilogram has 1000 grams

这段代码可以看出
typedef定义一个ratio
ratio_add还能让两个ratio相加,这么好的功能居然只能用常量= =
还有一些预定义的对象,比如说kilo

你可能感兴趣的:(C++ ratio)