|
| 1 | +class Rational { |
| 2 | + private int numerator; |
| 3 | + private int denominator; |
| 4 | + |
| 5 | + Rational(final int numerator, final int denominator) { |
| 6 | + if (denominator == 0) { |
| 7 | + throw new IllegalArgumentException("Denominator must not be zero."); |
| 8 | + } |
| 9 | + |
| 10 | + this.numerator = numerator; |
| 11 | + this.denominator = denominator; |
| 12 | + |
| 13 | + this.reduce(); |
| 14 | + this.ensureSignInNumerator(); |
| 15 | + } |
| 16 | + |
| 17 | + private void reduce() { |
| 18 | + final int commonDivisor = gcd(this.numerator, this.denominator); |
| 19 | + |
| 20 | + this.numerator /= commonDivisor; |
| 21 | + this.denominator /= commonDivisor; |
| 22 | + } |
| 23 | + |
| 24 | + private int gcd(int a, int b) { |
| 25 | + while (b != 0) { |
| 26 | + final int t = b; |
| 27 | + b = a % b; |
| 28 | + a = t; |
| 29 | + } |
| 30 | + return a; |
| 31 | + } |
| 32 | + |
| 33 | + private void ensureSignInNumerator() { |
| 34 | + if (this.denominator < 0) { |
| 35 | + this.denominator = -this.denominator; |
| 36 | + this.numerator = -this.numerator; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + int getNumerator() { |
| 41 | + return this.numerator; |
| 42 | + } |
| 43 | + |
| 44 | + int getDenominator() { |
| 45 | + return this.denominator; |
| 46 | + } |
| 47 | + |
| 48 | + Rational abs() { |
| 49 | + return new Rational(abs(this.numerator), abs(this.denominator)); |
| 50 | + } |
| 51 | + |
| 52 | + private int abs(int x) { |
| 53 | + final int mask = x >> 31; |
| 54 | + return (mask ^ x) - mask; |
| 55 | + } |
| 56 | + |
| 57 | + Rational add(Rational that) { |
| 58 | + final int commonDenominator = this.denominator * that.denominator; |
| 59 | + |
| 60 | + return new Rational( |
| 61 | + this.numerator * that.denominator + that.numerator * this.denominator, |
| 62 | + commonDenominator); |
| 63 | + } |
| 64 | + |
| 65 | + Rational subtract(Rational that) { |
| 66 | + final int commonDenominator = this.denominator * that.denominator; |
| 67 | + |
| 68 | + return new Rational( |
| 69 | + this.numerator * that.denominator - that.numerator * this.denominator, |
| 70 | + commonDenominator); |
| 71 | + } |
| 72 | + |
| 73 | + Rational multiply(Rational that) { |
| 74 | + return new Rational(this.numerator * that.numerator, this.denominator * that.denominator); |
| 75 | + } |
| 76 | + |
| 77 | + Rational divide(Rational that) { |
| 78 | + return new Rational(this.numerator * that.denominator, this.denominator * that.numerator); |
| 79 | + } |
| 80 | + |
| 81 | + Rational pow(int n) { |
| 82 | + return new Rational(pow(this.numerator, n), pow(this.denominator, n)); |
| 83 | + } |
| 84 | + |
| 85 | + double pow(double x) { |
| 86 | + return Math.pow(this.numerator, x) / Math.pow(this.denominator, x); |
| 87 | + } |
| 88 | + |
| 89 | + private int pow(final int base, final int exponent) { |
| 90 | + int product = 1; |
| 91 | + |
| 92 | + for (int i = 0; i < exponent; i++) { |
| 93 | + product *= base; |
| 94 | + } |
| 95 | + |
| 96 | + return product; |
| 97 | + } |
| 98 | + |
| 99 | + double exp(double base) { |
| 100 | + return Math.pow(10.0, Math.log10(Math.pow(base, this.numerator)) / this.denominator); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public String toString() { |
| 105 | + return String.format("%d/%d", this.getNumerator(), this.getDenominator()); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public boolean equals(Object obj) { |
| 110 | + if (obj == null || !this.getClass().isAssignableFrom(obj.getClass())) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + Rational other = (Rational) obj; |
| 115 | + return this.getNumerator() == other.getNumerator() |
| 116 | + && this.getDenominator() == other.getDenominator(); |
| 117 | + } |
| 118 | +} |
0 commit comments