`
xuning2516
  • 浏览: 7629 次
  • 性别: Icon_minigender_1
  • 来自: 江西
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

C++ ratio

 
阅读更多

C++11提供了compile_time fractions andcompile-time rational arithmetic support。支持编译时常量。


头文件 <ratio>

来看ratio是怎么定义的

00152   template<intmax_t _Num, intmax_t _Den = 1>
00153     struct ratio
00154     {
00155       static_assert(_Den != 0, "denominator cannot be zero");
00156       static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
00157             "out of range");
00158 
00159       // Note: sign(N) * abs(N) == N
00160       static constexpr intmax_t num =
00161         _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;   //是一个静态常量成员变量,注意看他的初始化是在类的定义里面实现的但是在类的外面还是要声明,可以通过类名直接进行访问
00162 
00163       static constexpr intmax_t den =
00164         __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value; //可以看到这个最终的值是经过约分的,其中是gcd求得是两个数的最大公约数
00165 
00166       typedef ratio<num, den> type;
00167     };
00168 
00169   template<intmax_t _Num, intmax_t _Den>
00170     constexpr intmax_t ratio<_Num, _Den>::num;  //ratio的成员函数
00171 
00172   template<intmax_t _Num, intmax_t _Den>
00173     constexpr intmax_t ratio<_Num, _Den>::den;

通过constexpr实现了compile_time 的属性。是一个compile-time constants,可以当成常量来使用。是一个具体类(还有一个具体类pair)。ratiotypes are used as template parameters fordurationobjects

来看看一个调用的例子

	typedef ratio<5,3> FiveThirds;  //注意这是一个类型
	cout<<FiveThirds::num<<"/"<<FiveThirds::den;
	ratio<5,3>::type one;
	cout<<FiveThirds::num<<"/"<<FiveThirds::den;
	typedef ratio<25,15> AlsoFiveThirds; 
	cout<<AlsoFiveThirds::num<<"/"<<AlsoFiveThirds::den;
	ratio<25,15> two;  //注意这是一个对象
	cout<<two.num<<"/"<<two.den<<endl;   //5/3
	//ratio<5,0> three;  //error
//提供如下的操作方法
(C++11)
adds tworatioobjects at compile-time
(class template)
(C++11)
subtracts tworatioobjects at compile-time
(class template)
(C++11)
multiplies tworatioobjects at compile-time
(class template)
(C++11)
divides tworatioobjects at compile-time
(class template)
下面是ratio_add 的源代码

00175   /// ratio_add
00176   template<typename _R1, typename _R2>
00177     struct ratio_add     //还是采用结构体的形式实现,因此他返回的类型是ratio<>
00178     {
00179     private:
00180       static constexpr intmax_t __gcd =
00181         __static_gcd<_R1::den, _R2::den>::value;
00182       static constexpr intmax_t __n = __safe_add<
00183         __safe_multiply<_R1::num, (_R2::den / __gcd)>::value,
00184         __safe_multiply<_R2::num, (_R1::den / __gcd)>::value>::value;
00185 
00186       // The new numerator may have common factors with the denominator,
00187       // but they have to also be factors of __gcd.
00188       static constexpr intmax_t __gcd2 = __static_gcd<__n, __gcd>::value;
00189       
00190     public:
00191       typedef ratio<__n / __gcd2,
00192         __safe_multiply<_R1::den / __gcd2, _R2::den / __gcd>::value> type;
00193 
00194       static constexpr intmax_t num = type::num;
00195       static constexpr intmax_t den = type::den;
00196     };
00197 
00198   template<typename _R1, typename _R2>
00199     constexpr intmax_t ratio_add<_R1, _R2>::num;
00200 
00201   template<typename _R1, typename _R2>
00202     constexpr intmax_t ratio_add<_R1, _R2>::den;
来看看这里的具体的调用。这里实现和VC2012略有不同。但是返回的都是ratio<>,因此静态成员产生的是相应的类型。
ratio_add<FiveThirds,AlsoFiveThirds>::type three;
	cout<<three.num<<"/"<<three.den<<endl;

下面看看ratio提供的关系运算

/// ratio_equal
00270  template<typename _R1, typename _R2>
00271   struct ratio_equal
00272   : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>  //返回的类型是true_value or false_type
00273   { };

	ratio_equal<FiveThirds,AlsoFiveThirds>::type requal;
	cout<<boolalpha<<requal.value<<endl;   //输出false vs2012 (libstdC++是要输出true的)这里有所不同下面有具体的讲解
	cout<<ratio_equal<ratio<5,3>,ratio<5,3>>::value<<endl; //输出true

在vc2012 下面第一个输出false(?),可以看到

	// CLASS TEMPLATE ratio_equal
template<class _R1,
	class _R2>
	struct ratio_equal;

template<intmax_t _N1,
	intmax_t _D1,
	intmax_t _N2,
	intmax_t _D2>
	struct ratio_equal<ratio<_N1, _D1>, ratio<_N2, _D2> >
		: integral_constant<bool, _N1 == _N2 && _D1 == _D2>
	{	// tests if ratio == ratio
	};
它这里没有进行约分,不知道这是基于什么考虑。

但是在libstdC++里面我们可以看到

/// ratio_equal
00270   template<typename _R1, typename _R2>
00271     struct ratio_equal
00272     : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
00273     { };
可以知道这是经过约分之后进行的比较。


分享到:
评论

相关推荐

    Aspect ratio conversion

    Aspect Ratio conversion

    Mastering the C++17 .pdf

    reading technical documentation on std::locale and std::ratio , just to find out that they aren't useful in your daily work? In this book, I'll teach you the most important features of the C++17 ...

    Opening Window Time Ratio (OPW-TR) 的多线程实现 C/C++

    Opening Window Time Ratio (OPW-TR) 的多线程实现 C/C++ 源代码包

    Visual C++实用图像处理专业教程

    专业版软件包括该书全部图像处理的C语言源程序以及可执行的Visual C++ 界面源程序,可以满足大学教师、科研人员以及图像处理专业人员的需要。 3. \ImageSys试用版 包括通用图像处理系统ImgeSys的介绍和试用版的安装...

    Google C++ International Standard.pdf

    4.4 The C++ memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.5 The C++ object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    基于模型预测控制(MPC)的航路点跟踪器C++实现源码.zip

    基于模型预测控制(MPC)的航路点跟踪器C++实现源码.zip 【项目介绍】 基于模型预测控制(MPC)的航路点跟踪器,用于精确的路径跟踪。可以将它用作waypoint_follower,以及其他跟随节点的路径(例如pure_pursuit)。 ...

    Huffman compression class in C++

    In the best case, a file consisting of just one symbol will be encoded with a compression ratio of 1:8. Huffman coding is used in image compression; however, in JPEG2000, an arithmetic codec is ...

    C ++ Visvalingam算法的实现(也称为 Visvalingam Whyatt算法)

    C++ 实现:通过重复消除最小区域的线泛化(“Visvalingam 算法”) 使用的源数据:自然地球数据

    C++ 标准 ISO 14882-2011

    1.7 The C++ memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.8 The C++ object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    optimum-cycle-ratio-algorithms:最佳循环比算法

    为了便于参考,我将把该软件称为 CYCLE_RATIO。 最佳循环比问题 考虑一个循环图,其中每条边都有两个与之相关联的数字,称为权重和运输时间(由于该问题在交通网络优化中的应用而得名)。 一个循环的权重等于沿着...

    峰值信噪比

    Calculates the PSNR (Peak Signal to Noise Ratio)

    High quality image rotation (rotate by shear)

    Rotated image retains size and aspect ratio of source image (destination image size is usally bigger). Supports double precision rotation angles (0..360). Supports generic bitmap structures. Specific ...

    cpp-stdlib:C++标准库

    c_plus_plus_stdlib(C++标准库) 通用工具 pair和tuple pair tuple Smart Pointer(智能指针) shared_ptr weak_ptr unique_ptr auto_ptr Type Trait和Type Utility Type Trait Reference Warpper(外覆器) Function ...

    计算机完美编程(C,Java,C++,C#)

    // same ratio will lead to 50% cpu usage Int64 startTime = 0; while(true) { startTime = GetTickCount(); // busy loop while((GetTickCount() - startTime) ) ; // idle loop Sleep(idleTime); }

    Effective C++ 条款

     编译器会永远也看不到ASPECT_RATIO这个符号名,因为在源码进入编译器之前,它会被预处理程序去掉,于是ASPECT_RATIO不会加入到符号列表中。如果涉及到这个常量的代码在编译时报错,会很令人费解,因为报错信息指...

    基于粒子系统的火焰

    // Calculate The Aspect Ratio Of The Window gluPerspective(45.0f,1.3333f,0.1f,1000.0f); glMatrixMode(GL_MODELVIEW); // Set The Blending Function For Translucency glBlendFunc(GL_SRC_ALPHA,GL_ONE...

    HRRN.rar_HRRN_return

    Highest Return Ratio process scheduling algorithm simulation in C++. Good for process scheduling study

    xraue.zip_Borland_C++_

    Including the final calculation of the compressed image peak signal to noise ratio and compression of the source

    白平衡三种算法.zip

    算法应该比灰度世界的效果要好些,但是也还是受到Ratio这个参数的影像。特别是第二个图片,过高的Ration导致图片过于泛白。 3.同经典的一些算法相同,算法分为两个步骤:白点检测和白点调整。动态阈值的优点:1、该...

    final.c.zip_数据结构_C/C++_

    the file contains the method for data conversion.iput is sample ratio.output is the converted data.input should be given in file

Global site tag (gtag.js) - Google Analytics