From 92e6d3e7970f0e574ebbaa5198971e7af0d30499 Mon Sep 17 00:00:00 2001 From: subcrip Date: Tue, 26 Nov 2024 19:02:53 +0000 Subject: [PATCH] Update number/fractional.cc Signed-off-by: subcrip --- number/fractional.cc | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/number/fractional.cc b/number/fractional.cc index 2893953..aa53e91 100644 --- a/number/fractional.cc +++ b/number/fractional.cc @@ -1,6 +1,3 @@ -template -inline T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); } - template struct fractional { T p, q; @@ -12,20 +9,20 @@ struct fractional { template fractional(const U& p) : p(p), q(1) { reduce(); } fractional(const T& p, const T& q) : p(p), q(q) { reduce(); } - inline fractional operator+(void) const { return *this; } - inline fractional operator-(void) const { return { -p, q }; } - inline fractional operator+(const fractional& rhs) const { return { p * rhs.q + q * rhs.p, q * rhs.q }; } - inline fractional operator-(const fractional& rhs) const { return *this + (-rhs); } - inline fractional operator*(const fractional& rhs) const { return { p * rhs.p, q * rhs.q }; } - inline fractional operator/(const fractional& rhs) const { return *this * fractional(rhs.q, rhs.p); } + inline friend fractional operator+(const fractional& lhs) { return *lhs; } + inline friend fractional operator-(const fractional& lhs) { return { -lhs.p, lhs.q }; } + inline friend fractional operator+(const fractional& lhs, const fractional& rhs) { return { lhs.p * rhs.q + lhs.q * rhs.p, lhs.q * rhs.q }; } + inline friend fractional operator-(const fractional& lhs, const fractional& rhs) { return lhs + (-rhs); } + inline friend fractional operator*(const fractional& lhs, const fractional& rhs) { return { lhs.p * rhs.p, lhs.q * rhs.q }; } + inline friend fractional operator/(const fractional& lhs, const fractional& rhs) { return lhs * fractional(rhs.q, rhs.p); } inline fractional& operator+=(const fractional& rhs) { return *this = *this + rhs; } inline fractional& operator-=(const fractional& rhs) { return *this = *this - rhs; } inline fractional& operator*=(const fractional& rhs) { return *this = *this * rhs; } inline fractional& operator/=(const fractional& rhs) { return *this = *this / rhs; } - inline bool operator==(const fractional& rhs) const { return p == rhs.p and q == rhs.q; } - inline bool operator!=(const fractional& rhs) const { return not (*this == rhs); } - inline bool operator<(const fractional& rhs) const { return (*this - rhs).p < 0; } - inline bool operator>=(const fractional& rhs) const { return not (*this < rhs); } - inline bool operator>(const fractional& rhs) const { return *this >= rhs and *this != rhs; } - inline bool operator<=(const fractional& rhs) const { return *this < rhs or *this == rhs; } + inline friend bool operator==(const fractional& lhs, const fractional& rhs) { return lhs.p == rhs.p and lhs.q == rhs.q; } + inline friend bool operator!=(const fractional& lhs, const fractional& rhs) { return not (lhs == rhs); } + inline friend bool operator<(const fractional& lhs, const fractional& rhs) { return (lhs - rhs).p < 0; } + inline friend bool operator>=(const fractional& lhs, const fractional& rhs) { return not (lhs < rhs); } + inline friend bool operator>(const fractional& lhs, const fractional& rhs) { return lhs >= rhs and lhs != rhs; } + inline friend bool operator<=(const fractional& lhs, const fractional& rhs) { return lhs < rhs or lhs == rhs; } };