All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
fraction.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
17 #ifndef PAAL_FRACTION_HPP
18 #define PAAL_FRACTION_HPP
19 
20 #include "paal/utils/floating.hpp"
21 
22 namespace paal {
23 namespace data_structures {
24 
31 template <class A, class B> struct fraction {
33  using num_type = A;
35  using den_type = B;
37  A num;
39  B den;
41  fraction(A num, B den) : num(num), den(den) {}
42 };
43 
56 template <class A, class B, class C, class D>
57 bool operator<(const fraction<A, B> &f1, const fraction<C, D> &f2)
58 {
59  return f1.num * f2.den < f2.num * f1.den;
60 }
61 
74 template <class A, class B, class C, class D>
75 bool operator>(const fraction<A, B> &f1, const fraction<C, D> &f2)
76 {
77  return f2 < f1;
78 }
79 
92 template <class A, class B, class C, class D>
93 bool operator<=(const fraction<A, B> &f1, const fraction<C, D> &f2)
94 {
95  return !(f2 < f1);
96 }
97 
110 template <class A, class B, class C, class D>
111 bool operator>=(const fraction<A, B> &f1, const fraction<C, D> &f2)
112 {
113  return !(f1 < f2);
114 }
115 
130 template<class A, class B, class C, class D, class EPS = A>
131 bool are_fractions_equal(const fraction<A, B>& f1, const fraction<C, D>& f2, EPS eps = A{})
132 {
133  auto x = f1.num * f2.den - f2.num * f1.den;
135  return cmp.e(x, 0);
136 }
137 
148 template <class A, class B>
150 {
151  return fraction<A, B>(a, b);
152 }
153 
165 template<class A, class B, class C>
166 auto operator*(C c, const fraction<A, B>& f) {
167 
168  return make_fraction(c * f.num, f.den);
169 }
170 
171 }
172 }
173 
174 #endif // PAAL_FRACTION_HPP
auto operator*(C c, const fraction< A, B > &f)
operator*
Definition: fraction.hpp:166
bool are_fractions_equal(const fraction< A, B > &f1, const fraction< C, D > &f2, EPS eps=A{})
operator==
Definition: fraction.hpp:131
bool operator>=(const fraction< A, B > &f1, const fraction< C, D > &f2)
operator&gt;=
Definition: fraction.hpp:111
bool operator>(const fraction< A, B > &f1, const fraction< C, D > &f2)
operator&gt;
Definition: fraction.hpp:75
fraction< A, B > make_fraction(A a, B b)
make function for fraction
Definition: fraction.hpp:149
B den_type
denominator type
Definition: fraction.hpp:35
Class for comparing floating point.
Definition: floating.hpp:26
fraction(A num, B den)
constructor
Definition: fraction.hpp:41
simple class to represent fraction
Definition: fraction.hpp:31