CNL  2.0.2 (development)
Compositional Numeric Library
operators.h
1 
2 // Copyright John McFarlane 2018.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file ../LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #if !defined(CNL_IMPL_FRACTION_OPERATORS_H)
8 #define CNL_IMPL_FRACTION_OPERATORS_H
9 
10 #include "../config.h"
11 #include "definition.h"
12 #include "make_fraction.h"
13 #include "to_string.h"
14 
15 #if defined(CNL_IOSTREAMS_ENABLED)
16 #include <ostream>
17 #endif
18 
20 namespace cnl {
21  // cnl::fraction arithmetic
22  template<typename RhsNumerator, typename RhsDenominator>
23  [[nodiscard]] constexpr auto operator+(fraction<RhsNumerator, RhsDenominator> const& rhs)
24  {
25  return _impl::make_fraction(+rhs.numerator, +rhs.denominator);
26  }
27 
28  template<typename RhsNumerator, typename RhsDenominator>
29  [[nodiscard]] constexpr auto operator-(fraction<RhsNumerator, RhsDenominator> const& rhs)
30  {
31  return _impl::make_fraction(-rhs.numerator, rhs.denominator);
32  }
33 
34  template<
35  typename LhsNumerator, typename LhsDenominator, typename RhsNumerator,
36  typename RhsDenominator>
37  [[nodiscard]] constexpr auto operator+(
38  fraction<LhsNumerator, LhsDenominator> const& lhs,
39  fraction<RhsNumerator, RhsDenominator> const& rhs)
40  {
41  return _impl::make_fraction(
42  lhs.numerator * rhs.denominator + rhs.numerator * lhs.denominator,
43  lhs.denominator * rhs.denominator);
44  }
45 
46  template<
47  typename LhsNumerator, typename LhsDenominator, typename RhsNumerator,
48  typename RhsDenominator>
49  [[nodiscard]] constexpr auto operator-(
50  fraction<LhsNumerator, LhsDenominator> const& lhs,
51  fraction<RhsNumerator, RhsDenominator> const& rhs)
52  {
53  return _impl::make_fraction(
54  lhs.numerator * rhs.denominator - rhs.numerator * lhs.denominator,
55  lhs.denominator * rhs.denominator);
56  }
57 
58  template<
59  typename LhsNumerator, typename LhsDenominator, typename RhsNumerator,
60  typename RhsDenominator>
61  [[nodiscard]] constexpr auto operator*(
62  fraction<LhsNumerator, LhsDenominator> const& lhs,
63  fraction<RhsNumerator, RhsDenominator> const& rhs)
64  {
65  return _impl::make_fraction(
66  lhs.numerator * rhs.numerator, lhs.denominator * rhs.denominator);
67  }
68 
69  template<
70  typename LhsNumerator, typename LhsDenominator, typename RhsNumerator,
71  typename RhsDenominator>
72  [[nodiscard]] constexpr auto operator/(
73  fraction<LhsNumerator, LhsDenominator> const& lhs,
74  fraction<RhsNumerator, RhsDenominator> const& rhs)
75  {
76  return _impl::make_fraction(
77  lhs.numerator * rhs.denominator, lhs.denominator * rhs.numerator);
78  }
79 
80  // cnl::fraction comparison
81  template<
82  typename LhsNumerator, typename LhsDenominator, typename RhsNumerator,
83  typename RhsDenominator>
84  [[nodiscard]] constexpr auto operator==(
85  fraction<LhsNumerator, LhsDenominator> const& lhs,
86  fraction<RhsNumerator, RhsDenominator> const& rhs)
87  {
88  return lhs.numerator * rhs.denominator == rhs.numerator * lhs.denominator;
89  }
90 
91  template<
92  typename LhsNumerator, typename LhsDenominator, typename RhsNumerator,
93  typename RhsDenominator>
94  [[nodiscard]] constexpr auto operator!=(
95  fraction<LhsNumerator, LhsDenominator> const& lhs,
96  fraction<RhsNumerator, RhsDenominator> const& rhs)
97  {
98  return lhs.numerator * rhs.denominator != rhs.numerator * lhs.denominator;
99  }
100 
101  template<
102  typename LhsNumerator, typename LhsDenominator, typename RhsNumerator,
103  typename RhsDenominator>
104  [[nodiscard]] constexpr auto operator<(
105  fraction<LhsNumerator, LhsDenominator> const& lhs,
106  fraction<RhsNumerator, RhsDenominator> const& rhs)
107  {
108  return lhs.numerator * rhs.denominator < rhs.numerator * lhs.denominator;
109  }
110 
111  template<
112  typename LhsNumerator, typename LhsDenominator, typename RhsNumerator,
113  typename RhsDenominator>
114  [[nodiscard]] constexpr auto operator>(
115  fraction<LhsNumerator, LhsDenominator> const& lhs,
116  fraction<RhsNumerator, RhsDenominator> const& rhs)
117  {
118  return lhs.numerator * rhs.denominator > rhs.numerator * lhs.denominator;
119  }
120 
121  template<
122  typename LhsNumerator, typename LhsDenominator, typename RhsNumerator,
123  typename RhsDenominator>
124  [[nodiscard]] constexpr auto operator<=(
125  fraction<LhsNumerator, LhsDenominator> const& lhs,
126  fraction<RhsNumerator, RhsDenominator> const& rhs)
127  {
128  return lhs.numerator * rhs.denominator <= rhs.numerator * lhs.denominator;
129  }
130 
131  template<
132  typename LhsNumerator, typename LhsDenominator, typename RhsNumerator,
133  typename RhsDenominator>
134  [[nodiscard]] constexpr auto operator>=(
135  fraction<LhsNumerator, LhsDenominator> const& lhs,
136  fraction<RhsNumerator, RhsDenominator> const& rhs)
137  {
138  return lhs.numerator * rhs.denominator >= rhs.numerator * lhs.denominator;
139  }
140 
141 #if defined(CNL_IOSTREAMS_ENABLED)
142  template<typename Numerator, typename Denominator>
143  auto& operator<<(std::ostream& out, fraction<Numerator, Denominator> const& f)
144  {
145  return out << to_string(f);
146  }
147 #endif
148 }
149 
150 #endif // CNL_IMPL_FRACTION_OPERATORS_H
std::rel_ops::operator!=
T operator!=(T... args)
std::ostream
STL class.
std::to_string
T to_string(T... args)
cnl
compositional numeric library
Definition: abort.h:15