001package org.clafer.math;
002
003import org.clafer.common.Util;
004
005/**
006 * A rational number. Operations throw an {@link ArithmeticException} when the
007 * result does not fit in a long, instead of silently overflowing.
008 *
009 * @author jimmy
010 */
011public class Rational implements Comparable<Rational> {
012
013    public static final Rational Zero = new Rational(0);
014    public static final Rational One = new Rational(1);
015
016    private final long numerator, denominator;
017
018    public Rational(long whole) {
019        this.numerator = whole;
020        this.denominator = 1;
021    }
022
023    public Rational(long numerator, long denominator) {
024        if (denominator == 0) {
025            throw new IllegalArgumentException();
026        }
027        long n, d;
028        if (denominator > 0) {
029            n = numerator;
030            d = denominator;
031        } else {
032            n = Math.negateExact(numerator);
033            d = Math.negateExact(denominator);
034        }
035        long gcd = Util.gcd(Math.abs(n), d);
036        assert gcd > 0;
037        assert n % gcd == 0;
038        assert d % gcd == 0;
039        this.numerator = n / gcd;
040        this.denominator = d / gcd;
041    }
042
043    private Rational(long numerator, long denominator, boolean force) {
044        this.numerator = numerator;
045        this.denominator = denominator;
046    }
047
048    public long getNumerator() {
049        return numerator;
050    }
051
052    public long getDenominator() {
053        return denominator;
054    }
055
056    public boolean isZero() {
057        return numerator == 0;
058    }
059
060    public boolean isOne() {
061        return numerator == denominator;
062    }
063
064    public boolean isWhole() {
065        return denominator == 1;
066    }
067
068    public boolean isPositive() {
069        return numerator > 0;
070    }
071
072    public boolean isNegative() {
073        return numerator < 0;
074    }
075
076    public long ceil() {
077        return Util.divCeil(numerator, denominator);
078    }
079
080    public long floor() {
081        return Util.divFloor(numerator, denominator);
082    }
083
084    public Rational minus() {
085        return new Rational(Math.negateExact(numerator), denominator, true);
086    }
087
088    public Rational abs() {
089        return isNegative() ? minus() : this;
090    }
091
092    public Rational add(long addend) {
093        if (addend == 0) {
094            return this;
095        }
096        return new Rational(
097                Math.addExact(numerator, Math.multiplyExact(denominator, addend)),
098                denominator
099        );
100    }
101
102    public Rational add(Rational addend) {
103        if (addend.isZero()) {
104            return this;
105        }
106        return new Rational(
107                Math.addExact(Math.multiplyExact(numerator, addend.denominator),
108                        Math.multiplyExact(denominator, addend.numerator)),
109                Math.multiplyExact(denominator, addend.denominator)
110        );
111    }
112
113    public Rational sub(long subtrahend) {
114        if (subtrahend == 0) {
115            return this;
116        }
117        return new Rational(
118                Math.subtractExact(numerator, Math.multiplyExact(denominator, subtrahend)),
119                denominator
120        );
121    }
122
123    public Rational sub(Rational subtrahend) {
124        if (subtrahend.isZero()) {
125            return this;
126        }
127        return new Rational(
128                Math.subtractExact(Math.multiplyExact(numerator, subtrahend.denominator),
129                        Math.multiplyExact(denominator, subtrahend.numerator)),
130                Math.multiplyExact(denominator, subtrahend.denominator)
131        );
132    }
133
134    public Rational mul(long multiplier) {
135        if (multiplier == 1) {
136            return this;
137        }
138        return new Rational(
139                Math.multiplyExact(numerator, multiplier),
140                denominator);
141    }
142
143    public Rational mul(Rational multiplier) {
144        if (multiplier.isOne()) {
145            return this;
146        }
147        return new Rational(
148                Math.multiplyExact(numerator, multiplier.numerator),
149                Math.multiplyExact(denominator, multiplier.denominator));
150    }
151
152    public Rational div(long divisor) {
153        if (divisor == 0) {
154            throw new ArithmeticException("/ by zero");
155        } else if (divisor == 1) {
156            return this;
157        }
158        return div(new Rational(divisor));
159    }
160
161    public Rational div(Rational divisor) {
162        if (divisor.isZero()) {
163            throw new ArithmeticException("/ by zero");
164        } else if (divisor.isOne()) {
165            return this;
166        }
167        return new Rational(
168                Math.multiplyExact(numerator, divisor.denominator),
169                Math.multiplyExact(denominator, divisor.numerator));
170    }
171
172    public Rational max(Rational m) {
173        return compareTo(m) > 0 ? this : m;
174    }
175
176    public Rational min(Rational m) {
177        return compareTo(m) < 0 ? this : m;
178    }
179
180    public boolean isLessThan(Rational o) {
181        return compareTo(o) < 0;
182    }
183
184    @Override
185    public int compareTo(Rational o) {
186        return Long.compare(Math.multiplyExact(numerator, o.denominator),
187                Math.multiplyExact(o.numerator, denominator));
188    }
189
190    @Override
191    public boolean equals(Object obj) {
192        if (obj instanceof Rational) {
193            Rational other = (Rational) obj;
194            return numerator == other.numerator && denominator == other.denominator;
195        }
196        return false;
197    }
198
199    @Override
200    public int hashCode() {
201        long hash = numerator * 63 + denominator;
202        return (int) (hash ^ (hash >>> 32));
203    }
204
205    @Override
206    public String toString() {
207        if (numerator == 0) {
208            return "0";
209        }
210        if (denominator == 1) {
211            return Long.toString(numerator);
212        }
213        return numerator + "/" + denominator;
214    }
215}