org.apfloat
Class Apint

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

public class Apint
extends Aprational

Arbitrary precision integer class.

In addition to the constructors, it is possible to create an apint from an apfloat or aprational via the methods that round these numbers to an integer value: Apfloat.floor(), Apfloat.ceil(), and Apfloat.truncate().

Version:
1.0.2
Author:
Mikko Tommila
See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.apfloat.Apcomplex
DEFAULT, I, INFINITE, ONE, ZERO
 
Constructor Summary
protected Apint()
          Default constructor.
  Apint(java.math.BigInteger value)
          Constructs an apint from a BigInteger.
  Apint(java.math.BigInteger value, int radix)
          Constructs an apint from a BigInteger using the specified radix.
  Apint(long value)
          Constructs an apfloat from the specified long.
  Apint(long value, int radix)
          Constructs an apfloat from the specified long and radix.
  Apint(java.io.PushbackReader in)
          Reads an apint from a stream using the default radix.
  Apint(java.io.PushbackReader in, int radix)
          Reads an apint from a stream using the specified radix.
  Apint(java.lang.String value)
          Constructs an apfloat from the specified string.
  Apint(java.lang.String value, int radix)
          Constructs an apfloat from the specified string and radix.
 
Method Summary
 Apint add(Apint x)
          Adds two apints.
 Apint ceil()
          Ceiling function.
 int compareTo(Apfloat x)
          Compare this apint to the specified apfloat.
 int compareTo(Apint x)
          Compare this apint to the specified apint.
 int compareTo(Aprational x)
          Compare this apint to the specified aprational.
 int compareTo(java.lang.Object obj)
          Compare this apint to the specified object.
 Apint denominator()
          Denominator of this aprational.
 Apint divide(Apint x)
          Divides two apints.
 boolean equals(java.lang.Object obj)
          Compares this object to the specified object.
 Apint floor()
          Floor function.
protected  ApfloatImpl getImpl(long precision)
          Returns an ApfloatImpl representing this apint up to the requested precision.
 int hashCode()
          Returns a hash code for this apint.
 boolean isShort()
          Returns if this apint is "short".
 Apint mod(Apint x)
          Calculates the remainder when divided by an apint.
 Apint multiply(Apint x)
          Multiplies two apints.
 Apint numerator()
          Numerator of this aprational.
 int radix()
          Radix of this apint.
 long scale()
          Returns the scale of this apint.
 int signum()
          Returns the signum function of this apint.
 Apint subtract(Apint x)
          Subtracts two apints.
 java.lang.String toString(boolean pretty)
          Returns a string representation of this aprational.
 Apint truncate()
          Truncates fractional part.
 void writeTo(java.io.Writer out, boolean pretty)
          Write a string representation of this aprational to a Writer.
 
Methods inherited from class org.apfloat.Aprational
add, divide, multiply, precision, subtract, toString, writeTo
 
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

Apint

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


Apint

public Apint(java.lang.String value)
      throws java.lang.NumberFormatException,
             ApfloatRuntimeException
Constructs an apfloat from the specified string. The default radix will be used.

Parameters:
value - The string representing the number.
Throws:
java.lang.NumberFormatException - In case the number is invalid.
ApfloatRuntimeException

Apint

public Apint(java.lang.String value,
             int radix)
      throws java.lang.NumberFormatException,
             ApfloatRuntimeException
Constructs an apfloat from the specified string and radix.

Parameters:
value - The string representing the number.
radix - The radix of the number.
Throws:
java.lang.NumberFormatException - In case the number is invalid.
ApfloatRuntimeException

Apint

public Apint(long value)
      throws java.lang.NumberFormatException,
             ApfloatRuntimeException
Constructs an apfloat from the specified long. The default radix will be used.

Parameters:
value - The value of the number.
Throws:
java.lang.NumberFormatException - In case the number is invalid.
ApfloatRuntimeException

Apint

public Apint(long value,
             int radix)
      throws java.lang.NumberFormatException,
             ApfloatRuntimeException
Constructs an apfloat from the specified long and radix.

Parameters:
value - The value of the number.
radix - The radix of the number.
Throws:
java.lang.NumberFormatException - In case the number is invalid.
ApfloatRuntimeException

Apint

