numerator/denominator
.
*
* Note that any value with a numerator of zero will be treated as zero, even if the
* denominator is also zero.
*
* @author Drew Noakes https://drewnoakes.com
*/
@SuppressWarnings("WeakerAccess")
public class Rational extends java.lang.Number implements Comparabledouble
.
* This may involve rounding.
*
* @return the numeric value represented by this object after conversion
* to type double
.
*/
@Override
public double doubleValue()
{
return _numerator == 0
? 0.0
: (double) _numerator / (double) _denominator;
}
/**
* Returns the value of the specified number as a float
.
* This may involve rounding.
*
* @return the numeric value represented by this object after conversion
* to type float
.
*/
@Override
public float floatValue()
{
return _numerator == 0
? 0.0f
: (float) _numerator / (float) _denominator;
}
/**
* Returns the value of the specified number as a byte
.
* This may involve rounding or truncation. This implementation simply
* casts the result of {@link Rational#doubleValue} to byte
.
*
* @return the numeric value represented by this object after conversion
* to type byte
.
*/
@Override
public final byte byteValue()
{
return (byte) doubleValue();
}
/**
* Returns the value of the specified number as an int
.
* This may involve rounding or truncation. This implementation simply
* casts the result of {@link Rational#doubleValue} to int
.
*
* @return the numeric value represented by this object after conversion
* to type int
.
*/
@Override
public final int intValue()
{
return (int) doubleValue();
}
/**
* Returns the value of the specified number as a long
.
* This may involve rounding or truncation. This implementation simply
* casts the result of {@link Rational#doubleValue} to long
.
*
* @return the numeric value represented by this object after conversion
* to type long
.
*/
@Override
public final long longValue()
{
return (long) doubleValue();
}
/**
* Returns the value of the specified number as a short
.
* This may involve rounding or truncation. This implementation simply
* casts the result of {@link Rational#doubleValue} to short
.
*
* @return the numeric value represented by this object after conversion
* to type short
.
*/
@Override
public final short shortValue()
{
return (short) doubleValue();
}
/** Returns the denominator. */
public final long getDenominator()
{
return this._denominator;
}
/** Returns the numerator. */
public final long getNumerator()
{
return this._numerator;
}
/**
* Returns the reciprocal value of this object as a new Rational.
*
* @return the reciprocal in a new object
*/
@NotNull
public Rational getReciprocal()
{
return new Rational(this._denominator, this._numerator);
}
/** Checks if this {@link Rational} number is an Integer, either positive or negative. */
public boolean isInteger()
{
return _denominator == 1 ||
(_denominator != 0 && (_numerator % _denominator == 0)) ||
(_denominator == 0 && _numerator == 0);
}
/** Checks if either the numerator or denominator are zero. */
public boolean isZero()
{
return _numerator == 0 || _denominator == 0;
}
/**
* Returns a string representation of the object of form numerator/denominator
.
*
* @return a string representation of the object.
*/
@Override
@NotNull
public String toString()
{
return _numerator + "/" + _denominator;
}
/** Returns the simplest representation of this {@link Rational}'s value possible. */
@NotNull
public String toSimpleString(boolean allowDecimal)
{
if (_denominator == 0 && _numerator != 0) {
return toString();
} else if (isInteger()) {
return Integer.toString(intValue());
} else if (_numerator != 1 && _denominator % _numerator == 0) {
// common factor between denominator and numerator
long newDenominator = _denominator / _numerator;
return new Rational(1, newDenominator).toSimpleString(allowDecimal);
} else {
Rational simplifiedInstance = getSimplifiedInstance();
if (allowDecimal) {
String doubleString = Double.toString(simplifiedInstance.doubleValue());
if (doubleString.length() < 5) {
return doubleString;
}
}
return simplifiedInstance.toString();
}
}
/**
* Compares two {@link Rational} instances, returning true if they are mathematically
* equivalent (in consistence with {@link Rational#equals(Object)} method).
*
* @param that the {@link Rational} to compare this instance to.
* @return the value {@code 0} if this {@link Rational} is
* equal to the argument {@link Rational} mathematically; a value less
* than {@code 0} if this {@link Rational} is less
* than the argument {@link Rational}; and a value greater
* than {@code 0} if this {@link Rational} is greater than the argument
* {@link Rational}.
*/
public int compareTo(@NotNull Rational that) {
return Double.compare(this.doubleValue(), that.doubleValue());
}
/**
* Indicates whether this instance and other
are numerically equal,
* even if their representations differ.
*
* For example, 1/2 is equal to 10/20 by this method.
* Similarly, 1/0 is equal to 100/0 by this method.
* To test equal representations, use EqualsExact.
*
* @param other The rational value to compare with
*/
public boolean equals(Rational other) {
return other.doubleValue() == doubleValue();
}
/**
* Indicates whether this instance and other
have identical
* Numerator and Denominator.
* * For example, 1/2 is not equal to 10/20 by this method. * Similarly, 1/0 is not equal to 100/0 by this method. * To test numerically equivalence, use Equals(Rational).
* * @param other The rational value to compare with */ public boolean equalsExact(Rational other) { return getDenominator() == other.getDenominator() && getNumerator() == other.getNumerator(); } /** * Compares two {@link Rational} instances, returning true if they are mathematically * equivalent. * * @param obj the {@link Rational} to compare this instance to. * @return true if instances are mathematically equivalent, otherwise false. Will also * return false ifobj
is not an instance of {@link Rational}.
*/
@Override
public boolean equals(@Nullable Object obj)
{
if (obj==null || !(obj instanceof Rational))
return false;
Rational that = (Rational) obj;
return this.doubleValue() == that.doubleValue();
}
@Override
public int hashCode()
{
return (23 * (int)_denominator) + (int)_numerator;
}
/**
* * Simplifies the representation of this {@link Rational} number.
** For example, 5/10 simplifies to 1/2 because both Numerator * and Denominator share a common factor of 5.
** Uses the Euclidean Algorithm to find the greatest common divisor.
* * @return A simplified instance if one exists, otherwise a copy of the original value. */ @NotNull public Rational getSimplifiedInstance() { long gcd = GCD(_numerator, _denominator); return new Rational(_numerator / gcd, _denominator / gcd); } private static long GCD(long a, long b) { if (a < 0) a = -a; if (b < 0) b = -b; while (a != 0 && b != 0) { if (a > b) a %= b; else b %= a; } return a == 0 ? b : a; } }