org.jscience.mathematics.algebraic.numbers
Class BigRational

java.lang.Object
  extended by java.lang.Number
      extended by org.jscience.mathematics.algebraic.numbers.BigRational
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable

public final class BigRational
extends java.lang.Number
implements java.lang.Comparable, java.io.Serializable

BigRational: dynamically sized immutable arbitrary-precision rational numbers.

See Also:
Serialized Form

Field Summary
static int DEFAULT_RADIX
          Default radix, used in string printing and scanning, 10.
static int DEFAULT_ROUND_MODE
          Default round mode, ROUND_HALF_UP.
static BigRational MINUS_ONE
          The constant minus-one (-1).
static BigRational ONE
          The constant one (1).
static int ROUND_CEILING
          Rounding mode to round towards positive infinity.
static int ROUND_DOWN
          Rounding mode to round towards zero.
static int ROUND_FLOOR
          Rounding mode to round towards negative infinity.
static int ROUND_HALF_CEILING
          Rounding mode to round towards nearest neighbor unless both neighbors are equidistant, in which case to round ceiling.
static int ROUND_HALF_DOWN
          Rounding mode to round towards nearest neighbor unless both neighbors are equidistant, in which case to round down.
static int ROUND_HALF_EVEN
          Rounding mode to round towards the nearest neighbor unless both neighbors are equidistant, in which case to round towards the even neighbor.
static int ROUND_HALF_FLOOR
          Rounding mode to round towards nearest neighbor unless both neighbors are equidistant, in which case to round floor.
static int ROUND_HALF_ODD
          Rounding mode to round towards the nearest neighbor unless both neighbors are equidistant, in which case to round towards the odd neighbor.
static int ROUND_HALF_UP
          Rounding mode to round towards nearest neighbor unless both neighbors are equidistant, in which case to round up.
static int ROUND_UNNECESSARY
          Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
static int ROUND_UP
          Rounding mode to round away from zero.
static BigRational ZERO
          The constant zero (0).
 
Constructor Summary
BigRational(java.math.BigInteger n)
          Construct a BigRational from a big number integer, denominator is 1.
BigRational(java.math.BigInteger n, java.math.BigInteger q)
          Construct a BigRational from numerator and denominator.
BigRational(java.math.BigInteger unscaledValue, int scale)
          Construct a BigRational from an unscaled value by scaling, default radix.
BigRational(java.math.BigInteger unscaledValue, int scale, int radix)
          Construct a BigRational from an unscaled value by scaling.
BigRational(long n)
          Construct a BigRational from a long fix number integer.
BigRational(long unscaledValue, int scale, int radix)
          Construct a BigRational from an unscaled fix number value by scaling.
BigRational(long n, long q)
          Construct a BigRational from long fix number integers representing numerator and denominator.
BigRational(java.lang.String s)
          Construct a BigRational from a string representation, with default radix, the supported formats are "[+-]d/[+-]q", "[+-]i.f", "[+-]i".
BigRational(java.lang.String s, int radix)
          Construct a BigRational from a string representation, the supported formats are "[+-]d/[+-]q", "[+-]i.f", "[+-]i".
 
