Class BigRational

java.lang.Object
  |
  +--BigRational

public class BigRational
extends java.lang.Object

BigRational: dynamically sized immutable arbitrary-precision rational numbers.

By Eric Laroche <laroche@lrdev.com>, June 2002
Version @(#)$ Id: BigRational.java,v 1.1 2002/06/25 10:03:56 laroche Exp $

BigRational provides most operations needed in rational number space calculations, including multiplication and division.

BigRational uses Sun's java.math.BigInteger (JDK 1.1 and later).

Binary operations (e.g. add, multiply) calculate their result from a BigRational object ('this') and one argument (typically called 'that'), returning a new immutable BigRational object. Both the original object and the argument are left unchanged (hence immutable).

Unary operations (e.g. negate, invert) calculate their result from the BigRational object ('this'), returning a new immutable BigRational object. The original object is left unchanged.

Some commonly used short function names (abs, ceil, div, inv, max, min, mod, mul, neg, pow, rem, sign, sub, trunc) are additionally defined as aliases to to the full function names (absolute, ceiling, divide, invert, maximum, minimum, modulus, multiply, negate, power, remainder, signum, subtract, truncate). This makes the interface somewhat fatter.

BigRational implements private proxy functions for BigInteger functionality, including scanning and multiplying, to enhance speed and to realize fast checks for common values (1, 0).

Usage samples:

	BigRational("-21/35"): rational -3/5
	BigRational("/3"): rational 1/3
	BigRational("3.4"): rational 17/5
	BigRational(".7"): 0.7, rational 7/10
	BigRational("f/37", 0x10): 3/11
	BigRational("f.37", 0x10): 3895/256
	BigRational("-dcba.efgh", 23): -46112938320/279841
	BigRational("1234.5678")).toStringDot(6): "1234.567800"
	BigRational("1234.5678")).toStringDot(2): "1234.57"
	BigRational("1234.5678")).toStringDot(-2): "1200"

The BigRational source and documentation can typically be found at the author's site, at http://www.lrdev.com/lr/java/BigRational.java and http://www.lrdev.com/lr/java/BigRational.html.


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()
           An alias to absolute().
 BigRational absolute()
           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.BigInteger bigIntegerValue()
           Convert to BigInteger, by rounding.
 BigRational ceil()
           An alias to ceiling().
 BigRational ceiling()
           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 div(BigRational that)
           An alias to divide().
 BigRational div(long that)
           An alias to divide().
 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.
 boolean equals(java.lang.Object object)
           Compare object for equality.
 BigRational floor()
           Floor, round towards negative infinity.
 BigRational fractionalPart()
           Fractional part.
 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 inv()
           An alias to invert().
 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)
           An alias to maximum().
 BigRational max(long that)
           An alias to maximum().
 BigRational maximum(BigRational that)
           Return the maximal value of two BigRationals.
 BigRational maximum(long that)
           Return the maximum value of a BigRational and a long fix number integer.
 BigRational min(BigRational that)
           An alias to minimum().
 BigRational min(long that)
           An alias to minimum().
 BigRational minimum(BigRational that)
           Return the minimal value of two BigRationals.
 BigRational minimum(long that)
           Return the minimal value of a BigRational and a long fix number integer.
 BigRational mod(BigRational that)
           An alias to modulus().
 BigRational mod(long that)
           An alias to modulus().
 BigRational modulus(BigRational that)
           Calculate the modulus of two BigRationals and return a new BigRational.
 BigRational modulus(long that)
           Calculate the modulus of a BigRational and a long fix number integer and return a new BigRational.
 BigRational mul(BigRational that)
           An alias to multiply().
 BigRational mul(long that)
           An alias to multiply().
 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 neg()
           An alias to negate().
 BigRational negate()
           Return a new BigRational with the negative value of this.
 BigRational pow(int exponent)
           An alias to power().
 BigRational power(int exponent)
           Calculate a BigRational's integer power and return a new BigRational.
 BigRational rem(BigRational that)
           An alias to remainder().
 BigRational rem(long that)
           An alias to remainder().
 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 sign()
           An alias to signum().
 int signum()
           Signum, -1, 0, or 1.
 BigRational sub(BigRational that)
           An alias to subtract().
 BigRational sub(long that)
           An alias to subtract().
 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.
static void test()
           Run tests, implementation is commented out, run at development and integration time.
 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
 java.lang.String toStringDot(int precision, int radix)
           Dot-format "[-]i.f" string representation, with a precision.
 BigRational trunc()
           An alias to truncate().
 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.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait