1
0
Fork 0

Update number/fractional.cc

Signed-off-by: subcrip <contact@subc.rip>
This commit is contained in:
subcrip 2024-11-26 19:13:27 +00:00
parent 92e6d3e797
commit 2c5c036fdf
1 changed files with 12 additions and 12 deletions

View File

@ -9,20 +9,20 @@ struct fractional {
template <typename U>
fractional(const U& p) : p(p), q(1) { reduce(); }
fractional(const T& p, const T& q) : p(p), q(q) { reduce(); }
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); }
friend inline fractional operator+(const fractional& lhs) { return *lhs; }
friend inline fractional operator-(const fractional& lhs) { return { -lhs.p, lhs.q }; }
friend inline fractional operator+(const fractional& lhs, const fractional& rhs) { return { lhs.p * rhs.q + lhs.q * rhs.p, lhs.q * rhs.q }; }
friend inline fractional operator-(const fractional& lhs, const fractional& rhs) { return lhs + (-rhs); }
friend inline fractional operator*(const fractional& lhs, const fractional& rhs) { return { lhs.p * rhs.p, lhs.q * rhs.q }; }
friend inline 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 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; }
friend inline bool operator==(const fractional& lhs, const fractional& rhs) { return lhs.p == rhs.p and lhs.q == rhs.q; }
friend inline bool operator!=(const fractional& lhs, const fractional& rhs) { return not (lhs == rhs); }
friend inline bool operator<(const fractional& lhs, const fractional& rhs) { return (lhs - rhs).p < 0; }
friend inline bool operator>=(const fractional& lhs, const fractional& rhs) { return not (lhs < rhs); }
friend inline bool operator>(const fractional& lhs, const fractional& rhs) { return lhs >= rhs and lhs != rhs; }
friend inline bool operator<=(const fractional& lhs, const fractional& rhs) { return lhs < rhs or lhs == rhs; }
};