Instantly share code, notes, and snippets.

# MihailJP/rational.hpp

Created January 17, 2013 16:40
Show Gist options
• Save MihailJP/4557372 to your computer and use it in GitHub Desktop.
Fraction class template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 #pragma once #include // Calculate GCD (for reduction) template T gcd(T x, T y) { // Euclidean algorithm T val1 = (x < y) ? y : x, val2 = (x < y) ? x : y; T* m = &val1; T* n = &val2; T* tmpptr = nullptr; while (true) { *m %= *n; if (*m == 0) break; tmpptr = m; m = n; n = tmpptr; } return *n; } // Fraction class template template class Rational { private: // Value T num, denom; // Reduction Rational& reduce() { if (denom == 0) throw std::range_error("Denominator is zero"); if (denom < 0) { num *= -1; denom *= -1; } if (num == 0) { denom = 1; } else { T gcd_ans = gcd(num, denom); num /= gcd_ans; denom /= gcd_ans; } return *this; } public: // Constructors Rational(T numerator = 0, T denominator = 1) { if (denominator == 0) throw std::invalid_argument("Cannot set denominator to 0"); num = numerator; denom = denominator; reduce(); } Rational(const Rational& arg) { num = arg.num; denom = arg.denom; } Rational& operator=(const Rational& arg) { num = arg.num; denom = arg.denom; return *this; } Rational& operator=(T arg) { num = arg; denom = 1; return *this; } // Unary plus and minus const Rational& operator+() const {return *this;} const Rational& operator-() const { return Rational(-num, denom).reduce(); } // addition const Rational& operator+(const Rational& addend) const { return Rational(num * addend.denom + addend.num * denom, denom * addend.denom).reduce(); } Rational& operator+=(const Rational& addend) { return (*this = Rational(num * addend.denom + addend.num * denom, denom * addend.denom).reduce()); } // Subtraction const Rational& operator-(const Rational& subtrahend) const { return Rational(num * subtrahend.denom - subtrahend.num * denom, denom * subtrahend.denom).reduce(); } Rational& operator-=(const Rational& subtrahend) { return (*this = Rational(num * subtrahend.denom - subtrahend.num * denom, denom * subtrahend.denom).reduce()); } // Multiplication const Rational& operator*(const Rational& multiplier) const { return Rational(num * multiplier.num, denom * multiplier.denom).reduce(); } Rational& operator*=(const Rational& multiplier) { return (*this = Rational(num * multiplier.num, denom * multiplier.denom).reduce()); } // Division const Rational& operator/(const Rational& divisor) const { if (multiplier.num == 0) throw std::invalid_argument("Division by zero"); return Rational(num * divisor.denom, denom * divisor.num).reduce(); } Rational& operator/=(const Rational& divisor) { if (multiplier.num == 0) throw std::invalid_argument("Division by zero"); return (*this = Rational(num * divisor.denom, denom * divisor.num).reduce()); } // Equality bool operator==(const Rational& arg) const { return (num == arg.num) && (denom == arg.denom); } bool operator!=(const Rational& arg) const {return !(*this == arg);} // Comparison bool operator<(const Rational& arg) const { return ((num * arg.denom) < (arg.num * denom)); } bool operator>=(const Rational& arg) const {return !(*this < arg);} bool operator>(const Rational& arg) const {return (arg < *this);} bool operator<=(const Rational& arg) const {return !(arg < *this);} // Accessor T getNumerator() const {return num;} T getDenominator() const {return denom;} // Cast into float operator float() const {return (float)num / (float)denom;} operator double() const {return (double)num / (double)denom;} operator long double() const {return (long double)num / (long double)denom;} };

### timjm25 commented Jul 21, 2022

I really like your template. Do you happen to know why the multiplier.num leads to an error in the if statement for division. Thank you so much for your help.