## Class BigRational

- java.lang.Object
- java.lang.Number
- edu.rit.numeric.BigRational

- All Implemented Interfaces:
- Serializable, Comparable<BigRational>

public class BigRationalextends Numberimplements 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 doautomatically 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*not*`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**(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

Methods Modifier and Type Method and Description `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`

### 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:
`ArithmeticException`

- (unchecked exception) Thrown if`d`is 0.

#### BigRational

public BigRational(BigInteger x)

Construct a new rational number. Its value is`x`.- Parameters:
`x`

- Big integer.

#### BigRational

public BigRational(BigInteger n, BigInteger d)

Construct a new rational number. Its value is`n/d`.- Parameters:
`n`

- Numerator.`d`

- Denominator.- Throws:
`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(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:
`NumberFormatException`

- (unchecked exception) Thrown if`s`cannot be parsed into a rational number.

### Method Detail

#### numerator

public BigInteger numerator()

Returns this rational number's numerator.- Returns:
- Numerator.

#### denominator

public 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:
`ArithmeticException`

- (unchecked exception) Thrown if`d`is 0.

#### assign

public BigRational assign(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(BigInteger n, 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:
`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(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:
`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:
`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:
`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:
`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.

#### longValue

public long longValue()

Converts this rational number to a long integer.

#### floatValue

public float floatValue()

Converts this rational number to a single precision floating point number.**Specified by:**`floatValue`

in class`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:**`doubleValue`

in class`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:**`compareTo`

in interface`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(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.

#### 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.

#### toString

public 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.

**SCaVis 2.0 © jWork.ORG**