Method Summary
 BigRational abs()
          Return a new BigRational with the absolute value of this.
 BigRational add(BigRational that)
          Add two BigRationals and return a new BigRational.
 BigRational add(long that)
          Add a BigRational and a long fix number integer and return a new BigRational.
 java.math.BigDecimal bigDecimalValue(int precision)
          Convert to BigDecimal
 java.math.BigInteger bigIntegerValue()
          Convert to BigInteger, by rounding.
 BigRational ceil()
          Ceiling, round towards positive infinity.
 int compareTo(java.math.BigInteger that)
          Compare to BigInteger.
 int compareTo(BigRational that)
          Compare two BigRationals.
 int compareTo(long that)
          Compare to long.
 int compareTo(java.lang.Object object)
          Compare object (BigRational/BigInteger/Long/Integer).
 BigRational divide(BigRational that)
          Divide a BigRational (this) through another and return a new BigRational.
 BigRational divide(long that)
          Divide a BigRational (this) through a long fix number integer and return a new BigRational.
 double doubleValue()
          Convert to double Implements Number.doubleValue().
 boolean equals(java.lang.Object object)
          Compare object for equality.
 float floatValue()
          Convert to floatl.
 BigRational floor()
          Floor, round towards negative infinity.
 BigRational fractionalPart()
          Fractional part.
 java.math.BigInteger getDenominator()
          Retrieves the denominator as a BigInteger.
 java.math.BigInteger getNumerator()
          Retrieves the numerator as a BigInteger.
 int hashCode()
          Hash code.
 BigRational[] integerAndFractionalPart()
          Return an array of BigRationals with both integer and fractional part.
 BigRational integerPart()
          Integer part.
 int intValue()
          Convert to int, by rounding and delegating to BigInteger.
 BigRational invert()
          Return a new BigRational with the inverted (reciprocal) value of this.
 long longValue()
          Convert to long, by rounding and delegating to BigInteger.
 BigRational max(BigRational that)
          Return the maximal value of two BigRationals.
 BigRational max(long that)
          Return the maximum value of a BigRational and a long fix number integer.
 BigRational min(BigRational that)
          Return the minimal value of two BigRationals.
 BigRational min(long that)
          Return the minimal value of a BigRational and a long fix number integer.
 BigRational mod(BigRational that)
          Calculate the modulus of two BigRationals and return a new BigRational.
 BigRational mod(long that)
          Calculate the modulus of a BigRational and a long fix number integer and return a new BigRational.
 BigRational multiply(BigRational that)
          Multiply two BigRationals and return a new BigRational.
 BigRational multiply(long that)
          Multiply a long fix number integer to this and return a new BigRational.
 BigRational negate()
          Return a new BigRational with the negative value of this.
 BigRational pow(int exponent)
          Calculate a BigRational's integer power and return a new BigRational.
 BigRational remainder(BigRational that)
          Calculate the remainder of two BigRationals and return a new BigRational.
 BigRational remainder(long that)
          Calculate the remainder of a BigRational and a long fix number integer and return a new BigRational.
 BigRational round()
          Round by default mode.
 BigRational round(int roundMode)
          Round.
 int signum()
          Signum, -1, 0, or 1.
 BigRational subtract(BigRational that)
          Subtract a BigRational from another (this) and return a new BigRational.
 BigRational subtract(long that)
          Subtract a long fix number integer from this and return a new BigRational.
 java.lang.String toString()
          BigRational string representation, format "[-]d[/q]", default radix.
 java.lang.String toString(int radix)
          BigRational string representation, format "[-]d[/q]".
 java.lang.String toStringDot(int precision)
          Dot-format "[-]i.f" string representation, with a precision, default radix Precision may be negative.
 java.lang.String toStringDot(int precision, int radix)
          Dot-format "[-]i.f" string representation, with a precision.
 BigRational truncate()
          Truncate, round towards zero.
static BigRational valueOf(java.math.BigInteger value)
          Manifactor a BigRational from a BigInteger.
static BigRational valueOf(long value)
          Manifactor a BigRational from a long fix number integer.
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_RADIX

public static final int DEFAULT_RADIX
Default radix, used in string printing and scanning, 10. Default radix is decimal, of course.

See Also:
Constant Field Values

ZERO

public static final BigRational ZERO
The constant zero (0). [Constant name: see class BigInteger.]


ONE

public static final BigRational ONE
The constant one (1). [Name: see class BigInteger.]


MINUS_ONE

public static final BigRational MINUS_ONE
The constant minus-one (-1).


ROUND_UP

public static final int ROUND_UP
Rounding mode to round away from zero.

See Also:
Constant Field Values

ROUND_DOWN

public static final int ROUND_DOWN
Rounding mode to round towards zero.

See Also:
Constant Field Values

ROUND_CEILING

public static final int ROUND_CEILING
Rounding mode to round towards positive infinity.

See Also:
Constant Field Values

ROUND_FLOOR

public static final int ROUND_FLOOR
Rounding mode to round towards negative infinity.

See Also:
Constant Field Values

ROUND_HALF_UP

public static final int ROUND_HALF_UP
Rounding mode to round towards nearest neighbor unless both neighbors are equidistant, in which case to round up.

See Also:
Constant Field Values

ROUND_HALF_DOWN

public static final int ROUND_HALF_DOWN
Rounding mode to round towards nearest neighbor unless both neighbors are equidistant, in which case to round down.

See Also:
Constant Field Values

ROUND_HALF_EVEN

