|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.Number
edu.rit.numeric.BigRational
public class 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.
| Constructor Summary | |
|---|---|
BigRational()
Construct a new rational number. |
|
BigRational(BigInteger x)
Construct a new rational number. |
|
BigRational(BigInteger n,
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(String s)
Construct a new rational number. |
|
| Method Summary | |
|---|---|
BigRational |
abs()
Set this rational number to the absolute value of itself. |
BigRational |
add(BigRational x)
Set this rational number to the sum of itself and the given number. |
BigRational |
assign(BigInteger x)
Set this rational number to the given number. |
BigRational |
assign(BigInteger n,
BigInteger d)
Set this rational number to the given fraction. |
BigRational |
assign(BigRational x)
Set this rational number to the given number. |
BigRational |
assign(long x)
Set this rational number to the given number. |
BigRational |
assign(long n,
long d)
Set this rational number to the given fraction. |
BigRational |
assign(String s)
Set this rational number to the value parsed from the given string. |
int |
compareTo(BigRational x)
Compare this rational number to the given rational number. |
BigRational |
decr()
Decrement this rational number. |
BigInteger |
denominator()
Returns this rational number's denominator. |
BigRational |
div(BigRational x)
Set this rational number to the quotient of itself and the given number. |
double |
doubleValue()
Converts this rational number to a double precision floating point number. |
boolean |
equals(Object obj)
Determine if this rational number equals the given object. |
float |
floatValue()
Converts this rational number to a single precision floating point number. |
BigRational |
fracPart()
Set this rational number to the fractional part of itself. |
int |
hashCode()
Returns a hash code for this rational number. |
BigRational |
incr()
Increment this rational number. |
BigRational |
intPart()
Set this rational number to the integer part of itself. |
int |
intValue()
Converts this rational number to an integer. |
long |
longValue()
Converts this rational number to a long integer. |
BigRational |
max(BigRational x)
Set this rational number to the larger of itself and the given number. |
BigRational |
min(BigRational x)
Set this rational number to the smaller of itself and the given number. |
BigRational |
mul(BigRational x)
Set this rational number to the product of itself and the given number. |
BigRational |
negate()
Set this rational number to the negative of itself. |
BigRational |
normalize()
Normalize this rational number. |
BigInteger |
numerator()
Returns this rational number's numerator. |
BigRational |
recip()
Set this rational number to the reciprocal of itself. |
BigRational |
rem(BigRational x)
Set this rational number to the remainder when divided by the given number. |
BigRational |
sub(BigRational x)
Set this rational number to the difference of itself and the given number. |
String |
toString()
Returns a string version of this rational number. |
| Methods inherited from class java.lang.Number |
|---|
byteValue, shortValue |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public BigRational()
public BigRational(long x)
x - Long integer.
public BigRational(long n,
long d)
n - Numerator.d - Denominator.
ArithmeticException - (unchecked exception) Thrown if d is 0.public BigRational(BigInteger x)
x - Big integer.
public BigRational(BigInteger n,
BigInteger d)
n - Numerator.d - Denominator.
ArithmeticException - (unchecked exception) Thrown if d is 0.public BigRational(BigRational x)
x - Rational number.public BigRational(String s)
s - String.
NumberFormatException - (unchecked exception) Thrown if s cannot be parsed into a
rational number.| Method Detail |
|---|
public BigInteger numerator()
public BigInteger denominator()
public BigRational assign(long x)
x - Long integer.
public BigRational assign(long n,
long d)
n - Numerator.d - Denominator.
ArithmeticException - (unchecked exception) Thrown if d is 0.public BigRational assign(BigInteger x)
x - Big integer.
public BigRational assign(BigInteger n,
BigInteger d)
n - Numerator.d - Denominator.
ArithmeticException - (unchecked exception) Thrown if d is 0.public BigRational assign(BigRational x)
x - Rational number.
public BigRational assign(String s)
s - String.
NumberFormatException - (unchecked exception) Thrown if s cannot be parsed into a
rational number.public BigRational intPart()
public BigRational fracPart()
public BigRational add(BigRational x)
x - Rational number.
public BigRational sub(BigRational x)
x - Rational number.
public BigRational mul(BigRational x)
x - Rational number.
public BigRational div(BigRational x)
x - Rational number.
ArithmeticException - (unchecked exception) Thrown if x is 0.public BigRational rem(BigRational x)
x - Rational number.
ArithmeticException - (unchecked exception) Thrown if x is 0.public BigRational incr()
public BigRational decr()
public BigRational abs()
public BigRational negate()
public BigRational recip()
ArithmeticException - (unchecked exception) Thrown if this rational number is 0.public BigRational min(BigRational x)
x - Rational number.
public BigRational max(BigRational x)
x - Rational number.
public BigRational normalize()
public int intValue()
intValue in class Numberpublic long longValue()
longValue in class Numberpublic float floatValue()
floatValue in class Numberpublic double doubleValue()
doubleValue in class Numberpublic int compareTo(BigRational x)
compareTo in interface Comparable<BigRational>x - Rational number to compare.
public boolean equals(Object obj)
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.
equals in class Objectobj - Object to compare.
public int hashCode()
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.
hashCode in class Objectpublic String toString()
Note: The toString() method yields the numerator and denominator as they are, without normalizing this rational number.
toString in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||