7 #if !defined(CNL_IMPL_FRACTION_OPERATORS_H)
8 #define CNL_IMPL_FRACTION_OPERATORS_H
10 #include "../config.h"
11 #include "definition.h"
12 #include "make_fraction.h"
13 #include "to_string.h"
15 #if defined(CNL_IOSTREAMS_ENABLED)
22 template<
typename RhsNumerator,
typename RhsDenominator>
23 [[nodiscard]] constexpr
auto operator+(fraction<RhsNumerator, RhsDenominator>
const& rhs)
25 return _impl::make_fraction(+rhs.numerator, +rhs.denominator);
28 template<
typename RhsNumerator,
typename RhsDenominator>
29 [[nodiscard]] constexpr
auto operator-(fraction<RhsNumerator, RhsDenominator>
const& rhs)
31 return _impl::make_fraction(-rhs.numerator, rhs.denominator);
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)
41 return _impl::make_fraction(
42 lhs.numerator * rhs.denominator + rhs.numerator * lhs.denominator,
43 lhs.denominator * rhs.denominator);
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)
53 return _impl::make_fraction(
54 lhs.numerator * rhs.denominator - rhs.numerator * lhs.denominator,
55 lhs.denominator * rhs.denominator);
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)
65 return _impl::make_fraction(
66 lhs.numerator * rhs.numerator, lhs.denominator * rhs.denominator);
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)
76 return _impl::make_fraction(
77 lhs.numerator * rhs.denominator, lhs.denominator * rhs.numerator);
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)
88 return lhs.numerator * rhs.denominator == rhs.numerator * lhs.denominator;
92 typename LhsNumerator,
typename LhsDenominator,
typename RhsNumerator,
93 typename RhsDenominator>
95 fraction<LhsNumerator, LhsDenominator>
const& lhs,
96 fraction<RhsNumerator, RhsDenominator>
const& rhs)
98 return lhs.numerator * rhs.denominator != rhs.numerator * lhs.denominator;
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)
108 return lhs.numerator * rhs.denominator < rhs.numerator * lhs.denominator;
112 typename LhsNumerator,
typename LhsDenominator,
typename RhsNumerator,
113 typename RhsDenominator>
115 fraction<LhsNumerator, LhsDenominator>
const& lhs,
116 fraction<RhsNumerator, RhsDenominator>
const& rhs)
118 return lhs.numerator * rhs.denominator > rhs.numerator * lhs.denominator;
122 typename LhsNumerator,
typename LhsDenominator,
typename RhsNumerator,
123 typename RhsDenominator>
125 fraction<LhsNumerator, LhsDenominator>
const& lhs,
126 fraction<RhsNumerator, RhsDenominator>
const& rhs)
128 return lhs.numerator * rhs.denominator <= rhs.numerator * lhs.denominator;
132 typename LhsNumerator,
typename LhsDenominator,
typename RhsNumerator,
133 typename RhsDenominator>
135 fraction<LhsNumerator, LhsDenominator>
const& lhs,
136 fraction<RhsNumerator, RhsDenominator>
const& rhs)
138 return lhs.numerator * rhs.denominator >= rhs.numerator * lhs.denominator;
141 #if defined(CNL_IOSTREAMS_ENABLED)
142 template<
typename Numerator,
typename Denominator>
143 auto& operator<<(
std::ostream& out, fraction<Numerator, Denominator>
const& f)
150 #endif // CNL_IMPL_FRACTION_OPERATORS_H