MPL11
 All Classes Namespaces Files Typedefs Macros Groups Pages
rational.hpp
Go to the documentation of this file.
1 
12 #ifndef BOOST_MPL11_RATIONAL_HPP
13 #define BOOST_MPL11_RATIONAL_HPP
14 
16 
17 #include <boost/mpl11/bool.hpp>
19 #include <boost/mpl11/core.hpp>
21 #include <boost/mpl11/field.hpp>
22 #include <boost/mpl11/group.hpp>
23 #include <boost/mpl11/monoid.hpp>
25 #include <boost/mpl11/ring.hpp>
26 
27 
28 namespace boost { namespace mpl11 {
29  template <typename T, T numerator, T denominator>
30  struct rational_c {
31  using type = rational_c;
32  using mpl_datatype = Rational;
33 
34  static constexpr T num = numerator;
35  static constexpr T den = denominator;
36  };
37 
38  template <>
39  struct cast<Integer, Rational> {
40  using type = cast;
41  template <typename i>
42  using apply = rational_c<
43  typename i::value_type, i::value
44  >;
45  };
46 
47  template <>
48  struct Monoid<Rational> : instantiate<Monoid>::with<Rational> {
49  template <typename x, typename y>
50  using plus_impl = rational_c<
51  decltype(true ? x::num : y::num),
52  (x::num * y::den) + (x::den * y::num),
53  x::den * y::den
54  >;
55 
56  template <typename ...>
57  using zero_impl = rational_c<long long, 0>;
58  };
59 
60  template <>
61  struct Group<Rational> : instantiate<Group>::with<Rational> {
62  template <typename x, typename y>
63  using minus_impl = rational_c<
64  decltype(true ? x::num : y::num),
65  (x::num * y::den) - (x::den * y::num),
66  x::den * y::den
67  >;
68 
69  template <typename x>
70  using negate_impl = rational_c<
71  decltype(-x::num), -x::num, x::den
72  >;
73  };
74 
75  template <>
76  struct Ring<Rational> : instantiate<Ring>::with<Rational> {
77  template <typename x, typename y>
78  using mult_impl = rational_c<
79  decltype(true ? x::num : y::num),
80  x::num * y::num,
81  x::den * y::den
82  >;
83 
84  template <typename ...>
85  using one_impl = rational_c<long long, 1>;
86  };
87 
88  template <>
89  struct Field<Rational> : instantiate<Field>::with<Rational> {
90  template <typename x, typename y>
91  using quot_impl = rational_c<
92  decltype(true ? x::num : y::num),
93  x::num * y::den,
94  x::den * y::num
95  >;
96 
97  template <typename x>
98  using recip_impl = rational_c<
99  decltype(x::num), x::den, x::num
100  >;
101  };
102 
103  template <>
104  struct Comparable<Rational> : instantiate<Comparable>::with<Rational> {
105  template <typename x, typename y>
106  using equal_impl = bool_<
107  x::num * y::den == x::den * y::num
108  >;
109 
110  template <typename x, typename y>
111  using not_equal_impl = bool_<
112  x::num * y::den != x::den * y::num
113  >;
114  };
115 
116  template <>
117  struct Orderable<Rational> : instantiate<Orderable>::with<Rational> {
118  template <typename x, typename y>
119  using less_impl = bool_<(
120  x::num * y::den < x::den * y::num
121  )>;
122 
123  template <typename x, typename y>
124  using less_equal_impl = bool_<(
125  x::num * y::den <= x::den * y::num
126  )>;
127 
128  template <typename x, typename y>
129  using greater_impl = bool_<(
130  x::num * y::den > x::den * y::num
131  )>;
132 
133  template <typename x, typename y>
134  using greater_equal_impl = bool_<(
135  x::num * y::den >= x::den * y::num
136  )>;
137  };
138 }} // end namespace boost::mpl11
139 
140 #endif // !BOOST_MPL11_RATIONAL_HPP
Forward declares the Rational datatype.
Defines the Ring typeclass.
Defines the Comparable typeclass.
Defines the Field typeclass.
Defines the methods of the Enumerable typeclass.
Defines the Core module.
Invokes a metafunction class with the given arguments.
Definition: functional.hpp:39
Defines the Group typeclass.
Defines the Orderable typeclass.
Forwards to boost/mpl11/integer.hpp.
Defines the Monoid typeclass.