org.apfloat
Class Aprational

java.lang.Object
  extended byjava.lang.Number
      extended byorg.apfloat.Apcomplex
          extended byorg.apfloat.Apfloat
              extended byorg.apfloat.Aprational
All Implemented Interfaces:
Comparable, Serializable
Direct Known Subclasses:
Apint

public class Aprational
extends Apfloat

Arbitrary precision rational number class. An aprational consists of a numerator and a denominator of type Apint.

Version:
1.0.3
Author:
Mikko Tommila
See Also:
Apint, Serialized Form

Field Summary
 
Fields inherited from class org.apfloat.Apcomplex
DEFAULT, I, INFINITE, ONE, ZERO
 
Constructor Summary
protected Aprational()
          Default constructor.
  Aprational(Apint value)
          Construct an integer aprational whose denominator is one.
  Aprational(Apint numerator, Apint denominator)
          Construct an aprational with the specified numerator and denominator.
  Aprational(BigInteger value)
          Constructs an aprational from a BigInteger.
  Aprational(BigInteger value, int radix)
          Constructs an aprational from a BigInteger using the specified radix.
  Aprational(PushbackReader in)
          Reads an aprational from a reader.
  Aprational(PushbackReader in, int radix)
          Reads an aprational from a reader.
  Aprational(String value)
          Constructs an aprational from a string.
  Aprational(String value, int radix)
          Constructs an aprational from a string with the specified radix.
 
Method Summary
 Aprational add(Aprational x)
          Adds two aprational numbers.
 Apint ceil()
          Ceiling function.
 int compareTo(Apfloat x)
          Compare this aprational to the specified apfloat.
 int compareTo(Aprational x)
          Compare this aprational to the specified aprational.
 int compareTo(Object obj)
          Compare this aprational to the specified object.
 Apint denominator()
          Denominator of this aprational.
 Aprational divide(Aprational x)
          Divides two aprational numbers.
 boolean equals(Object obj)
          Compares this object to the specified object.
 Apint floor()
          Floor function.
protected  ApfloatImpl getImpl(long precision)
          Returns an ApfloatImpl representing the approximation of this aprational up to the requested precision.
 int hashCode()
          Returns a hash code for this aprational.
 boolean isShort()
          Returns if this aprational is "short".
 Aprational multiply(Aprational x)
          Multiplies two aprational numbers.
 Apint numerator()
          Numerator of this aprational.
 long precision()
          Returns the precision of this aprational.
 int radix()
          Radix of this aprational.
 long scale()
          Returns the scale of this aprational.
 int signum()
          Returns the signum function of this aprational.
 Aprational subtract(Aprational x)
          Subtracts two aprational numbers.
 String toString()
          Returns a string representation of this aprational.
 String toString(boolean pretty)
          Returns a string representation of this aprational.
 Apint truncate()
          Truncates fractional part.
 void writeTo(Writer out)
          Write a string representation of this aprational to a Writer.
 void writeTo(Writer out, boolean pretty)
          Write a string representation of this aprational to a Writer.
 
Methods inherited from class org.apfloat.Apfloat
add, byteValue, divide, doubleValue, equalDigits, floatValue, imag, intValue, longValue, multiply, precision, real, shortValue, subtract
 
Methods inherited from class org.apfloat.Apcomplex
add, conj, divide, equalDigits, multiply, subtract
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Aprational

protected Aprational()
Default constructor. To be used only by subclasses that overload all needed methods.


Aprational

public Aprational(Apint value)
           throws ApfloatRuntimeException
Construct an integer aprational whose denominator is one.

Parameters:
value - The numerator of the number.

Aprational

public Aprational(Apint numerator,
                  Apint denominator)
           throws IllegalArgumentException,
                  ApfloatRuntimeException
Construct an aprational with the specified numerator and denominator.

Parameters:
numerator - The numerator.
denominator - The denominator.
Throws:
IllegalArgumentException - In case the denominator is zero, or if the denominator is not one or the numerator is not zero, and the radix of the numerator and denominator are different.
ApfloatRuntimeException

Aprational

public Aprational(String value)
           throws NumberFormatException,
                  IllegalArgumentException,
                  ApfloatRuntimeException
Constructs an aprational from a string. The default radix is used.

The input must be of one of the formats

integer
numerator [whitespace] "/" [whitespace] denominator

