Package com.drew.lang
Class Rational
java.lang.Object
java.lang.Number
com.drew.lang.Rational
- All Implemented Interfaces:
Serializable,Comparable<Rational>
Immutable class for holding a rational number without loss of precision. Provides
a familiar representation via
toString() in form 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
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionRational(long numerator, long denominator) Creates a new instance of Rational. -
Method Summary
Modifier and TypeMethodDescriptionfinal byteReturns the value of the specified number as abyte.intCompares twoRationalinstances, returning true if they are mathematically equivalent (in consistence withequals(Object)method).doubleReturns the value of the specified number as adouble.booleanIndicates whether this instance andotherare numerically equal, even if their representations differ.booleanCompares twoRationalinstances, returning true if they are mathematically equivalent.booleanequalsExact(Rational other) Indicates whether this instance andotherhave identical Numerator and Denominator.floatReturns the value of the specified number as afloat.Returns the absolute value of this object as a new Rational.final longReturns the denominator.final longReturns the numerator.Returns the reciprocal value of this object as a new Rational.Simplifies the representation of thisRationalnumber.inthashCode()final intintValue()Returns the value of the specified number as anint.booleanChecks if thisRationalnumber is an Integer, either positive or negative.booleanTrue if the value is non-zero and numerator and denominator are either both positive or both negative.booleanisZero()Checks if either the numerator or denominator are zero.final longReturns the value of the specified number as along.final shortReturns the value of the specified number as ashort.toSimpleString(boolean allowDecimal) Returns the simplest representation of thisRational's value possible.toString()Returns a string representation of the object of formnumerator/denominator.
-
Constructor Details
-
Rational
public Rational(long numerator, long denominator) Creates a new instance of Rational. Rational objects are immutable, so once you've set your numerator and denominator values here, you're stuck with them!
-
-
Method Details
-
doubleValue
public double doubleValue()Returns the value of the specified number as adouble. This may involve rounding.- Specified by:
doubleValuein classNumber- Returns:
- the numeric value represented by this object after conversion
to type
double.
-
floatValue
public float floatValue()Returns the value of the specified number as afloat. This may involve rounding.- Specified by:
floatValuein classNumber- Returns:
- the numeric value represented by this object after conversion
to type
float.
-
byteValue
public final byte byteValue()Returns the value of the specified number as abyte. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()tobyte. -
intValue
public final int intValue()Returns the value of the specified number as anint. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()toint. -
longValue
public final long longValue()Returns the value of the specified number as along. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()tolong. -
shortValue
public final short shortValue()Returns the value of the specified number as ashort. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()toshort.- Overrides:
shortValuein classNumber- Returns:
- the numeric value represented by this object after conversion
to type
short.
-
getDenominator
public final long getDenominator()Returns the denominator. -
getNumerator
public final long getNumerator()Returns the numerator. -
getReciprocal
Returns the reciprocal value of this object as a new Rational.- Returns:
- the reciprocal in a new object
-
getAbsolute
Returns the absolute value of this object as a new Rational.- Returns:
- the absolute value in a new object
-
isInteger
public boolean isInteger()Checks if thisRationalnumber is an Integer, either positive or negative. -
isZero
public boolean isZero()Checks if either the numerator or denominator are zero. -
isPositive
public boolean isPositive()True if the value is non-zero and numerator and denominator are either both positive or both negative. -
toString
Returns a string representation of the object of formnumerator/denominator. -
toSimpleString
Returns the simplest representation of thisRational's value possible. -
compareTo
Compares twoRationalinstances, returning true if they are mathematically equivalent (in consistence withequals(Object)method).- Specified by:
compareToin interfaceComparable<Rational>- Parameters:
that- theRationalto compare this instance to.- Returns:
- the value
0if thisRationalis equal to the argumentRationalmathematically; a value less than0if thisRationalis less than the argumentRational; and a value greater than0if thisRationalis greater than the argumentRational.
-
equals
Indicates whether this instance andotherare 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.- Parameters:
other- The rational value to compare with
-
equalsExact
Indicates whether this instance andotherhave 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).
- Parameters:
other- The rational value to compare with
-
equals
Compares twoRationalinstances, returning true if they are mathematically equivalent. -
hashCode
public int hashCode() -
getSimplifiedInstance
Simplifies the representation of this
Rationalnumber.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.
- Returns:
- A simplified instance if one exists, otherwise a copy of the original value.
-