Package org.apfloat
Class Apint
- java.lang.Object
-
- java.lang.Number
-
- org.apfloat.Apcomplex
-
- org.apfloat.Apfloat
-
- org.apfloat.Aprational
-
- org.apfloat.Apint
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<Apfloat>
,java.util.Formattable
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()
, andApfloat.truncate()
.- Version:
- 1.10.1
- See Also:
ApintMath
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static long
serialVersionUID
private Apfloat
value
-
Fields inherited from class org.apfloat.Apcomplex
DEFAULT, EXTRA_PRECISION, I, INFINITE, ONE, ONES, REAL_ABS_IMAG_ORDER, REAL_IMAG_ORDER, ZERO, ZEROS
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Apint()
Default constructor.Apint(long value)
Constructs an apfloat from the specifiedlong
.Apint(long value, int radix)
Constructs an apfloat from the specifiedlong
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.Apint(java.math.BigInteger value)
Constructs an apint from aBigInteger
.Apint(java.math.BigInteger value, int radix)
Constructs an apint from aBigInteger
using the specified radix.(package private)
Apint(Apfloat value)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) Apint
abs()
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.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.void
formatTo(java.util.Formatter formatter, int flags, int width, int precision)
Formats the object using the provided formatter.Apint
frac()
Returns the fractional part.protected ApfloatImpl
getImpl(long precision)
Returns anApfloatImpl
representing this apint up to the requested precision.int
hashCode()
Returns a hash code for this apint.boolean
isInteger()
Returns if this number has an integer value.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
negate()
Negative value.Apint
numerator()
Numerator of this aprational.int
radix()
Radix of this apint.(package private) Apint
roundAway()
long
scale()
Returns the scale of this apint.int
signum()
Returns the signum function of this apint.long
size()
Returns the size of this apint.Apint
subtract(Apint x)
Subtracts two apints.boolean
test(Apfloat x)
Tests two apfloat numbers for equality.boolean
test(Apint x)
Tests two apint numbers for equality.boolean
test(Aprational x)
Tests two aprational numbers for equality.java.math.BigInteger
toBigInteger()
Converts this apint to Java'sBigInteger
.Apint
toRadix(int radix)
Convert this apint to the specified radix.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 aWriter
.-
Methods inherited from class org.apfloat.Aprational
add, compareToHalf, divide, mod, multiply, precision, preferCompare, scale, subtract, toString, writeTo
-
Methods inherited from class org.apfloat.Apfloat
add, byteValue, divide, doubleValue, equalDigits, floatValue, imag, intValue, longValue, longValueExact, mod, multiply, precision, real, shortValue, subtract
-
Methods inherited from class org.apfloat.Apcomplex
add, byteValueExact, conj, divide, equalDigits, intValueExact, isZero, multiply, shortValueExact, subtract, test
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
value
private Apfloat value
-
-
Constructor Detail
-
Apint
protected Apint()
Default constructor. To be used only by subclasses that overload all needed methods.
-
Apint
Apint(Apfloat value)
-
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 specifiedlong
. 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 specifiedlong
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 fromradix
- 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 aBigInteger
. 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 aBigInteger
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 classAprational
- Returns:
this
.
-
denominator
public Apint denominator()
Denominator of this aprational.- Overrides:
denominator
in classAprational
- Returns:
Apcomplex.ONE
.
-
radix
public int radix()
Radix of this apint.- Overrides:
radix
in classAprational
- 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 classAprational
- Returns:
- Number of digits in this apint in the radix in which it's presented.
- Throws:
ApfloatRuntimeException
- See Also:
Apfloat.scale()
-
size
public long size() throws ApfloatRuntimeException
Returns the size of this apint. Size is equal to the number of significant digits in the number, excluding any trailing zeros.Zero has a size of
0
.- Overrides:
size
in classAprational
- Returns:
- Number of significant digits in this number, excluding trailing zeros, in the radix in which it's presented.
- Throws:
ApfloatRuntimeException
- Since:
- 1.6
- See Also:
Apfloat.size()
-
signum
public int signum()
Returns the signum function of this apint.- Overrides:
signum
in classAprational
- 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 classAprational
- Returns:
true
if the apint is "short",false
if not.- Throws:
ApfloatRuntimeException
- See Also:
Apfloat.isShort()
-
isInteger
public boolean isInteger() throws ApfloatRuntimeException
Returns if this number has an integer value.For
Apint
this method always returnstrue
.- Overrides:
isInteger
in classAprational
- Returns:
true
.- Throws:
ApfloatRuntimeException
- Since:
- 1.9.0
-
negate
public Apint negate() throws ApfloatRuntimeException
Negative value.- Overrides:
negate
in classAprational
- Returns:
-this
.- Throws:
ApfloatRuntimeException
- Since:
- 1.1
-
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. Ifx
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 classAprational
- 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 classAprational
- Returns:
- This apint.
-
truncate
public Apint truncate()
Truncates fractional part.- Overrides:
truncate
in classAprational
- Returns:
- This apint.
-
frac
public Apint frac() throws ApfloatRuntimeException
Returns the fractional part.- Overrides:
frac
in classAprational
- Returns:
- Always zero.
- Throws:
ApfloatRuntimeException
- Since:
- 1.7.0
-
toBigInteger
public java.math.BigInteger toBigInteger() throws java.lang.IllegalArgumentException
Converts this apint to Java'sBigInteger
. This method can be greatly faster than converting to String and then to BigInteger.- Returns:
- This apint converted to a
BigInteger
. - Throws:
java.lang.IllegalArgumentException
- If this number is too big to fit in aBigInteger
.- Since:
- 1.6
-
toRadix
public Apint toRadix(int radix) throws java.lang.NumberFormatException, ApfloatRuntimeException
Convert this apint to the specified radix.- Overrides:
toRadix
in classAprational
- Parameters:
radix
- The radix.- Returns:
- This number in the specified radix.
- Throws:
java.lang.NumberFormatException
- If the radix is invalid.ApfloatRuntimeException
- Since:
- 1.2
-
compareTo
public int compareTo(Apint x)
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
.
-
compareTo
public int compareTo(Aprational x)
Compare this apint to the specified aprational.- Overrides:
compareTo
in classAprational
- 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
.
-
compareTo
public int compareTo(Apfloat x)
Compare this apint to the specified apfloat.- Specified by:
compareTo
in interfacejava.lang.Comparable<Apfloat>
- Overrides:
compareTo
in classAprational
- 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
.
-
equals
public boolean equals(java.lang.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 classAprational
- Parameters:
obj
- The object to compare with.- Returns:
true
if the objects are the same;false
otherwise.
-
test
public boolean test(Apint x)
Tests two apint numbers for equality. Returnsfalse
if the numbers are definitely known to be not equal. Iftrue
is returned, equality is unknown and should be verified by callingequals(Object)
. This method is usually significantly faster than callingequals(Object)
.- Parameters:
x
- The number to test against.- Returns:
false
if the numbers are definitely not equal,true
if unknown.- Since:
- 1.10.0
-
test
public boolean test(Aprational x)
Description copied from class:Aprational
Tests two aprational numbers for equality. Returnsfalse
if the numbers are definitely known to be not equal. Iftrue
is returned, equality is unknown and should be verified by callingAprational.equals(Object)
. This method is usually significantly faster than callingequals(Object)
.- Overrides:
test
in classAprational
- Parameters:
x
- The number to test against.- Returns:
false
if the numbers are definitely not equal,true
if unknown.
-
test
public boolean test(Apfloat x)
Description copied from class:Apfloat
Tests two apfloat numbers for equality. Returnsfalse
if the numbers are definitely known to be not equal. Iftrue
is returned, equality is unknown and should be verified by callingApfloat.equals(Object)
. This method is usually significantly faster than callingequals(Object)
.- Overrides:
test
in classAprational
- Parameters:
x
- The number to test against.- Returns:
false
if the numbers are definitely not equal,true
if unknown.
-
hashCode
public int hashCode()
Returns a hash code for this apint.- Overrides:
hashCode
in classAprational
- 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 classAprational
- 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 aWriter
.- Overrides:
writeTo
in classAprational
- Parameters:
out
- The outputWriter
.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
-
formatTo
public void formatTo(java.util.Formatter formatter, int flags, int width, int precision)
Description copied from class:Aprational
Formats the object using the provided formatter.- Specified by:
formatTo
in interfacejava.util.Formattable
- Overrides:
formatTo
in classAprational
- Parameters:
formatter
- The formatter.flags
- The flags to modify the output format.width
- The minimum number of characters to be written to the output, or-1
for no minimum.precision
- The maximum number of characters to be written to the output, or-1
for no maximum.- See Also:
Apfloat.formatTo(Formatter,int,int,int)
-
getImpl
protected ApfloatImpl getImpl(long precision) throws ApfloatRuntimeException
Returns anApfloatImpl
representing this apint up to the requested precision.- Overrides:
getImpl
in classAprational
- Parameters:
precision
- Precision of theApfloatImpl
that is needed.- Returns:
- An
ApfloatImpl
representing this object to the requested precision. - Throws:
ApfloatRuntimeException
-
roundAway
Apint roundAway()
- Overrides:
roundAway
in classAprational
-
abs
Apint abs()
- Overrides:
abs
in classAprational
-
-