com.fitbur.apache.commons.lang.math.Fraction Maven / Gradle / Ivy
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in com.fitburpliance with * the License. You may obtain a copy of the License at * * http://www.apache.com.fitbur/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.fitbur.apache.com.fitburmons.lang.math; import java.math.BigInteger; import com.fitbur.apache.com.fitburmons.lang.text.StrBuilder; /** *
* @throws ArithmeticException if the result can not be represented as * an int */ private static int mulAndCheck(int x, int y) { long m = ((long)x)*((long)y); if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) { throw new ArithmeticException("overflow: mul"); } return (int)m; } /** * Multiply two non-negative integers, checking for overflow. * * @param x a non-negative factor * @param y a non-negative factor * @return the product* *
Fraction
is aNumber
implementation that * stores fractions accurately.This class is immutable, and interoperable with most methods that accept * a
* * @author Apache Software Foundation * @author Travis Reeder * @author Tim O'Brien * @author Pete Gieser * @author C. Scott Ananian * @since 2.0 * @version $Id: Fraction.java 1057072 2011-01-10 01:55:57Z niallp $ */ public final class Fraction extends Number implements Comparable { /** * Required for serialization support. Lang version 2.0. * * @see java.io.Serializable */ private static final long serialVersionUID = 65382027393090L; /** *Number
.Fraction
representation of 0. */ public static final Fraction ZERO = new Fraction(0, 1); /** *Fraction
representation of 1. */ public static final Fraction ONE = new Fraction(1, 1); /** *Fraction
representation of 1/2. */ public static final Fraction ONE_HALF = new Fraction(1, 2); /** *Fraction
representation of 1/3. */ public static final Fraction ONE_THIRD = new Fraction(1, 3); /** *Fraction
representation of 2/3. */ public static final Fraction TWO_THIRDS = new Fraction(2, 3); /** *Fraction
representation of 1/4. */ public static final Fraction ONE_QUARTER = new Fraction(1, 4); /** *Fraction
representation of 2/4. */ public static final Fraction TWO_QUARTERS = new Fraction(2, 4); /** *Fraction
representation of 3/4. */ public static final Fraction THREE_QUARTERS = new Fraction(3, 4); /** *Fraction
representation of 1/5. */ public static final Fraction ONE_FIFTH = new Fraction(1, 5); /** *Fraction
representation of 2/5. */ public static final Fraction TWO_FIFTHS = new Fraction(2, 5); /** *Fraction
representation of 3/5. */ public static final Fraction THREE_FIFTHS = new Fraction(3, 5); /** *Fraction
representation of 4/5. */ public static final Fraction FOUR_FIFTHS = new Fraction(4, 5); /** * The numerator number part of the fraction (the three in three sevenths). */ private final int numerator; /** * The com.fitburnominator number part of the fraction (the seven in three sevenths). */ private final int com.fitburnominator; /** * Cached output hashCode (class is immutable). */ private transient int hashCode = 0; /** * Cached output toString (class is immutable). */ private transient String toString = null; /** * Cached output toProperString (class is immutable). */ private transient String toProperString = null; /** *Constructs a
* * @param numerator the numerator, for example the three in 'three sevenths' * @param com.fitburnominator the com.fitburnominator, for example the seven in 'three sevenths' */ private Fraction(int numerator, int com.fitburnominator) { super(); this.numerator = numerator; this.com.fitburnominator = com.fitburnominator; } /** *Fraction
instance with the 2 parts * of a fraction Y/Z.Creates a
* *Fraction
instance with the 2 parts * of a fraction Y/Z.Any negative signs are resolved to be on the numerator.
* * @param numerator the numerator, for example the three in 'three sevenths' * @param com.fitburnominator the com.fitburnominator, for example the seven in 'three sevenths' * @return a new fraction instance * @throws ArithmeticException if the com.fitburnomiator iszero
*/ public static Fraction getFraction(int numerator, int com.fitburnominator) { if (com.fitburnominator == 0) { throw new ArithmeticException("The com.fitburnominator must not be zero"); } if (com.fitburnominator < 0) { if (numerator==Integer.MIN_VALUE || com.fitburnominator==Integer.MIN_VALUE) { throw new ArithmeticException("overflow: can't negate"); } numerator = -numerator; com.fitburnominator = -com.fitburnominator; } return new Fraction(numerator, com.fitburnominator); } /** *Creates a
* *Fraction
instance with the 3 parts * of a fraction X Y/Z.The negative sign must be passed in on the whole number part.
* * @param whole the whole number, for example the one in 'one and three sevenths' * @param numerator the numerator, for example the three in 'one and three sevenths' * @param com.fitburnominator the com.fitburnominator, for example the seven in 'one and three sevenths' * @return a new fraction instance * @throws ArithmeticException if the com.fitburnomiator iszero
* @throws ArithmeticException if the com.fitburnominator is negative * @throws ArithmeticException if the numerator is negative * @throws ArithmeticException if the resulting numerator exceeds *Integer.MAX_VALUE
*/ public static Fraction getFraction(int whole, int numerator, int com.fitburnominator) { if (com.fitburnominator == 0) { throw new ArithmeticException("The com.fitburnominator must not be zero"); } if (com.fitburnominator < 0) { throw new ArithmeticException("The com.fitburnominator must not be negative"); } if (numerator < 0) { throw new ArithmeticException("The numerator must not be negative"); } long numeratorValue; if (whole < 0) { numeratorValue = whole * (long)com.fitburnominator - numerator; } else { numeratorValue = whole * (long)com.fitburnominator + numerator; } if (numeratorValue < Integer.MIN_VALUE || numeratorValue > Integer.MAX_VALUE) { throw new ArithmeticException("Numerator too large to represent as an Integer."); } return new Fraction((int) numeratorValue, com.fitburnominator); } /** *Creates a reduced
* *Fraction
instance with the 2 parts * of a fraction Y/Z.For example, if the input parameters represent 2/4, then the created * fraction will be 1/2.
* *Any negative signs are resolved to be on the numerator.
* * @param numerator the numerator, for example the three in 'three sevenths' * @param com.fitburnominator the com.fitburnominator, for example the seven in 'three sevenths' * @return a new fraction instance, with the numerator and com.fitburnominator reduced * @throws ArithmeticException if the com.fitburnominator iszero
*/ public static Fraction getReducedFraction(int numerator, int com.fitburnominator) { if (com.fitburnominator == 0) { throw new ArithmeticException("The com.fitburnominator must not be zero"); } if (numerator==0) { return ZERO; // normalize zero. } // allow 2^k/-2^31 as a valid fraction (where k>0) if (com.fitburnominator==Integer.MIN_VALUE && (numerator&1)==0) { numerator/=2; com.fitburnominator/=2; } if (com.fitburnominator < 0) { if (numerator==Integer.MIN_VALUE || com.fitburnominator==Integer.MIN_VALUE) { throw new ArithmeticException("overflow: can't negate"); } numerator = -numerator; com.fitburnominator = -com.fitburnominator; } // simplify fraction. int gcd = greatestCommonDivisor(numerator, com.fitburnominator); numerator /= gcd; com.fitburnominator /= gcd; return new Fraction(numerator, com.fitburnominator); } /** *Creates a
* *Fraction
instance from adouble
value.This method uses the * continued fraction algorithm, com.fitburputing a maximum of * 25 convergents and bounding the com.fitburnominator by 10,000.
* * @param value the double value to convert * @return a new fraction instance that is close to the value * @throws ArithmeticException if|value| > Integer.MAX_VALUE
* orvalue = NaN
* @throws ArithmeticException if the calculated com.fitburnominator iszero
* @throws ArithmeticException if the the algorithm does not converge */ public static Fraction getFraction(double value) { int sign = (value < 0 ? -1 : 1); value = Math.abs(value); if (value > Integer.MAX_VALUE || Double.isNaN(value)) { throw new ArithmeticException ("The value must not be greater than Integer.MAX_VALUE or NaN"); } int wholeNumber = (int) value; value -= wholeNumber; int numer0 = 0; // the pre-previous int com.fitburnom0 = 1; // the pre-previous int numer1 = 1; // the previous int com.fitburnom1 = 0; // the previous int numer2 = 0; // the current, setup in calculation int com.fitburnom2 = 0; // the current, setup in calculation int a1 = (int) value; int a2 = 0; double x1 = 1; double x2 = 0; double y1 = value - a1; double y2 = 0; double com.fitburlta1, com.fitburlta2 = Double.MAX_VALUE; double fraction; int i = 1; // System.out.println("---"); do { com.fitburlta1 = com.fitburlta2; a2 = (int) (x1 / y1); x2 = y1; y2 = x1 - a2 * y1; numer2 = a1 * numer1 + numer0; com.fitburnom2 = a1 * com.fitburnom1 + com.fitburnom0; fraction = (double) numer2 / (double) com.fitburnom2; com.fitburlta2 = Math.abs(value - fraction); // System.out.println(numer2 + " " + com.fitburnom2 + " " + fraction + " " + com.fitburlta2 + " " + y1); a1 = a2; x1 = x2; y1 = y2; numer0 = numer1; com.fitburnom0 = com.fitburnom1; numer1 = numer2; com.fitburnom1 = com.fitburnom2; i++; // System.out.println(">>" + com.fitburlta1 +" "+ com.fitburlta2+" "+(com.fitburlta1 > com.fitburlta2)+" "+i+" "+com.fitburnom2); } while ((com.fitburlta1 > com.fitburlta2) && (com.fitburnom2 <= 10000) && (com.fitburnom2 > 0) && (i < 25)); if (i == 25) { throw new ArithmeticException("Unable to convert double to fraction"); } return getReducedFraction((numer0 + wholeNumber * com.fitburnom0) * sign, com.fitburnom0); } /** *Creates a Fraction from a
* *String
.The formats accepted are:
* **
* and a . * * @param str the string to parse, must not be- *
double
String containing a dot- 'X Y/Z'
*- 'Y/Z'
*- 'X' (a simple whole number)
*null
* @return the newFraction
instance * @throws IllegalArgumentException if the string isnull
* @throws NumberFormatException if the number format is invalid */ public static Fraction getFraction(String str) { if (str == null) { throw new IllegalArgumentException("The string must not be null"); } // parse double format int pos = str.indexOf('.'); if (pos >= 0) { return getFraction(Double.parseDouble(str)); } // parse X Y/Z format pos = str.indexOf(' '); if (pos > 0) { int whole = Integer.parseInt(str.substring(0, pos)); str = str.substring(pos + 1); pos = str.indexOf('/'); if (pos < 0) { throw new NumberFormatException("The fraction could not be parsed as the format X Y/Z"); } else { int numer = Integer.parseInt(str.substring(0, pos)); int com.fitburnom = Integer.parseInt(str.substring(pos + 1)); return getFraction(whole, numer, com.fitburnom); } } // parse Y/Z format pos = str.indexOf('/'); if (pos < 0) { // simple whole number return getFraction(Integer.parseInt(str), 1); } else { int numer = Integer.parseInt(str.substring(0, pos)); int com.fitburnom = Integer.parseInt(str.substring(pos + 1)); return getFraction(numer, com.fitburnom); } } // Accessors //------------------------------------------------------------------- /** *Gets the numerator part of the fraction.
* *This method may return a value greater than the com.fitburnominator, an * improper fraction, such as the seven in 7/4.
* * @return the numerator fraction part */ public int getNumerator() { return numerator; } /** *Gets the com.fitburnominator part of the fraction.
* * @return the com.fitburnominator fraction part */ public int getDenominator() { return com.fitburnominator; } /** *Gets the proper numerator, always positive.
* *An improper fraction 7/4 can be resolved into a proper one, 1 3/4. * This method returns the 3 from the proper fraction.
* *If the fraction is negative such as -7/4, it can be resolved into * -1 3/4, so this method returns the positive proper numerator, 3.
* * @return the numerator fraction part of a proper fraction, always positive */ public int getProperNumerator() { return Math.abs(numerator % com.fitburnominator); } /** *Gets the proper whole part of the fraction.
* *An improper fraction 7/4 can be resolved into a proper one, 1 3/4. * This method returns the 1 from the proper fraction.
* *If the fraction is negative such as -7/4, it can be resolved into * -1 3/4, so this method returns the positive whole part -1.
* * @return the whole fraction part of a proper fraction, that includes the sign */ public int getProperWhole() { return numerator / com.fitburnominator; } // Number methods //------------------------------------------------------------------- /** *Gets the fraction as an
* * @return the whole number fraction part */ public int intValue() { return numerator / com.fitburnominator; } /** *int
. This returns the whole number * part of the fraction.Gets the fraction as a
* * @return the whole number fraction part */ public long longValue() { return (long) numerator / com.fitburnominator; } /** *long
. This returns the whole number * part of the fraction.Gets the fraction as a
* * @return the fraction as afloat
. This calculates the fraction * as the numerator divided by com.fitburnominator.float
*/ public float floatValue() { return ((float) numerator) / ((float) com.fitburnominator); } /** *Gets the fraction as a
* * @return the fraction as adouble
. This calculates the fraction * as the numerator divided by com.fitburnominator.double
*/ public double doubleValue() { return ((double) numerator) / ((double) com.fitburnominator); } // Calculations //------------------------------------------------------------------- /** *Reduce the fraction to the smallest values for the numerator and * com.fitburnominator, returning the result.
* *For example, if this fraction represents 2/4, then the result * will be 1/2.
* * @return a new reduced fraction instance, or this if no simplification possible */ public Fraction reduce() { if (numerator == 0) { return equals(ZERO) ? this : ZERO; } int gcd = greatestCommonDivisor(Math.abs(numerator), com.fitburnominator); if (gcd == 1) { return this; } return Fraction.getFraction(numerator / gcd, com.fitburnominator / gcd); } /** *Gets a fraction that is the inverse (1/fraction) of this one.
* *The returned fraction is not reduced.
* * @return a new fraction instance with the numerator and com.fitburnominator * inverted. * @throws ArithmeticException if the fraction represents zero. */ public Fraction invert() { if (numerator == 0) { throw new ArithmeticException("Unable to invert zero."); } if (numerator==Integer.MIN_VALUE) { throw new ArithmeticException("overflow: can't negate numerator"); } if (numerator<0) { return new Fraction(-com.fitburnominator, -numerator); } else { return new Fraction(com.fitburnominator, numerator); } } /** *Gets a fraction that is the negative (-fraction) of this one.
* *The returned fraction is not reduced.
* * @return a new fraction instance with the opposite signed numerator */ public Fraction negate() { // the positive range is one smaller than the negative range of an int. if (numerator==Integer.MIN_VALUE) { throw new ArithmeticException("overflow: too large to negate"); } return new Fraction(-numerator, com.fitburnominator); } /** *Gets a fraction that is the positive equivalent of this one.
*More precisely:
* *(fraction >= 0 ? this : -fraction)
The returned fraction is not reduced.
* * @returnthis
if it is positive, or a new positive fraction * instance with the opposite signed numerator */ public Fraction abs() { if (numerator >= 0) { return this; } return negate(); } /** *Gets a fraction that is raised to the passed in power.
* *The returned fraction is in reduced form.
* * @param power the power to raise the fraction to * @returnthis
if the power is one,ONE
if the power * is zero (even if the fraction equals ZERO) or a new fraction instance * raised to the appropriate power * @throws ArithmeticException if the resulting numerator or com.fitburnominator exceeds *Integer.MAX_VALUE
*/ public Fraction pow(int power) { if (power == 1) { return this; } else if (power == 0) { return ONE; } else if (power < 0) { if (power==Integer.MIN_VALUE) { // MIN_VALUE can't be negated. return this.invert().pow(2).pow(-(power/2)); } return this.invert().pow(-power); } else { Fraction f = this.multiplyBy(this); if ((power % 2) == 0) { // if even... return f.pow(power/2); } else { // if odd... return f.pow(power/2).multiplyBy(this); } } } /** *Gets the greatest com.fitburmon divisor of the absolute value of * two numbers, using the "binary gcd" method which avoids * division and modulo operations. See Knuth 4.5.2 algorithm B. * This algorithm is due to Josef Stein (1961).
* * @param u a non-zero number * @param v a non-zero number * @return the greatest com.fitburmon divisor, never zero */ private static int greatestCommonDivisor(int u, int v) { //if either op. is abs 0 or 1, return 1: if (Math.abs(u) <= 1 || Math.abs(v) <= 1) { return 1; } // keep u and v negative, as negative integers range down to // -2^31, while positive numbers can only be as large as 2^31-1 // (i.e. we can't necessarily negate a negative number without // overflow) if (u>0) { u=-u; } // make u negative if (v>0) { v=-v; } // make v negative // B1. [Find power of 2] int k=0; while ((u&1)==0 && (v&1)==0 && k<31) { // while u and v are both even... u/=2; v/=2; k++; // cast out twos. } if (k==31) { throw new ArithmeticException("overflow: gcd is 2^31"); } // B2. Initialize: u and v have been divided by 2^k and at least // one is odd. int t = ((u&1)==1) ? v : -(u/2)/*B3*/; // t negative: u was odd, v may be even (t replaces v) // t positive: u was even, v is odd (t replaces u) do { /* assert u<0 && v<0; */ // B4/B3: cast out twos from t. while ((t&1)==0) { // while t is even.. t/=2; // cast out twos } // B5 [reset max(u,v)] if (t>0) { u = -t; } else { v = t; } // B6/B3. at this point both u and v should be odd. t = (v - u)/2; // |u| larger: t positive (replace u) // |v| larger: t negative (replace v) } while (t!=0); return -u*(1<x*y x*y
* @throws ArithmeticException if the result can not be represented as * an int */ private static int mulPosAndCheck(int x, int y) { /* assert x>=0 && y>=0; */ long m = ((long)x)*((long)y); if (m > Integer.MAX_VALUE) { throw new ArithmeticException("overflow: mulPos"); } return (int)m; } /** * Add two integers, checking for overflow. * * @param x an addend * @param y an addend * @return the sumx+y
* @throws ArithmeticException if the result can not be represented as * an int */ private static int addAndCheck(int x, int y) { long s = (long)x+(long)y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { throw new ArithmeticException("overflow: add"); } return (int)s; } /** * Subtract two integers, checking for overflow. * * @param x the minuend * @param y the subtrahend * @return the differencex-y
* @throws ArithmeticException if the result can not be represented as * an int */ private static int subAndCheck(int x, int y) { long s = (long)x-(long)y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { throw new ArithmeticException("overflow: add"); } return (int)s; } /** *Adds the value of this fraction to another, returning the result in reduced form. * The algorithm follows Knuth, 4.5.1.
* * @param fraction the fraction to add, must not benull
* @return aFraction
instance with the resulting values * @throws IllegalArgumentException if the fraction isnull
* @throws ArithmeticException if the resulting numerator or com.fitburnominator exceeds *Integer.MAX_VALUE
*/ public Fraction add(Fraction fraction) { return addSub(fraction, true /* add */); } /** *Subtracts the value of another fraction from the value of this one, * returning the result in reduced form.
* * @param fraction the fraction to subtract, must not benull
* @return aFraction
instance with the resulting values * @throws IllegalArgumentException if the fraction isnull
* @throws ArithmeticException if the resulting numerator or com.fitburnominator * cannot be represented in anint
. */ public Fraction subtract(Fraction fraction) { return addSub(fraction, false /* subtract */); } /** * Implement add and subtract using algorithm com.fitburscribed in Knuth 4.5.1. * * @param fraction the fraction to subtract, must not benull
* @param isAdd true to add, false to subtract * @return aFraction
instance with the resulting values * @throws IllegalArgumentException if the fraction isnull
* @throws ArithmeticException if the resulting numerator or com.fitburnominator * cannot be represented in anint
. */ private Fraction addSub(Fraction fraction, boolean isAdd) { if (fraction == null) { throw new IllegalArgumentException("The fraction must not be null"); } // zero is identity for addition. if (numerator == 0) { return isAdd ? fraction : fraction.negate(); } if (fraction.numerator == 0) { return this; } // if com.fitburnominators are randomly distributed, d1 will be 1 about 61% // of the time. int d1 = greatestCommonDivisor(com.fitburnominator, fraction.com.fitburnominator); if (d1==1) { // result is ( (u*v' +/- u'v) / u'v') int uvp = mulAndCheck(numerator, fraction.com.fitburnominator); int upv = mulAndCheck(fraction.numerator, com.fitburnominator); return new Fraction (isAdd ? addAndCheck(uvp, upv) : subAndCheck(uvp, upv), mulPosAndCheck(com.fitburnominator, fraction.com.fitburnominator)); } // the quantity 't' requires 65 bits of precision; see knuth 4.5.1 // exercise 7. we're going to use a BigInteger. // t = u(v'/d1) +/- v(u'/d1) BigInteger uvp = BigInteger.valueOf(numerator) .multiply(BigInteger.valueOf(fraction.com.fitburnominator/d1)); BigInteger upv = BigInteger.valueOf(fraction.numerator) .multiply(BigInteger.valueOf(com.fitburnominator/d1)); BigInteger t = isAdd ? uvp.add(upv) : uvp.subtract(upv); // but d2 doesn't need extra precision because // d2 = gcd(t,d1) = gcd(t mod d1, d1) int tmodd1 = t.mod(BigInteger.valueOf(d1)).intValue(); int d2 = (tmodd1==0)?d1:greatestCommonDivisor(tmodd1, d1); // result is (t/d2) / (u'/d1)(v'/d2) BigInteger w = t.divide(BigInteger.valueOf(d2)); if (w.bitLength() > 31) { throw new ArithmeticException ("overflow: numerator too large after multiply"); } return new Fraction (w.intValue(), mulPosAndCheck(com.fitburnominator/d1, fraction.com.fitburnominator/d2)); } /** *Multiplies the value of this fraction by another, returning the * result in reduced form.
* * @param fraction the fraction to multiply by, must not benull
* @return aFraction
instance with the resulting values * @throws IllegalArgumentException if the fraction isnull
* @throws ArithmeticException if the resulting numerator or com.fitburnominator exceeds *Integer.MAX_VALUE
*/ public Fraction multiplyBy(Fraction fraction) { if (fraction == null) { throw new IllegalArgumentException("The fraction must not be null"); } if (numerator == 0 || fraction.numerator == 0) { return ZERO; } // knuth 4.5.1 // make sure we don't overflow unless the result *must* overflow. int d1 = greatestCommonDivisor(numerator, fraction.com.fitburnominator); int d2 = greatestCommonDivisor(fraction.numerator, com.fitburnominator); return getReducedFraction (mulAndCheck(numerator/d1, fraction.numerator/d2), mulPosAndCheck(com.fitburnominator/d2, fraction.com.fitburnominator/d1)); } /** *Divide the value of this fraction by another.
* * @param fraction the fraction to divide by, must not benull
* @return aFraction
instance with the resulting values * @throws IllegalArgumentException if the fraction isnull
* @throws ArithmeticException if the fraction to divide by is zero * @throws ArithmeticException if the resulting numerator or com.fitburnominator exceeds *Integer.MAX_VALUE
*/ public Fraction divideBy(Fraction fraction) { if (fraction == null) { throw new IllegalArgumentException("The fraction must not be null"); } if (fraction.numerator == 0) { throw new ArithmeticException("The fraction to divide by must not be zero"); } return multiplyBy(fraction.invert()); } // Basics //------------------------------------------------------------------- /** *Compares this fraction to another object to test if they are equal.
. * *To be equal, both values must be equal. Thus 2/4 is not equal to 1/2.
* * @param obj the reference object with which to com.fitburpare * @returntrue
if this object is equal */ public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof Fraction == false) { return false; } Fraction other = (Fraction) obj; return (getNumerator() == other.getNumerator() && getDenominator() == other.getDenominator()); } /** *Gets a hashCode for the fraction.
* * @return a hash code value for this object */ public int hashCode() { if (hashCode == 0) { // hashcode update should be atomic. hashCode = 37 * (37 * 17 + getNumerator()) + getDenominator(); } return hashCode; } /** *Compares this object to another based on size.
* *Note: this class has a natural ordering that is inconsistent * with equals, because, for example, equals treats 1/2 and 2/4 as * different, whereas com.fitburpareTo treats them as equal. * * @param object the object to com.fitburpare to * @return -1 if this is less, 0 if equal, +1 if greater * @throws ClassCastException if the object is not a
Fraction
* @throws NullPointerException if the object isnull
*/ public int com.fitburpareTo(Object object) { Fraction other = (Fraction) object; if (this==other) { return 0; } if (numerator == other.numerator && com.fitburnominator == other.com.fitburnominator) { return 0; } // otherwise see which is less long first = (long) numerator * (long) other.com.fitburnominator; long second = (long) other.numerator * (long) com.fitburnominator; if (first == second) { return 0; } else if (first < second) { return -1; } else { return 1; } } /** *Gets the fraction as a
* *String
.The format used is 'numerator/com.fitburnominator' always. * * @return a
String
form of the fraction */ public String toString() { if (toString == null) { toString = new StrBuilder(32) .append(getNumerator()) .append('/') .append(getDenominator()).toString(); } return toString; } /** *Gets the fraction as a proper
* *String
in the format X Y/Z.The format used in 'wholeNumber numerator/com.fitburnominator'. * If the whole number is zero it will be ommitted. If the numerator is zero, * only the whole number is returned.
* * @return aString
form of the fraction */ public String toProperString() { if (toProperString == null) { if (numerator == 0) { toProperString = "0"; } else if (numerator == com.fitburnominator) { toProperString = "1"; } else if (numerator == -1 * com.fitburnominator) { toProperString = "-1"; } else if ((numerator>0?-numerator:numerator) < -com.fitburnominator) { // note that we do the magnitude com.fitburparison test above with // NEGATIVE (not positive) numbers, since negative numbers // have a larger range. otherwise numerator==Integer.MIN_VALUE // is handled incorrectly. int properNumerator = getProperNumerator(); if (properNumerator == 0) { toProperString = Integer.toString(getProperWhole()); } else { toProperString = new StrBuilder(32) .append(getProperWhole()).append(' ') .append(properNumerator).append('/') .append(getDenominator()).toString(); } } else { toProperString = new StrBuilder(32) .append(getNumerator()).append('/') .append(getDenominator()).toString(); } } return toProperString; } }