org.jscience.mathematics.algebraic.numbers
Class ExactRational

java.lang.Object
  extended by java.lang.Number
      extended by org.jscience.mathematics.algebraic.numbers.ComparableNumber<ExactRational>
          extended by org.jscience.mathematics.algebraic.numbers.ExactRational
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<ExactRational>, Field.Member, Ring.Member, AbelianGroup.Member, Member

public final class ExactRational
extends ComparableNumber<ExactRational>
implements java.lang.Cloneable, java.io.Serializable, Field.Member

The ExactRational class encapsulates rational numbers with infinite precision. Theyt are inbetween ExactInteger and ExactReal.

See Also:
RationalField, Serialized Form

Field Summary
static int DEFAULT_ROUND_MODE
          Default round mode, ROUND_HALF_UP.
static ExactRational NaN
           
static ExactRational NEGATIVE_INFINITY
           
static ExactRational ONE
          The exact rational representing the multiplicative identity.
static ExactRational POSITIVE_INFINITY
           
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 java.lang.Class TYPE
          The Class instance representing the type.
static ExactRational ZERO
          The exact rational representing the additive identity.
 
Constructor Summary
protected ExactRational(BigRational num)
          Constructs a ExactRational number.
  ExactRational(ExactInteger num)
          Constructs a ExactRational number.
  ExactRational(ExactInteger num, ExactInteger den)
          Constructs a ExactRational number.
  ExactRational(ExactRational num)
          Constructs a ExactRational number.
  ExactRational(int num)
          Constructs a ExactRational number.
  ExactRational(Integer num)
          Constructs a ExactRational number.
  ExactRational(long num)
          Constructs a ExactRational number.
  ExactRational(Long num)
          Constructs a ExactRational number.
  ExactRational(long num, long den)
          Constructs a ExactRational number.
  ExactRational(Long num, Long den)
          Constructs a ExactRational number.
  ExactRational(Rational num)
          Constructs a ExactRational number.
  ExactRational(java.lang.String s)
          Constructs the ExactRational number represented by a string.
 
Method Summary
 ExactRational abs()
          Returns the abs of this number.
 AbelianGroup.Member add(AbelianGroup.Member n)
          Returns the addition of this number and another.
 ExactRational add(ExactRational n)
          Returns the addition of this ExactRational number and another.
 ExactRational ceil()
          Ceiling, round towards positive infinity.
 java.lang.Object clone()
          Returns the arc hyperbolic tangent of a number.
 int compareTo(ComparableNumber obj)
          Compares two numbers.
 int compareTo(ExactRational value)
          Compares two ExactRational numbers.
 ExactRational divide(ExactRational n)
          Returns the division of this ExactRational number and another.
 Field.Member divide(Field.Member n)
          Returns the division of this number and another.
 double doubleValue()
           
 boolean equals(java.lang.Object obj)
          Compares two ExactRational numbers for equality.
 float floatValue()
           
 ExactRational floor()
          Floor, round towards negative infinity.
 ExactInteger fractionalPart()
          Retrieves the integer part as a ExactInteger.
 ExactInteger getDenominator()
          Retrieves the denominator as a ExactReal.
 ExactRational getDistance(ComparableNumber n)
          DOCUMENT ME!
 ExactRational getNaN()
          DOCUMENT ME!
 ExactRational getNegativeInfinity()
          DOCUMENT ME!
 ExactInteger getNumerator()
          Retrieves the numerator as a ExactInteger.
 ExactRational getPositiveInfinity()
          DOCUMENT ME!
 int hashCode()
          Returns the hashcode of this number.
 ExactInteger integerPart()
          Retrieves the integer part as a ExactInteger.
 int intValue()
           
 Field.Member inverse()
          Returns the inverse of this number.
 boolean isDefined()
           
 boolean isInfinite()
          Returns true if this number is infinite.
 boolean isNaN()
          Returns true if this number is NaN.
 boolean isNegativeInfinity()
          DOCUMENT ME!
 boolean isPositiveInfinity()
          DOCUMENT ME!
 long longValue()
           
 ExactRational max(ExactRational val)
          Returns the max of this number and another, according to compareTo rules.
 ExactRational min(ExactRational val)
          Returns the min of this number and another, according to compareTo rules.
 ExactRational mod(ExactRational that)
          Calculate the modulus of two ExactRationals and return a new ExactRational.
 ExactRational multiply(ExactRational n)
          Returns the multiplication of this ExactRational number and another.
 Ring.Member multiply(Ring.Member n)
          Returns the multiplication of this number and another.
 AbelianGroup.Member negate()
          Returns the negative of this number.
 ExactRational pow(int i)
          Returns this number raised to the power of a number.
 Rational rationalValue()
           
 ExactRational remainder(ExactRational that)
          Calculate the remainder of two ExactRationals and return a new ExactRational.
 ExactRational round()
          Round by default mode.
 ExactRational round(int roundMode)
          Round.
 int signum()
          Returns the sign of this number.
 AbelianGroup.Member subtract(AbelianGroup.Member n)
          Returns the subtraction of this number and another.
 ExactRational subtract(ExactRational n)
          Returns the subtraction of this ExactRational number and another.
 java.lang.String toString()
          Returns a string representing the value of this ExactRational number.
 ExactRational truncate()
          Truncate, round towards zero.
 java.math.BigDecimal value()
           
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final ExactRational ZERO
The exact rational representing the additive identity.


