@@ -48,6 +48,14 @@ export namespace zero::math {
4848 [[nodiscard]] inline constexpr Natural operator -(Natural rhs) const noexcept ;
4949 [[nodiscard]] inline constexpr Natural operator *(Natural rhs) const noexcept ;
5050 [[nodiscard]] inline constexpr Rational operator /(Natural rhs) const noexcept ;
51+ // Comparison operator overloads
52+ [[nodiscard]] inline constexpr bool operator ==(Natural rhs) const noexcept ;
53+ [[nodiscard]] inline constexpr bool operator ==(unsigned int rhs) const noexcept ;
54+ // Printable
55+ inline constexpr friend std::ostream& operator <<(std::ostream& os, const Natural& rhs) {
56+ os << rhs._number ;
57+ return os;
58+ }
5159 };
5260
5361 // / A whole real number
@@ -70,10 +78,17 @@ export namespace zero::math {
7078 [[nodiscard]] inline constexpr Integer operator *(Integer rhs) const noexcept ;
7179 [[nodiscard]] inline constexpr Rational operator *(Rational rhs) const noexcept ;
7280 [[nodiscard]] inline constexpr Rational operator /(Integer rhs) const noexcept ;
81+ // Comparison operator overloads
7382 [[nodiscard]] inline constexpr bool operator ==(Integer rhs) const noexcept ;
83+ [[nodiscard]] inline constexpr bool operator ==(int rhs) const noexcept ;
7484
7585 // Explicit conversion operators
7686 [[nodiscard]] inline explicit operator int () const { return _number; }
87+ // Printable
88+ inline constexpr friend std::ostream& operator <<(std::ostream& os, const Integer& rhs) {
89+ os << rhs._number ;
90+ return os;
91+ }
7792 };
7893
7994 // / @brief A type that represents rational numbers of the form: ℚ = {a, b ∈ ℤ, b ≠ 0}
@@ -99,6 +114,8 @@ export namespace zero::math {
99114 Integer _numerator; // /< The numerator of the rational number, belonging to ℤ.
100115 Integer _denominator; // /< The denominator of the rational number, belonging to ℤ, NOT excluding the zero.
101116 public:
117+ static constexpr MathSymbol symbol = MathSymbol::Rationals;
118+
102119 [[nodiscard]] constexpr Rational (int numerator, int denominator) noexcept
103120 : _numerator(numerator), _denominator(denominator) {}
104121 [[nodiscard]] constexpr Rational (Natural numerator, Natural denominator) noexcept
@@ -113,6 +130,16 @@ export namespace zero::math {
113130
114131 // Arithmetic operator overloads
115132 [[nodiscard]] inline constexpr Rational operator +(Rational rhs) const ;
133+ // TODO complete arithmetic overloads
134+ // Comparison operator overloads
135+ [[nodiscard]] inline constexpr bool operator ==(Rational rhs) const noexcept ;
136+ // Printable
137+ inline constexpr friend std::ostream& operator <<(std::ostream& os, const Rational& rhs) {
138+ os << rhs._numerator ;
139+ os << 0x2044 ;
140+ os << rhs._denominator ;
141+ return os;
142+ }
116143 };
117144
118145
@@ -132,6 +159,7 @@ using namespace zero::math;
132159
133160 /* ++++++++ Operator overloads implementations ++++++++++*/
134161/* +++++++++++++++++ Naturals +++++++++++++++++*/
162+ // Arithmetic
135163[[nodiscard]] inline constexpr Natural Natural::operator +(const Natural rhs) const noexcept {
136164 return Natural (_number + rhs.number ());
137165}
@@ -146,8 +174,16 @@ using namespace zero::math;
146174[[nodiscard]] inline constexpr Rational Natural::operator /(const Natural rhs) const noexcept {
147175 return {static_cast <signed int >(_number), static_cast <signed int >(rhs.number ())};
148176}
177+ // Equality
178+ [[nodiscard]] inline constexpr bool Natural::operator ==(const Natural rhs) const noexcept {
179+ return _number == rhs.number ();
180+ }
181+ [[nodiscard]] inline constexpr bool Natural::operator ==(const unsigned int rhs) const noexcept {
182+ return _number == rhs;
183+ }
149184
150- /* +++++++++++++++++ Integers +++++++++++++++++*/
185+ /* +++++++++++++++++ Integers +++++++++++++++++*/
186+ // Arithmetic
151187[[nodiscard]] inline constexpr Integer Integer::operator +(const Integer rhs) const noexcept {
152188 return Integer (_number + rhs.number ());
153189}
@@ -163,11 +199,17 @@ using namespace zero::math;
163199[[nodiscard]] inline constexpr Rational Integer::operator /(const Integer rhs) const noexcept {
164200 return {static_cast <signed int >(_number), static_cast <signed int >(rhs.number ())};
165201}
202+ // Equality
166203[[nodiscard]] inline constexpr bool Integer::operator ==(const Integer rhs) const noexcept {
167204 return _number == rhs.number ();
168205}
206+ [[nodiscard]] inline constexpr bool Integer::operator ==(const int rhs) const noexcept {
207+ return _number == rhs;
208+ }
169209
170210 /* +++++++++++++++++ Rationals +++++++++++++++++*/
211+ // Arithmetic
212+
171213// / Adds the current rational number to another rational number.
172214// / @param rhs The rational number to be added.
173215// / @return The sum of the two rational numbers.
@@ -177,17 +219,18 @@ using namespace zero::math;
177219// / finds the least common multiple (LCM) of the denominators and scales the
178220// / numerators to have the LCM as the common denominator before adding.
179221[[nodiscard]] inline constexpr Rational Rational::operator +(const Rational rhs) const {
180- if (_denominator == rhs.denominator ())
222+ if (_denominator == rhs.denominator ()) // Like fractions
181223 return {
182224 static_cast <int >(_numerator) + static_cast <int >(rhs.numerator ()),
183225 static_cast <int >(_denominator)
184- }; // Like fractions
185- else {
226+ };
227+ else { // Unlike fractions
186228 const int lhs_numerator = static_cast <int >(_numerator);
187229 const int rhs_numerator = static_cast <int >(rhs._numerator );
188230 const int lhs_denominator = static_cast <int >(_denominator);
189231 const int rhs_denominator = static_cast <int >(rhs._denominator );
190232
233+ // Get their lcd by finding their lcm
191234 const auto lcd = zero::math::lcm (_denominator.number (), rhs.denominator ().number ());
192235
193236 // Scale numerators to have the common denominator (lcm)
@@ -196,3 +239,27 @@ using namespace zero::math;
196239 return {numerator, lcd};
197240 }
198241}
242+
243+ // Equality
244+
245+ // TODO should we check that 4/2 is the same as 2/1 right? Or we should maintain the difference and explictly
246+ // say that 4/2 aren't the same Rational number as 2/1?
247+ [[nodiscard]] inline constexpr bool Rational::operator ==(const Rational rhs) const noexcept {
248+ return _numerator == rhs.numerator () && _denominator == rhs.denominator ();
249+ }
250+
251+ /* +++++++++++++++++ oss operator overloads (oss) +++++++++++++++++*/
252+ // inline constexpr std::ostream& Natural::operator<<(std::ostream& os) {
253+ // os << _number;
254+ // return os;
255+ // }
256+ // inline constexpr std::ostream& Integer::operator<<(std::ostream& os) {
257+ // os << _number;
258+ // return os;
259+ // }
260+ // inline constexpr std::ostream& Rational::operator<<(std::ostream& os) {
261+ // os << _numerator;
262+ // os << 0x2044;
263+ // os << _denominator;
264+ // return os;
265+ // }
0 commit comments