Parameters:
value - The input string.
Throws:
NumberFormatException - In case the number is invalid.
IllegalArgumentException - In case the denominator is zero.
ApfloatRuntimeException

Aprational

public Aprational(String value,
                  int radix)
           throws NumberFormatException,
                  IllegalArgumentException,
                  ApfloatRuntimeException
Constructs an aprational from a string with the specified radix.

The input must be of one of the formats

integer
numerator [whitespace] "/" [whitespace] denominator

Parameters:
value - The input string.
radix - The radix to be used.
Throws:
NumberFormatException - In case the number is invalid.
IllegalArgumentException - In case the denominator is zero.
ApfloatRuntimeException

Aprational

public Aprational(PushbackReader in)
           throws IOException,
                  NumberFormatException,
                  IllegalArgumentException,
                  ApfloatRuntimeException
Reads an aprational from a reader. The default radix is used. The constructor stops reading at the first character it doesn't understand. The reader must thus be a PushbackReader so that the invalid character can be returned back to the stream.

The input must be of one of the formats

integer [whitespace]
numerator [whitespace] "/" [whitespace] denominator

Parameters:
in - The input stream.
Throws:
IOException - In case of I/O error reading the stream.
NumberFormatException - In case the number is invalid.
IllegalArgumentException - In case the denominator is zero.
ApfloatRuntimeException

Aprational

public Aprational(PushbackReader in,
                  int radix)
           throws IOException,
                  IllegalArgumentException,
                  NumberFormatException,
                  IllegalArgumentException,
                  ApfloatRuntimeException
Reads an aprational from a reader. The specified radix is used.

Parameters:
in - The input stream.
radix - The radix to be used.
Throws:
IOException - In case of I/O error reading the stream.
IllegalArgumentException - In case the denominator is zero.
NumberFormatException - In case the number is invalid.
ApfloatRuntimeException
See Also:
Aprational(PushbackReader)

Aprational

public Aprational(BigInteger value)
           throws ApfloatRuntimeException
Constructs an aprational from a BigInteger. The default radix is used.

Parameters:
value - The numerator of the number.

Aprational

public Aprational(BigInteger value,
                  int radix)
           throws ApfloatRuntimeException
Constructs an aprational from a BigInteger using the specified radix.

Parameters:
value - The numerator of the number.
radix - The radix of the number.
Method Detail

numerator

public Apint numerator()
Numerator of this aprational.

Returns:
n where this = n / m.

denominator

public Apint denominator()
Denominator of this aprational.

Returns:
m where this = n / m.

radix

public int radix()
Radix of this aprational.

Overrides:
radix in class Apfloat
Returns:
Radix of this aprational.

precision

public long precision()
               throws ApfloatRuntimeException
Returns the precision of this aprational.

Overrides:
precision in class Apfloat
Returns:
INFINITE
Throws:
ApfloatRuntimeException

scale

public long scale()
           throws ApfloatRuntimeException
Returns the scale of this aprational. Scale is equal to the number of digits in the aprational's truncated value.

Zero has a scale of -INFINITE.

Overrides:
scale in class Apfloat
Returns:
Number of digits in the truncated value of this aprational in the radix in which it's presented.
Throws:
ApfloatRuntimeException

signum

public int signum()
Returns the signum function of this aprational.

Overrides:
signum in class Apfloat
Returns:
-1, 0 or 1 as the value of this aprational is negative, zero or positive.

isShort

public boolean isShort()
                throws ApfloatRuntimeException
Returns if this aprational is "short".

Overrides:
isShort in class Apfloat
Returns:
true if the aprational is "short", false if not.
Throws:
ApfloatRuntimeException
See Also:
Apfloat.isShort()

add

public Aprational add(Aprational x)
               throws ApfloatRuntimeException
Adds two aprational numbers.

Parameters:
x - The number to be added to this number.
Returns:
this + x.
Throws:
ApfloatRuntimeException

subtract

public Aprational subtract(Aprational x)
                    throws ApfloatRuntimeException
Subtracts two aprational numbers.

Parameters:
x - The number to be subtracted from this number.
Returns:
this - x.
Throws:
ApfloatRuntimeException

multiply

public Aprational multiply(Aprational x)
                    throws ApfloatRuntimeException
Multiplies two aprational numbers.

Parameters:
x - The number to be multiplied by this number.
Returns:
this * x.
Throws:
ApfloatRuntimeException

divide

