Package org.jscience.mathematics.number
Class Rational
- java.lang.Object
-
- java.lang.Number
-
- org.jscience.mathematics.number.Number<Rational>
-
- org.jscience.mathematics.number.Rational
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<Rational>,javolution.lang.Immutable,javolution.lang.Realtime,javolution.lang.ValueType,javolution.xml.XMLSerializable,Field<Rational>,GroupAdditive<Rational>,GroupMultiplicative<Rational>,Ring<Rational>,Structure<Rational>
public final class Rational extends Number<Rational> implements Field<Rational>
This class represents the ratio of two
LargeIntegernumbers.Instances of this class are immutable and can be used to find exact solutions to linear equations with the
Matrixclass.- Version:
- 3.0, February 13, 2006
- Author:
- Jean-Marie Dautelle
- See Also:
- Wikipedia: Rational Numbers, Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Rationalabs()Returns the absolute value of this rational number.intcompareTo(Rational that)Compares two rational number numerically.Rationalcopy()Returns a copy of this numberallocatedby the calling thread (possibly on the stack).Rationaldivide(Rational that)Returns this rational number divided by the one specified.doubledoubleValue()Returns the value of this rational number as adouble.booleanequals(java.lang.Object that)Compares this rational number against the specified object.LargeIntegergetDividend()Returns the smallest dividend of the fraction representing this rational number.LargeIntegergetDivisor()Returns the smallest divisor of the fraction representing this rational (always positive).inthashCode()Returns the hash code for this rational number.Rationalinverse()Returns the inverse of this rational number.booleanisLargerThan(Rational that)Compares the absolute value of two rational numbers.booleanisNegative()Indicates if this rational number is less than zero.booleanisPositive()Indicates if this rational number is greater than zero.booleanisZero()Indicates if this rational number is equal to zero.longlongValue()Returns the value of this rational number as along.Rationalminus(Rational that)Returns the difference between this rational number and the one specified.Rationalopposite()Returns the opposite of this rational number.Rationalplus(Rational that)Returns the sum of this rational number with the one specified.LargeIntegerround()Returns the closest integer value to this rational number.Rationaltimes(long multiplier)Returns the product of this rational number with the specifiedlongmultiplier.Rationaltimes(Rational that)Returns the product of this rational number with the one specified.javolution.text.TexttoText()Returns the decimal text representation of this number.static RationalvalueOf(long dividend, long divisor)Returns the rational number for the specified integer dividend and divisor.static RationalvalueOf(java.lang.CharSequence chars)Returns the rational number for the specified character sequence.static RationalvalueOf(LargeInteger dividend, LargeInteger divisor)Returns the rational number for the specified large integer dividend and divisor.-
Methods inherited from class org.jscience.mathematics.number.Number
byteValue, floatValue, intValue, isGreaterThan, isLessThan, pow, shortValue, toString
-
-
-
-
Method Detail
-
valueOf
public static Rational valueOf(long dividend, long divisor)
Returns the rational number for the specified integer dividend and divisor.- Parameters:
dividend- the dividend value.divisor- the divisor value.- Returns:
dividend / divisor- Throws:
java.lang.ArithmeticException- ifdivisor == 0
-
valueOf
public static Rational valueOf(LargeInteger dividend, LargeInteger divisor)
Returns the rational number for the specified large integer dividend and divisor.- Parameters:
dividend- the dividend value.divisor- the divisor value.- Returns:
dividend / divisor- Throws:
java.lang.ArithmeticException- ifdivisor.isZero()
-
valueOf
public static Rational valueOf(java.lang.CharSequence chars)
Returns the rational number for the specified character sequence.- Parameters:
chars- the character sequence.- Returns:
- the corresponding rational number.
-
getDividend
public LargeInteger getDividend()
Returns the smallest dividend of the fraction representing this rational number.- Returns:
- this rational dividend.
-
getDivisor
public LargeInteger getDivisor()
Returns the smallest divisor of the fraction representing this rational (always positive).- Returns:
- this rational divisor.
-
round
public LargeInteger round()
Returns the closest integer value to this rational number.- Returns:
- this rational rounded to the nearest integer.
-
opposite
public Rational opposite()
Returns the opposite of this rational number.- Specified by:
oppositein interfaceGroupAdditive<Rational>- Returns:
-this.
-
plus
public Rational plus(Rational that)
Returns the sum of this rational number with the one specified.- Specified by:
plusin interfaceGroupAdditive<Rational>- Parameters:
that- the rational number to be added.- Returns:
this + that.
-
minus
public Rational minus(Rational that)
Returns the difference between this rational number and the one specified.
-
times
public Rational times(long multiplier)
Returns the product of this rational number with the specifiedlongmultiplier.- Parameters:
multiplier- thelongmultiplier.- Returns:
this · multiplier.
-
times
public Rational times(Rational that)
Returns the product of this rational number with the one specified.
-
inverse
public Rational inverse()
Returns the inverse of this rational number.- Specified by:
inversein interfaceGroupMultiplicative<Rational>- Returns:
1 / this.- Throws:
java.lang.ArithmeticException- ifdividend.isZero()
-
divide
public Rational divide(Rational that)
Returns this rational number divided by the one specified.- Parameters:
that- the rational number divisor.- Returns:
this / that.- Throws:
java.lang.ArithmeticException- ifthat.equals(ZERO)
-
isZero
public boolean isZero()
Indicates if this rational number is equal to zero.- Returns:
this == 0
-
isPositive
public boolean isPositive()
Indicates if this rational number is greater than zero.- Returns:
this > 0
-
isNegative
public boolean isNegative()
Indicates if this rational number is less than zero.- Returns:
this < 0
-
isLargerThan
public boolean isLargerThan(Rational that)
Compares the absolute value of two rational numbers.- Specified by:
isLargerThanin classNumber<Rational>- Parameters:
that- the rational number to be compared with.- Returns:
|this| > |that|
-
toText
public javolution.text.Text toText()
Returns the decimal text representation of this number.
-
equals
public boolean equals(java.lang.Object that)
Compares this rational number against the specified object.
-
hashCode
public int hashCode()
Returns the hash code for this rational number.
-
longValue
public long longValue()
Returns the value of this rational number as along.
-
doubleValue
public double doubleValue()
Returns the value of this rational number as adouble.- Specified by:
doubleValuein classNumber<Rational>- Returns:
- the numeric value represented by this rational after conversion
to type
double.
-
-