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

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

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

std::ratio的声明:

 

template<intmax_t N, intmax_t D = 1> 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<ratio>中,除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 <typename R1, typename R2>
using ratio_add = ratio < R1::num*R2::den+R2::num*R1::den, R1::den*R2::den >

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

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

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

template <class R1, class R2>
struct ratio_equal : integral_constant<bool, R1::num==R2::num && R1::den==R2::den> {}

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

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

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

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

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

下标剪切于: cplusplus

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

#include "ratio.hpp"
#include <iostream>
#include <ratio>

//
// 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<one_third, two_fourths> 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<one_half, two_thirds> 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<two_thirds, one_half> 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<one_half, one_third> 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<one_half, one_third> 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<one_half, two_fourths>::value << std::endl;
}

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

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

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

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

{ // std::ratio_not_equal
	std::cout << "1/2 != 2/4 ? " << std::boolalpha;
	std::cout << std::ratio_not_equal<one_half, two_fourths>::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<decltype(one_third), decltype(two_fourths)> sum;
	std::cout << decltype(sum)::den << std::endl;

	return 0;
}


GitHubhttps://github.com/fengbingchun/Messy_Test

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值