public static final int ROUND_HALF_EVEN
Rounding mode to round towards the nearest neighbor unless both neighbors are equidistant, in which case to round towards the even neighbor.

See Also:
Constant Field Values

ROUND_UNNECESSARY

public static final int ROUND_UNNECESSARY
Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary. If this rounding mode is specified on an operation that yields an inexact result, an ArithmeticException is thrown.

See Also:
Constant Field Values

ROUND_HALF_CEILING

public static final int ROUND_HALF_CEILING
Rounding mode to round towards nearest neighbor unless both neighbors are equidistant, in which case to round ceiling.

See Also:
Constant Field Values

ROUND_HALF_FLOOR

public static final int ROUND_HALF_FLOOR
Rounding mode to round towards nearest neighbor unless both neighbors are equidistant, in which case to round floor.

See Also:
Constant Field Values

ROUND_HALF_ODD

public static final int ROUND_HALF_ODD
Rounding mode to round towards the nearest neighbor unless both neighbors are equidistant, in which case to round towards the odd neighbor.

See Also:
Constant Field Values

DEFAULT_ROUND_MODE

public static final int DEFAULT_ROUND_MODE
Default round mode, ROUND_HALF_UP.

See Also:
Constant Field Values
Constructor Detail

BigRational

public BigRational(java.math.BigInteger n,
                   java.math.BigInteger q)
Construct a BigRational from numerator and denominator. Both n and q may be negative. n/q may be denormalized.


BigRational

public BigRational(java.math.BigInteger n)
Construct a BigRational from a big number integer, denominator is 1.


BigRational

public BigRational(long n,
                   long q)
Construct a BigRational from long fix number integers representing numerator and denominator.


BigRational

public BigRational(long n)
Construct a BigRational from a long fix number integer.


BigRational

public BigRational(java.lang.String s,
                   int radix)
Construct a BigRational from a string representation, the supported formats are "[+-]d/[+-]q", "[+-]i.f", "[+-]i".


BigRational

public BigRational(java.lang.String s)
Construct a BigRational from a string representation, with default radix, the supported formats are "[+-]d/[+-]q", "[+-]i.f", "[+-]i".


BigRational

public BigRational(java.math.BigInteger unscaledValue,
                   int scale,
                   int radix)
Construct a BigRational from an unscaled value by scaling.


BigRational

public BigRational(java.math.BigInteger unscaledValue,
                   int scale)
Construct a BigRational from an unscaled value by scaling, default radix.


BigRational

public BigRational(long unscaledValue,
                   int scale,
                   int radix)
Construct a BigRational from an unscaled fix number value by scaling.

Method Detail

toString

public java.lang.String toString(int radix)
BigRational string representation, format "[-]d[/q]".


toString

public java.lang.String toString()
BigRational string representation, format "[-]d[/q]", default radix. Default string representation. Overwrites Object.toString().

Overrides:
toString in class java.lang.Object

toStringDot

public java.lang.String toStringDot(int precision,
                                    int radix)
Dot-format "[-]i.f" string representation, with a precision. Precision may be negative.


toStringDot

public java.lang.String toStringDot(int precision)
Dot-format "[-]i.f" string representation, with a precision, default radix Precision may be negative.


getNumerator

public java.math.BigInteger getNumerator()
Retrieves the numerator as a BigInteger.


getDenominator

public java.math.BigInteger getDenominator()
Retrieves the denominator as a BigInteger.


add

public BigRational add(BigRational that)
Add two BigRationals and return a new BigRational. [Name: see class BigInteger.]


add

public BigRational add(long that)
Add a BigRational and a long fix number integer and return a new BigRational.


subtract

public BigRational subtract(BigRational that)
Subtract a BigRational from another (this) and return a new BigRational. [Name: see class BigInteger.]


subtract

public BigRational subtract(long that)
Subtract a long fix number integer from this and return a new BigRational.


multiply

public BigRational multiply(BigRational that)
Multiply two BigRationals and return a new BigRational. [Name: see class BigInteger.]


multiply

public BigRational multiply(long that)
Multiply a long fix number integer to this and return a new BigRational.


divide

public BigRational divide(BigRational that)
Divide a BigRational (this) through another and return a new BigRational. [Name: see class BigInteger.]


divide

public BigRational divide(long that)
Divide a BigRational (this) through a long fix number integer and return a new BigRational.


