tinympl  0.2
mini MPL library for C++11
ratio.hpp
1 // Copyright (C) 2013, Ennio Barbaro.
2 //
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://sbabbi.github.io/tinympl for documentation.
8 //
9 // You are welcome to contact the author at:
10 // enniobarbaro@gmail.com
11 //
12 
13 #ifndef TINYMPL_RATIO_HPP
14 #define TINYMPL_RATIO_HPP
15 
16 #include <cstdint>
17 #include <ratio>
18 #include <tinympl/functional.hpp>
19 
20 namespace tinympl
21 {
22 
23 namespace detail
24 {
25 //Construct a rational number by forcing a coprime representation
26 template<std::intmax_t Num, std::intmax_t Den> struct make_rational
27 {
28  typedef std::ratio<Num,Den> ratio_t;
29  typedef std::ratio<
30  ratio_t::num,
31  ratio_t::den> type;
32 };
33 }
34 
49 template<std::intmax_t Num,std::intmax_t Den> using rational = typename detail::make_rational<Num,Den>::type;
50 
51 template<std::intmax_t Num1,std::intmax_t Den1,
52  std::intmax_t Num2,std::intmax_t Den2> struct plus<
53  std::ratio<Num1,Den1>,
54  std::ratio<Num2,Den2> > : std::ratio_add<std::ratio<Num1,Den1>, std::ratio<Num2,Den2> > {};
55 
56 template<std::intmax_t Num1,std::intmax_t Den1,
57  std::intmax_t Num2,std::intmax_t Den2> struct minus<
58  std::ratio<Num1,Den1>,
59  std::ratio<Num2,Den2> > : std::ratio_subtract<std::ratio<Num1,Den1>, std::ratio<Num2,Den2> > {};
60 
61 
62 template<std::intmax_t Num1,std::intmax_t Den1,
63  std::intmax_t Num2,std::intmax_t Den2> struct multiplies<
64  std::ratio<Num1,Den1>,
65  std::ratio<Num2,Den2> > : std::ratio_multiply<std::ratio<Num1,Den1>, std::ratio<Num2,Den2> > {};
66 
67 template<std::intmax_t Num1,std::intmax_t Den1,
68  std::intmax_t Num2,std::intmax_t Den2> struct divides<
69  std::ratio<Num1,Den1>,
70  std::ratio<Num2,Den2> > : std::ratio_divide<std::ratio<Num1,Den1>, std::ratio<Num2,Den2> > {};
71 
72 template<std::intmax_t Num,std::intmax_t Den> struct negate<std::ratio<Num,Den> >
73 {
74  typedef std::ratio<-Num,Den> type;
75 };
76 
77 template<std::intmax_t Num1,std::intmax_t Den1,
78  std::intmax_t Num2,std::intmax_t Den2> struct equal_to<
79  std::ratio<Num1,Den1>,
80  std::ratio<Num2,Den2> > :
81  std::integral_constant<bool,
82  std::ratio<Num1,Den1>::num == std::ratio<Num2,Den2>::num &&
83  std::ratio<Num1,Den1>::den == std::ratio<Num2,Den2>::den> {};
84 
85 template<std::intmax_t Num1,std::intmax_t Den1,
86  std::intmax_t Num2,std::intmax_t Den2> struct less<
87  std::ratio<Num1,Den1>,
88  std::ratio<Num2,Den2> > : std::ratio_less<std::ratio<Num1,Den1>, std::ratio<Num2,Den2> > {};
89 
91 }
92 
93 #endif // TINYMPL_RATIO_HPP
Sums its arguments.
Definition: plus.hpp:26
typename detail::make_rational< Num, Den >::type rational
Convenience wrapper around std::ratio to automatically reduce num and den to coprime factors...
Definition: ratio.hpp:49