public Aprational divide(Aprational x)
                  throws ArithmeticException,
                         ApfloatRuntimeException
Divides two aprational numbers.

Parameters:
x - The number by which this number is to be divided.
Returns:
this / x.
Throws:
ArithmeticException - In case the divisor is zero.
ApfloatRuntimeException

floor

public Apint floor()
            throws ApfloatRuntimeException
Floor function. Returns the largest (closest to positive infinity) value that is not greater than this aprational and is equal to a mathematical integer.

Overrides:
floor in class Apfloat
Returns:
This aprational rounded towards negative infinity.
Throws:
ApfloatRuntimeException

ceil

public Apint ceil()
           throws ApfloatRuntimeException
Ceiling function. Returns the smallest (closest to negative infinity) value that is not less than this aprational and is equal to a mathematical integer.

Overrides:
ceil in class Apfloat
Returns:
This aprational rounded towards positive infinity.
Throws:
ApfloatRuntimeException

truncate

public Apint truncate()
               throws ApfloatRuntimeException
Truncates fractional part.

Overrides:
truncate in class Apfloat
Returns:
This aprational rounded towards zero.
Throws:
ApfloatRuntimeException

compareTo

public int compareTo(Aprational x)
              throws ApfloatRuntimeException
Compare this aprational to the specified aprational.

Parameters:
x - Aprational to which this aprational is to be compared.
Returns:
-1, 0 or 1 as this aprational is numerically less than, equal to, or greater than x.
Throws:
ApfloatRuntimeException

compareTo

public int compareTo(Apfloat x)
              throws ApfloatRuntimeException
Compare this aprational to the specified apfloat.

Overrides:
compareTo in class Apfloat
Parameters:
x - Apfloat to which this aprational is to be compared.
Returns:
-1, 0 or 1 as this aprational is numerically less than, equal to, or greater than x.
Throws:
ApfloatRuntimeException

compareTo

public int compareTo(Object obj)
              throws ClassCastException
Compare this aprational to the specified object.

Specified by:
compareTo in interface Comparable
Overrides:
compareTo in class Apfloat
Parameters:
obj - Object to which this aprational is to be compared.
Returns:
-1, 0 or 1 as this aprational is numerically less than, equal to, or greater than obj.
Throws:
ClassCastException - If the specified object is not an apfloat.

equals

public boolean equals(Object obj)
Compares this object to the specified object.

Note: if two apfloats are compared where one number doesn't have enough precise digits, the mantissa is assumed to contain zeros. See Apfloat.compareTo(Apfloat).

Overrides:
equals in class Apfloat
Parameters:
obj - The object to compare with.
Returns:
true if the objects are the same; false otherwise.

hashCode

public int hashCode()
Returns a hash code for this aprational.

Overrides:
hashCode in class Apfloat
Returns:
The hash code value for this object.

toString

public String toString()
Returns a string representation of this aprational.

Overrides:
toString in class Apcomplex
Returns:
A string representing this object.

toString

public String toString(boolean pretty)
                throws ApfloatRuntimeException
Returns a string representation of this aprational.

Overrides:
toString in class Apfloat
Parameters:
pretty - true to use a fixed-point notation, false to use an exponential notation.
Returns:
A string representing this object.
Throws:
ApfloatRuntimeException

writeTo

public void writeTo(Writer out)
             throws IOException,
                    ApfloatRuntimeException
Write a string representation of this aprational to a Writer.

Overrides:
writeTo in class Apcomplex
Parameters:
out - The output Writer.
Throws:
IOException - In case of I/O error writing to the stream.
ApfloatRuntimeException

writeTo

public void writeTo(Writer out,
                    boolean pretty)
             throws IOException,
                    ApfloatRuntimeException
Write a string representation of this aprational to a Writer.

Overrides:
writeTo in class Apfloat
Parameters:
out - The output Writer.
pretty - true to use a fixed-point notation, false to use an exponential notation.
Throws:
IOException - In case of I/O error writing to the stream.
ApfloatRuntimeException

getImpl

protected ApfloatImpl getImpl(long precision)
                       throws ApfloatRuntimeException
Returns an ApfloatImpl representing the approximation of this aprational up to the requested precision.

Overrides:
getImpl in class Apfloat
Parameters:
precision - Precision of the ApfloatImpl that is needed.
Returns:
An ApfloatImpl representing this object to the requested precision.
Throws:
ApfloatRuntimeException