ONE

public static final ExactRational ONE
The exact rational representing the multiplicative identity.


NEGATIVE_INFINITY

public static final ExactRational NEGATIVE_INFINITY

POSITIVE_INFINITY

public static final ExactRational POSITIVE_INFINITY

NaN

public static final ExactRational NaN

TYPE

public static final java.lang.Class TYPE
The Class instance representing the type.


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

ExactRational

protected ExactRational(BigRational num)
Constructs a ExactRational number.


ExactRational

public ExactRational(ExactRational num)
Constructs a ExactRational number.


ExactRational

public ExactRational(ExactInteger num,
                     ExactInteger den)
Constructs a ExactRational number.


ExactRational

public ExactRational(ExactInteger num)
Constructs a ExactRational number.


ExactRational

public ExactRational(Long num,
                     Long den)
Constructs a ExactRational number.


ExactRational

public ExactRational(Long num)
Constructs a ExactRational number.


ExactRational

public ExactRational(Rational num)
Constructs a ExactRational number.


ExactRational

public ExactRational(Integer num)
Constructs a ExactRational number.


ExactRational

public ExactRational(long num,
                     long den)
Constructs a ExactRational number.


ExactRational

public ExactRational(long num)
Constructs a ExactRational number.


ExactRational

public ExactRational(int num)
Constructs a ExactRational number.


ExactRational

public ExactRational(java.lang.String s)
              throws java.lang.NumberFormatException
Constructs the ExactRational number represented by a string.

Parameters:
s - a string representing a ExactRational number.
Throws:
java.lang.NumberFormatException - if the string does not contain a parsable number.
Method Detail

integerPart

public ExactInteger integerPart()
Retrieves the integer part as a ExactInteger.


fractionalPart

public ExactInteger fractionalPart()
Retrieves the integer part as a ExactInteger.


getNumerator

public ExactInteger getNumerator()
Retrieves the numerator as a ExactInteger.


getDenominator

public ExactInteger getDenominator()
Retrieves the denominator as a ExactReal.


equals

public boolean equals(java.lang.Object obj)
Compares two ExactRational numbers for equality.

Overrides:
equals in class java.lang.Object
Parameters:
obj - a ExactRational number.

compareTo

public int compareTo(ComparableNumber obj)
              throws java.lang.IllegalArgumentException
Compares two numbers.

Parameters:
obj - a comparable number.
Returns:
a negative value if this<obj, zero if this==obj, and a positive value if this>obj.
Throws:
java.lang.IllegalArgumentException

compareTo

public int compareTo(ExactRational value)
Compares two ExactRational numbers.

Specified by:
compareTo in interface java.lang.Comparable<ExactRational>
Parameters:
value - a ExactRational number.
Returns:
a negative value if this<obj, zero if this==obj, and a positive value if this>obj.

toString

public java.lang.String toString()
Returns a string representing the value of this ExactRational number.

Overrides:
toString in class java.lang.Object

value

public java.math.BigDecimal value()

intValue

public int intValue()
Specified by:
intValue in class java.lang.Number

longValue

public long longValue()
Specified by:
longValue in class java.lang.Number

floatValue

public float floatValue()
Specified by:
floatValue in class java.lang.Number

rationalValue

public Rational rationalValue()

doubleValue

public double doubleValue()
Specified by:
doubleValue in class java.lang.Number

abs

public ExactRational abs()
Returns the abs of this number.


signum

public int signum()
Returns the sign of this number.


min

public ExactRational min(ExactRational val)
Returns the min of this number and another, according to compareTo rules.

Overrides:
min in class ComparableNumber<ExactRational>
Parameters:
val - DOCUMENT ME!
Returns:
DOCUMENT ME!

