Class BigRational
- java.lang.Object
-
- java.lang.Number
-
- edu.rit.numeric.BigRational
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Comparable<BigRational>
public class BigRational extends java.lang.Number implements java.lang.Comparable<BigRational>
Class BigRational provides an arbitrary precision rational number. An arbitrary precision rational number is the ratio of two arbitrary precision integers (type java.math.BigInteger). Operations are provided for exact arithmetic with rational numbers.A rational number is said to be normalized if GCD(numerator,denominator) = 1. The methods below do not automatically normalize the rational number. Thus, the numerator and denominator tend to get larger and larger as operations are performed on the rational number. To reduce the numerator and denominator to lowest terms again, call the normalize() method. It is up to you to decide whether to normalize the rational number after each operation, or after a series of operations.
Class BigRational provides the equals() and hashCode() methods, and BigRational objects can be used as keys in hashed data structures. However, BigRational objects are mutable. If a BigRational object is used as a hash key, be sure not to change its value.
Class BigRational is not multiple thread safe.
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description BigRational()Construct a new rational number.BigRational(java.math.BigInteger x)Construct a new rational number.BigRational(java.math.BigInteger n, java.math.BigInteger d)Construct a new rational number.BigRational(BigRational x)Construct a new rational number.BigRational(long x)Construct a new rational number.BigRational(long n, long d)Construct a new rational number.BigRational(java.lang.String s)Construct a new rational number.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description BigRationalabs()Set this rational number to the absolute value of itself.BigRationaladd(BigRational x)Set this rational number to the sum of itself and the given number.BigRationalassign(java.math.BigInteger x)Set this rational number to the given number.BigRationalassign(java.math.BigInteger n, java.math.BigInteger d)Set this rational number to the given fraction.BigRationalassign(BigRational x)Set this rational number to the given number.BigRationalassign(long x)Set this rational number to the given number.BigRationalassign(long n, long d)Set this rational number to the given fraction.BigRationalassign(java.lang.String s)Set this rational number to the value parsed from the given string.intcompareTo(BigRational x)Compare this rational number to the given rational number.BigRationaldecr()Decrement this rational number.java.math.BigIntegerdenominator()Returns this rational number's denominator.BigRationaldiv(BigRational x)Set this rational number to the quotient of itself and the given number.doubledoubleValue()Converts this rational number to a double precision floating point number.booleanequals(java.lang.Object obj)Determine if this rational number equals the given object.floatfloatValue()Converts this rational number to a single precision floating point number.BigRationalfracPart()Set this rational number to the fractional part of itself.inthashCode()Returns a hash code for this rational number.BigRationalincr()Increment this rational number.BigRationalintPart()Set this rational number to the integer part of itself.intintValue()Converts this rational number to an integer.longlongValue()Converts this rational number to a long integer.BigRationalmax(BigRational x)Set this rational number to the larger of itself and the given number.BigRationalmin(BigRational x)Set this rational number to the smaller of itself and the given number.BigRationalmul(BigRational x)Set this rational number to the product of itself and the given number.BigRationalnegate()Set this rational number to the negative of itself.BigRationalnormalize()Normalize this rational number.java.math.BigIntegernumerator()Returns this rational number's numerator.BigRationalrecip()Set this rational number to the reciprocal of itself.BigRationalrem(BigRational x)Set this rational number to the remainder when divided by the given number.BigRationalsub(BigRational x)Set this rational number to the difference of itself and the given number.java.lang.StringtoString()Returns a string version of this rational number.
-
-
-
Constructor Detail
-
BigRational
public BigRational()
Construct a new rational number. Its value is 0.
-
BigRational
public BigRational(long x)
Construct a new rational number. Its value is x.- Parameters:
x- Long integer.
-
BigRational
public BigRational(long n, long d)Construct a new rational number. Its value is n/d.- Parameters:
n- Numerator.d- Denominator.- Throws:
java.lang.ArithmeticException- (unchecked exception) Thrown if d is 0.
-
BigRational
public BigRational(java.math.BigInteger x)
Construct a new rational number. Its value is x.- Parameters:
x- Big integer.
-
BigRational
public BigRational(java.math.BigInteger n, java.math.BigInteger d)Construct a new rational number. Its value is n/d.- Parameters:
n- Numerator.d- Denominator.- Throws:
java.lang.ArithmeticException- (unchecked exception) Thrown if d is 0.
-
BigRational
public BigRational(BigRational x)
Construct a new rational number. Its value is x.- Parameters:
x- Rational number.
-
BigRational
public BigRational(java.lang.String s)
Construct a new rational number. Its value comes from the string s. The string must obey this syntax: optional minus sign (-), numerator (decimal integer), slash (/), denominator (decimal integer). The slash-and-denominator is optional. If present, the denominator must be greater than 0. There is no whitespace within the string.- Parameters:
s- String.- Throws:
java.lang.NumberFormatException- (unchecked exception) Thrown if s cannot be parsed into a rational number.
-
-
Method Detail
-
numerator
public java.math.BigInteger numerator()
Returns this rational number's numerator.- Returns:
- Numerator.
-
denominator
public java.math.BigInteger denominator()
Returns this rational number's denominator.- Returns:
- Denominator.
-
assign
public BigRational assign(long x)
Set this rational number to the given number.- Parameters:
x- Long integer.- Returns:
- This rational number, with its value set to x.
-
assign
public BigRational assign(long n, long d)
Set this rational number to the given fraction.- Parameters:
n- Numerator.d- Denominator.- Returns:
- This rational number, with its value set to n/d.
- Throws:
java.lang.ArithmeticException- (unchecked exception) Thrown if d is 0.
-
assign
public BigRational assign(java.math.BigInteger x)
Set this rational number to the given number.- Parameters:
x- Big integer.- Returns:
- This rational number, with its value set to x.
-
assign
public BigRational assign(java.math.BigInteger n, java.math.BigInteger d)
Set this rational number to the given fraction.- Parameters:
n- Numerator.d- Denominator.- Returns:
- This rational number, with its value set to n/d.
- Throws:
java.lang.ArithmeticException- (unchecked exception) Thrown if d is 0.
-
assign
public BigRational assign(BigRational x)
Set this rational number to the given number.- Parameters:
x- Rational number.- Returns:
- This rational number, with its value set to x.
-
assign
public BigRational assign(java.lang.String s)
Set this rational number to the value parsed from the given string. The string must obey this syntax: optional minus sign (-), numerator (decimal integer), slash (/), denominator (decimal integer). The slash-and-denominator is optional. If present, the denominator must be greater than 0. There is no whitespace within the string.- Parameters:
s- String.- Returns:
- This rational number, with its value set to s.
- Throws:
java.lang.NumberFormatException- (unchecked exception) Thrown if s cannot be parsed into a rational number.
-
intPart
public BigRational intPart()
Set this rational number to the integer part of itself.- Returns:
- This rational number, with its value set to int(this).
-
fracPart
public BigRational fracPart()
Set this rational number to the fractional part of itself.- Returns:
- This rational number, with its value set to frac(this).
-
add
public BigRational add(BigRational x)
Set this rational number to the sum of itself and the given number.- Parameters:
x- Rational number.- Returns:
- This rational number, with its value set to this+x.
-
sub
public BigRational sub(BigRational x)
Set this rational number to the difference of itself and the given number.- Parameters:
x- Rational number.- Returns:
- This rational number, with its value set to this-x.
-
mul
public BigRational mul(BigRational x)
Set this rational number to the product of itself and the given number.- Parameters:
x- Rational number.- Returns:
- This rational number, with its value set to this*x.
-
div
public BigRational div(BigRational x)
Set this rational number to the quotient of itself and the given number.- Parameters:
x- Rational number.- Returns:
- This rational number, with its value set to this/x.
- Throws:
java.lang.ArithmeticException- (unchecked exception) Thrown if x is 0.
-
rem
public BigRational rem(BigRational x)
Set this rational number to the remainder when divided by the given number.- Parameters:
x- Rational number.- Returns:
- This rational number, with its value set to frac(this/x)*x.
- Throws:
java.lang.ArithmeticException- (unchecked exception) Thrown if x is 0.
-
incr
public BigRational incr()
Increment this rational number.- Returns:
- This rational number, with its value set to this+1.
-
decr
public BigRational decr()
Decrement this rational number.- Returns:
- This rational number, with its value set to this-1.
-
abs
public BigRational abs()
Set this rational number to the absolute value of itself.- Returns:
- This rational number, with its value set to abs(this).
-
negate
public BigRational negate()
Set this rational number to the negative of itself.- Returns:
- This rational number, with its value set to -this.
-
recip
public BigRational recip()
Set this rational number to the reciprocal of itself.- Returns:
- This rational number, with its value set to 1/this.
- Throws:
java.lang.ArithmeticException- (unchecked exception) Thrown if this rational number is 0.
-
min
public BigRational min(BigRational x)
Set this rational number to the smaller of itself and the given number.- Parameters:
x- Rational number.- Returns:
- This rational number, with its value set to min(this,x).
-
max
public BigRational max(BigRational x)
Set this rational number to the larger of itself and the given number.- Parameters:
x- Rational number.- Returns:
- This rational number, with its value set to max(this,x).
-
normalize
public BigRational normalize()
Normalize this rational number. Afterwards, the denominator is greater than or equal to 1, and the denominator does not divide the numerator; in other words, GCD(numerator,denominator) = 1.- Returns:
- This rational number, normalized.
-
intValue
public int intValue()
Converts this rational number to an integer.- Specified by:
intValuein classjava.lang.Number- Returns:
- Integer value of this rational number.
-
longValue
public long longValue()
Converts this rational number to a long integer.- Specified by:
longValuein classjava.lang.Number- Returns:
- Long integer value of this rational number.
-
floatValue
public float floatValue()
Converts this rational number to a single precision floating point number.- Specified by:
floatValuein classjava.lang.Number- Returns:
- Single precision floating point value of this rational number.
-
doubleValue
public double doubleValue()
Converts this rational number to a double precision floating point number.- Specified by:
doubleValuein classjava.lang.Number- Returns:
- Double precision floating point value of this rational number.
-
compareTo
public int compareTo(BigRational x)
Compare this rational number to the given rational number.- Specified by:
compareToin interfacejava.lang.Comparable<BigRational>- Parameters:
x- Rational number to compare.- Returns:
- A number less than, equal to, or greater than 0 if this rational number is less than, equal to, or greater than x, respectively.
-
equals
public boolean equals(java.lang.Object obj)
Determine if this rational number equals the given object.Note: Class BigRational provides the equals() and hashCode() methods, and BigRational objects can be used as keys in hashed data structures. However, BigRational objects are mutable. If a BigRational object is used as a hash key, be sure not to change its value.
Note: Two rational numbers are equal only if their numerators and denominators are equal. Thus, it is possible for equals() to return false even if the two rational numbers have the same numerical value. To ensure that equals() will return true if the two rational numbers have the same numerical value, normalize the two rational numbers first.
- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- Object to compare.- Returns:
- True if this rational number equals obj, false otherwise.
-
hashCode
public int hashCode()
Returns a hash code for this rational number.Note: Class BigRational provides the equals() and hashCode() methods, and BigRational objects can be used as keys in hashed data structures. However, BigRational objects are mutable. If a BigRational object is used as a hash key, be sure not to change its value.
Note: Two rational numbers have the same hash code only if their numerators and denominators are equal. Thus, it is possible for hashCode() to return different values even if the two rational numbers have the same numerical value. To ensure that hashCode() will return the same value if the two rational numbers have the same numerical value, normalize the two rational numbers first.
- Overrides:
hashCodein classjava.lang.Object- Returns:
- Hash code.
-
toString
public java.lang.String toString()
Returns a string version of this rational number. If this rational number's denominator is 1, the string is in the form "<numer>", otherwise the string is in the form "<numer>/<denom>". The negative sign, if any, is attached to the numerator.Note: The toString() method yields the numerator and denominator as they are, without normalizing this rational number.
- Overrides:
toStringin classjava.lang.Object
-
-
DMelt 3.0 © DataMelt by jWork.ORG