pow

public BigRational pow(int exponent)
Calculate a BigRational's integer power and return a new BigRational. The integer exponent may be negative.


remainder

public BigRational remainder(BigRational that)
Calculate the remainder of two BigRationals and return a new BigRational. [Name: see class BigInteger.] The remainder result may be negative. The remainder is based on round down (towards zero) / truncate. 5/3 == 1 + 2/3 (remainder 2), 5/-3 == -1 + 2/-3 (remainder 2), -5/3 == -1 + -2/3 (remainder -2), -5/-3 == 1 + -2/-3 (remainder -2).


remainder

public BigRational remainder(long that)
Calculate the remainder of a BigRational and a long fix number integer and return a new BigRational.


mod

public BigRational mod(BigRational that)
Calculate the modulus of two BigRationals and return a new BigRational. The modulus result may be negative. Modulus is based on round floor (towards negative). 5/3 == 1 + 2/3 (modulus 2), 5/-3 == -2 + -1/-3 (modulus -1), -5/3 == -2 + 1/3 (modulus 1), -5/-3 == 1 + -2/-3 (modulus -2).


mod

public BigRational mod(long that)
Calculate the modulus of a BigRational and a long fix number integer and return a new BigRational.


signum

public int signum()
Signum, -1, 0, or 1. [Name: see class BigInteger.]


abs

public BigRational abs()
Return a new BigRational with the absolute value of this.


negate

public BigRational negate()
Return a new BigRational with the negative value of this. [Name: see class BigInteger.]


invert

public BigRational invert()
Return a new BigRational with the inverted (reciprocal) value of this.


min

public BigRational min(BigRational that)
Return the minimal value of two BigRationals.


min

public BigRational min(long that)
Return the minimal value of a BigRational and a long fix number integer.


max

public BigRational max(BigRational that)
Return the maximal value of two BigRationals.


max

public BigRational max(long that)
Return the maximum value of a BigRational and a long fix number integer.


equals

public boolean equals(java.lang.Object object)
Compare object for equality. Overwrites Object.equals(). Only some object types are allowed (see compareTo). Never throws.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Hash code. Overwrites Object.hashCode().

Overrides:
hashCode in class java.lang.Object

compareTo

public int compareTo(BigRational that)
Compare two BigRationals.


compareTo

public int compareTo(java.math.BigInteger that)
Compare to BigInteger.


compareTo

public int compareTo(long that)
Compare to long.


compareTo

public int compareTo(java.lang.Object object)
Compare object (BigRational/BigInteger/Long/Integer). Implements Comparable.compareTo(Object) (jdk-1.2 and later). Only BigRational/BigInteger/Long/Integer objects allowed, will throw otherwise.

Specified by:
compareTo in interface java.lang.Comparable

bigIntegerValue

public java.math.BigInteger bigIntegerValue()
Convert to BigInteger, by rounding.


bigDecimalValue

public java.math.BigDecimal bigDecimalValue(int precision)
Convert to BigDecimal


longValue

public long longValue()
Convert to long, by rounding and delegating to BigInteger. Implements Number.longValue().

Specified by:
longValue in class java.lang.Number

intValue

public int intValue()
Convert to int, by rounding and delegating to BigInteger. Implements Number.intValue().

Specified by:
intValue in class java.lang.Number

floatValue

public float floatValue()
Convert to floatl. Implements Number.floatValue().

Specified by:
floatValue in class java.lang.Number

doubleValue

public double doubleValue()
Convert to double Implements Number.doubleValue().

Specified by:
doubleValue in class java.lang.Number

valueOf

public static BigRational valueOf(java.math.BigInteger value)
Manifactor a BigRational from a BigInteger.


valueOf

public static BigRational valueOf(long value)
Manifactor a BigRational from a long fix number integer.


round

public BigRational round(int roundMode)
Round.


round

public BigRational round()
Round by default mode.


floor

public BigRational floor()
Floor, round towards negative infinity.


ceil

public BigRational ceil()
Ceiling, round towards positive infinity.


truncate

public BigRational truncate()
Truncate, round towards zero.


integerPart

public BigRational integerPart()
Integer part.


fractionalPart

public BigRational fractionalPart()
Fractional part.


integerAndFractionalPart

public BigRational[] integerAndFractionalPart()
Return an array of BigRationals with both integer and fractional part.