max

public ExactRational max(ExactRational val)
Returns the max of this number and another, according to compareTo rules.

Overrides:
max in class ComparableNumber<ExactRational>
Parameters:
val - DOCUMENT ME!
Returns:
DOCUMENT ME!

hashCode

public int hashCode()
Returns the hashcode of this number.

Overrides:
hashCode in class java.lang.Object

isNaN

public boolean isNaN()
Returns true if this number is NaN.

Specified by:
isNaN in class ComparableNumber<ExactRational>
Returns:
DOCUMENT ME!

isDefined

public boolean isDefined()

isInfinite

public boolean isInfinite()
Returns true if this number is infinite.

Specified by:
isInfinite in class ComparableNumber<ExactRational>
Returns:
DOCUMENT ME!

isNegativeInfinity

public boolean isNegativeInfinity()
Description copied from class: ComparableNumber
DOCUMENT ME!

Specified by:
isNegativeInfinity in class ComparableNumber<ExactRational>
Returns:
DOCUMENT ME!

isPositiveInfinity

public boolean isPositiveInfinity()
Description copied from class: ComparableNumber
DOCUMENT ME!

Specified by:
isPositiveInfinity in class ComparableNumber<ExactRational>
Returns:
DOCUMENT ME!

getNaN

public ExactRational getNaN()
Description copied from class: ComparableNumber
DOCUMENT ME!

Specified by:
getNaN in class ComparableNumber<ExactRational>
Returns:
DOCUMENT ME!

getNegativeInfinity

public ExactRational getNegativeInfinity()
Description copied from class: ComparableNumber
DOCUMENT ME!

Specified by:
getNegativeInfinity in class ComparableNumber<ExactRational>
Returns:
DOCUMENT ME!

getPositiveInfinity

public ExactRational getPositiveInfinity()
Description copied from class: ComparableNumber
DOCUMENT ME!

Specified by:
getPositiveInfinity in class ComparableNumber<ExactRational>
Returns:
DOCUMENT ME!

getDistance

public ExactRational getDistance(ComparableNumber n)
Description copied from class: ComparableNumber
DOCUMENT ME!

Specified by:
getDistance in class ComparableNumber<ExactRational>
Parameters:
n - DOCUMENT ME!
Returns:
DOCUMENT ME!

negate

public AbelianGroup.Member negate()
Returns the negative of this number.

Specified by:
negate in interface AbelianGroup.Member
Returns:
DOCUMENT ME!

inverse

public Field.Member inverse()
Returns the inverse of this number.

Specified by:
inverse in interface Field.Member
Returns:
DOCUMENT ME!

add

public AbelianGroup.Member add(AbelianGroup.Member n)
Returns the addition of this number and another.

Specified by:
add in interface AbelianGroup.Member
Parameters:
n - a group member
Returns:
DOCUMENT ME!

add

public ExactRational add(ExactRational n)
Returns the addition of this ExactRational number and another.


subtract

public AbelianGroup.Member subtract(AbelianGroup.Member n)
Returns the subtraction of this number and another.

Specified by:
subtract in interface AbelianGroup.Member
Parameters:
n - a group member
Returns:
DOCUMENT ME!

subtract

public ExactRational subtract(ExactRational n)
Returns the subtraction of this ExactRational number and another.


multiply

public Ring.Member multiply(Ring.Member n)
Returns the multiplication of this number and another.

Specified by:
multiply in interface Ring.Member
Parameters:
n - a ring member
Returns:
DOCUMENT ME!

multiply

public ExactRational multiply(ExactRational n)
Returns the multiplication of this ExactRational number and another.


divide

public Field.Member divide(Field.Member n)
Returns the division of this number and another.

Specified by:
divide in interface Field.Member
Parameters:
n - a field member
Returns:
DOCUMENT ME!

divide

public ExactRational divide(ExactRational n)
Returns the division of this ExactRational number and another.


remainder

public ExactRational remainder(ExactRational that)
Calculate the remainder of two ExactRationals and return a new ExactRational. [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).


mod

public ExactRational mod(ExactRational that)
Calculate the modulus of two ExactRationals and return a new ExactRational. 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).


round

public ExactRational round(int roundMode)
Round.


round

public ExactRational round()
Round by default mode.


floor

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


ceil

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


truncate

public ExactRational truncate()
Truncate, round towards zero.


pow

public ExactRational pow(int i)
Returns this number raised to the power of a number.


clone

public java.lang.Object clone()
Returns the arc hyperbolic tangent of a number.

Overrides:
clone in class java.lang.Object