Package gnu.math
Class IntNum
-
- All Implemented Interfaces:
Externalizable,Serializable,Comparable
- Direct Known Subclasses:
XInteger
public class IntNum extends RatNum implements Externalizable
A class for infinite-precision integers.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static IntNumabs(IntNum x)static IntNumadd(int x, int y)Add two ints, yielding an IntNum.static IntNumadd(IntNum x, int y)Add an IntNum and an int, yielding a new IntNum.static IntNumadd(IntNum x, IntNum y)Add two IntNums, yielding their sum as another IntNum.static IntNumadd(IntNum x, IntNum y, int k)Add two IntNums, yielding their sum as another IntNum.Numericadd(Object y, int k)Return this + k * obj.static IntNumalloc(int nwords)Allocate a new non-shared IntNum.BigDecimalasBigDecimal()BigIntegerasBigInteger()static IntNumasIntNumOrNull(Object value)IntNumcanonicalize()static intcompare(IntNum x, long y)Return -1, 0, or 1, depending on which value is greater.static intcompare(IntNum x, IntNum y)Return -1, 0, or 1, depending on which value is greater.intcompare(Object obj)Return an integer for which ofthisorobjis larger.IntNumdenominator()Numericdiv(Object y)static voiddivide(long x, long y, IntNum quotient, IntNum remainder, int rounding_mode)static voiddivide(IntNum x, IntNum y, IntNum quotient, IntNum remainder, int rounding_mode)Divide two integers, yielding quotient and remainder.doubledoubleValue()The value of the real component, as a double.static booleanequals(IntNum x, IntNum y)booleanequals(Object obj)voidformat(int radix, StringBuffer buffer)voidformat(int radix, StringBuilder buffer)static intgcd(int a, int b)Calculate Greatest Common Divisor for non-negative ints.static IntNumgcd(IntNum x, IntNum y)voidgetAbsolute(int[] words)Copy the abolute value of this into an array of words.inthashCode()booleaninIntRange()Does this value fit in a signed 32-bitint?booleaninLongRange()Does this value fit in a signed 64-bitlong?booleaninRange(long lo, long hi)Is this integer both>= loand<= hi?.intintLength()Calculatesceiling(log2(this < 0 ? -this : this+1)).intintValue()static intintValue(Object obj)Cast an Object to an int.booleanisMinusOne()booleanisNegative()booleanisOdd()booleanisOne()booleanisZero()static IntNumlcm(IntNum x, IntNum y)longlongValue()static IntNummake(int value)Return a (possibly-shared) IntNum with a given int value.static IntNummake(int[] words)static IntNummake(int[] words, int len)Make a canonicalized IntNum from an array of words.static IntNummake(long value)static IntNumminusOne()Return the IntNum for -1.static IntNummodulo(IntNum x, IntNum y)Numericmul(Object y)Numericneg()static IntNumneg(IntNum x)static booleannegate(int[] dest, int[] src, int len)Set dest[0:len-1] to the negation of src[0:len-1].IntNumnumerator()static IntNumone()Numericpower(IntNum y)Return this raised to an integer power.static IntNumpower(IntNum x, int y)Calculate the integral power of an IntNum.static IntNumquotient(IntNum x, IntNum y)static IntNumquotient(IntNum x, IntNum y, int rounding_mode)voidreadExternal(ObjectInput in)ObjectreadResolve()voidrealloc(int nwords)Change words.length to nwords.static IntNumremainder(IntNum x, IntNum y)static IntNumremainder(IntNum x, IntNum y, int rounding_mode)doubleroundToDouble(int exp, boolean neg, boolean remainder)Convert a semi-processed IntNum to double.voidset(int y)Destructively set the value of this to an int.voidset(int[] words, int length)Destructively set the value of this to the given words.voidset(long y)Destructively set the value of this to a long.voidset(IntNum y)Destructively set the value of this to that of y.voidsetAdd(int y)Destructively add an int to this.voidsetAdd(IntNum x, int y)Set this to the sum of x and y.voidsetNegative()Destructively negate this.voidsetNegative(IntNum x)Destructively set this to the negative of x.static IntNumshift(IntNum x, int count)intsign()Return 1 if>0; 0 if==0; -1 if<0; -2 ifNaN.static IntNumsub(IntNum x, IntNum y)Subtract two IntNums, yielding their sum as another IntNum.static IntNumten()static IntNumtimes(int x, int y)Multiply two ints, yielding an IntNum.static IntNumtimes(IntNum x, int y)static IntNumtimes(IntNum x, IntNum y)IntNumtoExactInt(int rounding_mode)Converts to an exact integer, with specified rounding mode.RealNumtoInt(int rounding_mode)Converts a real to an integer, according to a specified rounding mode.StringtoString(int radix)static IntNumvalueOf(byte[] digits, int byte_len, boolean negative, int radix)static IntNumvalueOf(char[] buf, int offset, int length, int radix, boolean negative)static IntNumvalueOf(int value)Return a (possibly-shared) IntNum with a given int value.static IntNumvalueOf(long value)Return a (possibly-shared) IntNum with a given long value.static IntNumvalueOf(String s)static IntNumvalueOf(String s, int radix)static IntNumvalueOfUnsigned(int value)Make an IntNum from an unsigned 32-bit value.static IntNumvalueOfUnsigned(long value)Make an IntNum from an unsigned 64-bit value.static intwordsNeeded(int[] words, int len)Calculate how many words are significant in words[0:len-1].voidwriteExternal(ObjectOutput out)static IntNumzero()-
Methods inherited from class gnu.math.RatNum
add, asRatNumOrNull, classifyFinite, compare, divide, equals, infinity, isExact, make, neg, rationalize, rneg, times, toExact, valueOf
-
Methods inherited from class gnu.math.RealNum
abs, add, angle, asRealNumOrNull, compareTo, conjugate, cos, divide, exp, im, isReal, log, max, min, re, sin, sqrt, tan, times, toExactInt, toExactInt, toInexact, toInt, toScaledInt, toScaledInt, toScaledInt, toStringDecimal, toStringScientific, toStringScientific, toStringScientific, toStringScientific, unitQuaternion, unitVector, vectorPart
-
Methods inherited from class gnu.math.Complex
add, addReversed, colatitude, compare, complexPart, divide, divReversed, equals, imMinusOne, imOne, jm, km, longitude, make, make, mulReversed, neg, polar, polar, power, times
-
Methods inherited from class gnu.math.Quaternion
add, compare, divide, doubleImagValue, doubleJmagValue, doubleKmagValue, doubleRealValue, equals, jmMinusOne, jmOne, kmMinusOne, kmOne, make, make, neg, number, polar, polar, power, times
-
Methods inherited from class gnu.math.Quantity
add, compare, compareReversed, dimensions, divide, imValue, jmValue, kmValue, make, make, make, reValue, times, unit
-
Methods inherited from class gnu.math.Numeric
add, asNumericOrNull, div_inv, floatValue, geq, grt, mul_ident, sub, toString
-
Methods inherited from class java.lang.Number
byteValue, shortValue
-
-
-
-
Method Detail
-
zero
public static final IntNum zero()
-
one
public static final IntNum one()
-
ten
public static final IntNum ten()
-
minusOne
public static IntNum minusOne()
Return the IntNum for -1.
-
alloc
public static IntNum alloc(int nwords)
Allocate a new non-shared IntNum.- Parameters:
nwords- number of words to allocate
-
realloc
public void realloc(int nwords)
Change words.length to nwords. We allow words.length to be upto nwords+2 without reallocating.
-
denominator
public final IntNum denominator()
- Specified by:
denominatorin classRatNum
-
isNegative
public final boolean isNegative()
- Specified by:
isNegativein classRealNum
-
sign
public int sign()
Description copied from class:RealNumReturn 1 if>0; 0 if==0; -1 if<0; -2 ifNaN.
-
compare
public static int compare(IntNum x, IntNum y)
Return -1, 0, or 1, depending on which value is greater.
-
compare
public static int compare(IntNum x, long y)
Return -1, 0, or 1, depending on which value is greater.
-
compare
public int compare(Object obj)
Description copied from class:NumericReturn an integer for which ofthisorobjis larger. Return 1 ifthis>obj; 0 ifthis==obj; -1 ifthis<obj; -2 ifthis!=objotherwise (for example if either is NaN); -3 if not comparable (incompatible types).
-
isOdd
public final boolean isOdd()
-
isOne
public final boolean isOne()
-
isMinusOne
public final boolean isMinusOne()
-
wordsNeeded
public static int wordsNeeded(int[] words, int len)Calculate how many words are significant in words[0:len-1]. Returns the least valuexsuch thatx>0 && words[0:x-1]==words[0:len-1], whenwordsis viewed as a 2's complement integer.
-
canonicalize
public IntNum canonicalize()
-
add
public static final IntNum add(int x, int y)
Add two ints, yielding an IntNum.
-
setAdd
public void setAdd(IntNum x, int y)
Set this to the sum of x and y. OK if x==this.
-
setAdd
public final void setAdd(int y)
Destructively add an int to this.
-
set
public final void set(int y)
Destructively set the value of this to an int.
-
set
public final void set(long y)
Destructively set the value of this to a long.
-
set
public final void set(int[] words, int length)Destructively set the value of this to the given words. The words array is reused, not copied.
-
set
public final void set(IntNum y)
Destructively set the value of this to that of y.
-
add
public static IntNum add(IntNum x, IntNum y)
Add two IntNums, yielding their sum as another IntNum.
-
sub
public static IntNum sub(IntNum x, IntNum y)
Subtract two IntNums, yielding their sum as another IntNum.
-
add
public static IntNum add(IntNum x, IntNum y, int k)
Add two IntNums, yielding their sum as another IntNum.
-
times
public static final IntNum times(int x, int y)
Multiply two ints, yielding an IntNum.
-
divide
public static void divide(long x, long y, IntNum quotient, IntNum remainder, int rounding_mode)
-
divide
public static void divide(IntNum x, IntNum y, IntNum quotient, IntNum remainder, int rounding_mode)
Divide two integers, yielding quotient and remainder.- Parameters:
x- the numerator in the divisiony- the denominator in the divisionquotient- is set to the quotient of the result (iff quotient!=null)remainder- is set to the remainder of the result (iff remainder!=null)rounding_mode- one of FLOOR, CEILING, TRUNCATE, or ROUND.
-
toExactInt
public IntNum toExactInt(int rounding_mode)
Description copied from class:RealNumConverts to an exact integer, with specified rounding mode.- Overrides:
toExactIntin classRatNum
-
toInt
public RealNum toInt(int rounding_mode)
Description copied from class:RealNumConverts a real to an integer, according to a specified rounding mode. Note an inexact argument gives an inexact result, following Scheme. See also toExactInt.
-
power
public Numeric power(IntNum y)
Description copied from class:NumericReturn this raised to an integer power. Implemented by repeated squaring and multiplication. Ify < 0, returns div_inv of the result.
-
power
public static IntNum power(IntNum x, int y)
Calculate the integral power of an IntNum.- Parameters:
x- the value (base) to exponentiatey- the exponent (must be non-negative)
-
gcd
public static final int gcd(int a, int b)Calculate Greatest Common Divisor for non-negative ints.
-
format
public void format(int radix, StringBuffer buffer)
-
format
public void format(int radix, StringBuilder buffer)
-
intValue
public static int intValue(Object obj)
Cast an Object to an int. The Object must (currently) be an IntNum.
-
longValue
public long longValue()
- Overrides:
longValuein classQuaternion
-
make
public static IntNum make(int value)
Return a (possibly-shared) IntNum with a given int value.
-
valueOfUnsigned
public static IntNum valueOfUnsigned(long value)
Make an IntNum from an unsigned 64-bit value.
-
valueOfUnsigned
public static IntNum valueOfUnsigned(int value)
Make an IntNum from an unsigned 32-bit value.
-
make
public static IntNum make(int[] words, int len)
Make a canonicalized IntNum from an array of words. The array may be reused (without copying).
-
make
public static IntNum make(int[] words)
-
make
public static IntNum make(long value)
-
valueOf
public static IntNum valueOf(int value)
Return a (possibly-shared) IntNum with a given int value.
-
valueOf
public static IntNum valueOf(long value)
Return a (possibly-shared) IntNum with a given long value.
-
valueOf
public static IntNum valueOf(char[] buf, int offset, int length, int radix, boolean negative)
-
valueOf
public static IntNum valueOf(String s, int radix) throws NumberFormatException
- Throws:
NumberFormatException
-
valueOf
public static IntNum valueOf(byte[] digits, int byte_len, boolean negative, int radix)
-
valueOf
public static IntNum valueOf(String s) throws NumberFormatException
- Throws:
NumberFormatException
-
doubleValue
public double doubleValue()
Description copied from class:QuantityThe value of the real component, as a double. This is relative to the unit().dims - i.e. unit()/doubleValue() is factored in.- Overrides:
doubleValuein classQuaternion
-
roundToDouble
public double roundToDouble(int exp, boolean neg, boolean remainder)Convert a semi-processed IntNum to double. Number must be non-negative. Multiplies by a power of two, applies sign, and converts to double, with the usual java rounding.- Parameters:
exp- power of two, positive or negative, by which to multiplyneg- true if negativeremainder- true if the IntNum is the result of a truncating division that had non-zero remainder. To ensure proper rounding in this case, the IntNum must have at least 54 bits.
-
add
public Numeric add(Object y, int k)
Description copied from class:NumericReturn this + k * obj.
-
getAbsolute
public void getAbsolute(int[] words)
Copy the abolute value of this into an array of words. Assumeswords.length >= (this.words == null ? 1 : this.ival). Result is zero-extended, but need not be a valid 2's complement number.
-
negate
public static boolean negate(int[] dest, int[] src, int len)Set dest[0:len-1] to the negation of src[0:len-1]. Return true if overflow (i.e. if src is -2**(32*len-1)). Ok for src==dest.
-
setNegative
public void setNegative(IntNum x)
Destructively set this to the negative of x. It is OK if x==this.
-
setNegative
public final void setNegative()
Destructively negate this.
-
intLength
public int intLength()
Calculatesceiling(log2(this < 0 ? -this : this+1)). See Common Lisp: the Language, 2nd ed, p. 361.
-
writeExternal
public void writeExternal(ObjectOutput out) throws IOException
- Specified by:
writeExternalin interfaceExternalizable- Throws:
IOException
-
readExternal
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
- Specified by:
readExternalin interfaceExternalizable- Throws:
IOExceptionClassNotFoundException
-
readResolve
public Object readResolve() throws ObjectStreamException
- Throws:
ObjectStreamException
-
asBigInteger
public BigInteger asBigInteger()
-
asBigDecimal
public BigDecimal asBigDecimal()
- Overrides:
asBigDecimalin classRealNum
-
inRange
public boolean inRange(long lo, long hi)Is this integer both>= loand<= hi?.
-
inIntRange
public boolean inIntRange()
Does this value fit in a signed 32-bitint?
-
inLongRange
public boolean inLongRange()
Does this value fit in a signed 64-bitlong?
-
-