C++11中头文件ratio的使用

include是在C++11中引入的,在此文件中有一些模板类。

模板类std::ratio及相关的模板类(如std::ratio_add)提供编译时有理数算术支持。此模板的每个实例化都准确表示任一有限有理数。它们都是用来表示比例关系的模板类。

std::ratio的声明:

 

template class ratio;

其中N表示分子,D表示分母;intmax_t表示最大的有符号整数类型,N和D的绝对值都应该在intmax_t可表示的范围内,D不能是0。

 

std::ratio类一般不通过它的对象来表示,而是这个类型本身来表示的,但也可以通过它的对象来表示。std::ratio有两个成员常量:num表示的是分子,den表示的是分母。这里的num和den已经经过化简,因此输出值可能和定义时传入的不同,如typedef std::ratio<100, 10> ratio1, 输出值ratio1::num为10,ratio1::den为1.

在include中,除std::ratio外,还有编译时有理数算术:std::ratio_add、std::ratio_subtract、std::ratio_multiply、std::ratio_divide,它们和std::ratio一样,也都有两个成员常量:num和den。编译时有理数比较:std::ratio_equal、std::ratio_not_equal、std::ratio_less、std::ratio_less_equal、std::ratio_greater、std::ratio_greater_equal,它们都有value成员常量。它们的计算公式如下:

 

template 
using ratio_add = ratio < R1::num*R2::den+R2::num*R1::den, R1::den*R2::den >

template 
using ratio_subtract = std::ratio < R1::num*R2::den-R2::num*R1::den, R1::den*R2::den >

template 
using ratio_multiply = std::ratio < R1::num * R2::num, R1::den * R2::den >;

template 
using ratio_divide = ratio < R1::num * R2::den, R2::num * R1::den >;

template 
struct ratio_equal : integral_constant {}

template 
struct ratio_less : integral_constant < bool, R1::num*R2::den < R2::num*R1::den > {};

template 
struct ratio_greater : integral_constant < bool, ratio_less::value > {};

template 
struct ratio_greater_equal : integral_constant < bool, !ratio_less::value > {};

template 
struct ratio_less_equal : integral_constant < bool, !ratio_less::value > {}

template 
struct ratio_equal : integral_constant < bool, !ratio_equal::value > {};

下标剪切于: cplusplus

C++11中头文件ratio的使用_第1张图片

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "ratio.hpp"
#include 
#include 

//////////////////////////////////////////////////////////////////
// reference: http://www.cplusplus.com/reference/ratio/
int test_ratio_1()
{
	typedef std::ratio<1, 3> one_third;
	typedef std::ratio<2, 4> two_fourths;
	typedef std::ratio<2, 3> two_thirds;
	typedef std::ratio<1, 2> one_half;

	std::cout << "one_third= " << one_third::num << "/" << one_third::den << std::endl;
	std::cout << "two_fourths= " << two_fourths::num << "/" << two_fourths::den << std::endl; // Note: 1/2
	std::cout << "two_thirds= " << two_thirds::num << "/" << two_thirds::den << std::endl;
	std::cout << "one_half= " << one_half::num << "/" << one_half::den << std::endl;
	std::cout << std::endl;

{ // std::ratio
	typedef std::ratio_add sum;

	std::cout << "sum= " << sum::num << "/" << sum::den;
	std::cout << " (which is: " << (double(sum::num) / sum::den) << ")" << std::endl;
	std::cout << "1 kilogram has " << (std::kilo::num / std::kilo::den) << " grams";
	std::cout << std::endl;
}

{ // std::ratio_add
	typedef std::ratio_add sum;

	std::cout << "sum = " << sum::num << "/" << sum::den;
	std::cout << " (which is: " << (double(sum::num) / sum::den) << ")" << std::endl;
}

{ // std::ratio_subtract
	typedef std::ratio_subtract diff;

	std::cout << "diff = " << diff::num << "/" << diff::den;
	std::cout << " (which is: " << (double(diff::num) / diff::den) << ")" << std::endl;
}

{ // std::ratio_multiply
	typedef std::ratio_multiply result;

	std::cout << "result = " << result::num << "/" << result::den;
	std::cout << " (which is: " << (double(result::num) / result::den) << ")" << std::endl;
}

{ // std::ratio_divide
	typedef std::ratio_divide result;

	std::cout << "result = " << result::num << "/" << result::den;
	std::cout << " (which is: " << (double(result::num) / result::den) << ")" << std::endl;
	std::cout << std::endl;
}

{ // std::ratio_equal
	std::cout << "1/2 == 2/4 ? " << std::boolalpha;
	std::cout << std::ratio_equal::value << std::endl;
}

{ // std::ratio_greater
	std::cout << "1/3 > 1/2 ? " << std::boolalpha;
	std::cout << std::ratio_greater::value << std::endl;
}

{ // std::ratio_greater_equal
	std::cout << "1/3 >= 1/2 ? " << std::boolalpha;
	std::cout << std::ratio_greater_equal::value << std::endl;
}

{ // std::ratio_less
	std::cout << "1/3 < 1/2 ? " << std::boolalpha;
	std::cout << std::ratio_less::value << std::endl;
}

{ // std::ratio_less_equal
	std::cout << "1/3 <= 1/2 ? " << std::boolalpha;
	std::cout << std::ratio_less_equal::value << std::endl;
}

{ // std::ratio_not_equal
	std::cout << "1/2 != 2/4 ? " << std::boolalpha;
	std::cout << std::ratio_not_equal::value << std::endl;
}
	std::cout << std::endl;

	return 0;
}

/////////////////////////////////////////////////////////////////////
// reference: https://stackoverflow.com/questions/25005205/why-can-i-have-a-ratio-object-in-c
int test_ratio_2()
{
	// If you don't use a typedef you're creating an instance of std::ratio<1, 3> named one_third,
	// which is not suitable for passing as a type argument.In that case you'll need to use decltype
	// to get to the appropriate type that can be passed to ratio_add
	std::ratio<1, 3> one_third;
	std::ratio<2, 4> two_fourths;
	std::ratio_add sum;
	std::cout << decltype(sum)::den << std::endl;

	return 0;
}


GitHub:https://github.com/fengbingchun/Messy_Test

 

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