public Apint(java.io.PushbackReader in)
      throws java.io.IOException,
             java.lang.NumberFormatException,
             ApfloatRuntimeException
Reads an apint from a stream using the default radix.

Parameters:
in - The stream to read from
Throws:
java.io.IOException - If an I/O error occurs accessing the stream.
java.lang.NumberFormatException - If the number is not valid.
ApfloatRuntimeException

Apint

public Apint(java.io.PushbackReader in,
             int radix)
      throws java.io.IOException,
             java.lang.NumberFormatException,
             ApfloatRuntimeException
Reads an apint from a stream using the specified radix.

Parameters:
in - The stream to read from
radix - The radix of the number.
Throws:
java.io.IOException - If an I/O error occurs accessing the stream.
java.lang.NumberFormatException - If the number is not valid.
ApfloatRuntimeException

Apint

public Apint(java.math.BigInteger value)
      throws java.lang.NumberFormatException,
             ApfloatRuntimeException
Constructs an apint from a BigInteger. The default radix is used.

Parameters:
value - The value of the number.
Throws:
java.lang.NumberFormatException - If the default radix is not valid.
ApfloatRuntimeException

Apint

public Apint(java.math.BigInteger value,
             int radix)
      throws java.lang.NumberFormatException,
             ApfloatRuntimeException
Constructs an apint from a BigInteger using the specified radix.

Parameters:
value - The value of the number.
radix - The radix of the number.
Throws:
java.lang.NumberFormatException - If the radix is not valid.
ApfloatRuntimeException
Method Detail

numerator

public Apint numerator()
Numerator of this aprational.

Overrides:
numerator in class Aprational
Returns:
this.

denominator

public Apint denominator()
Denominator of this aprational.

Overrides:
denominator in class Aprational
Returns:
Apcomplex.ONE.

radix

public int radix()
Radix of this apint.

Overrides:
radix in class Aprational
Returns:
Radix of this apint.

scale

public long scale()
           throws ApfloatRuntimeException
Returns the scale of this apint. Scale is equal to the number of digits in an apint.

Zero has a scale of -INFINITE.

Overrides:
scale in class Aprational
Returns:
Number of digits in this apint in the radix in which it's presented.
Throws:
ApfloatRuntimeException

signum

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

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

isShort

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

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

add

public Apint add(Apint x)
          throws ApfloatRuntimeException
Adds two apints.

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

subtract

public Apint subtract(Apint x)
               throws ApfloatRuntimeException
Subtracts two apints.

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

multiply

public Apint multiply(Apint x)
               throws ApfloatRuntimeException
Multiplies two apints.

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

divide

public Apint divide(Apint x)
             throws java.lang.ArithmeticException,
                    ApfloatRuntimeException
Divides two apints.

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

mod

public Apint mod(Apint x)
          throws ApfloatRuntimeException
Calculates the remainder when divided by an apint. The result has the same sign as this number. If x is zero, then zero is returned.

Parameters:
x - The number that is used as the divisor in the remainder calculation.
Returns:
this % x.
Throws:
ApfloatRuntimeException
See Also:
ApfloatMath.fmod(Apfloat,Apfloat)

floor

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

Overrides:
floor in class Aprational
Returns:
This apint.

ceil

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

Overrides:
ceil in class Aprational
Returns:
This apint.

truncate

public Apint truncate()
Truncates fractional part.

Overrides:
truncate in class Aprational
Returns:
This apint.

compareTo

public int compareTo(Apint x)
              throws ApfloatRuntimeException
Compare this apint to the specified apint.

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

compareTo

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

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

compareTo

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

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

compareTo

public int compareTo(java.lang.Object obj)
              throws java.lang.ClassCastException,
                     ApfloatRuntimeException
Compare this apint to the specified object.

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

equals

public boolean equals(java.lang.Object obj)
               throws ApfloatRuntimeException
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 Aprational
Parameters:
obj - The object to compare with.
Returns:
true if the objects are the same; false otherwise.
Throws:
ApfloatRuntimeException

hashCode

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

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

toString

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

Overrides:
toString in class Aprational
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(java.io.Writer out,
                    boolean pretty)
             throws java.io.IOException,
                    ApfloatRuntimeException
Write a string representation of this aprational to a Writer.

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

getImpl

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

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