gnu.math

Class IntNum

public class IntNum extends RatNum implements Externalizable

A class for infinite-precision integers.
Field Summary
intival
All integers are stored in 2's-complement form.
int[]words
Constructor Summary
IntNum()
IntNum(int value)
Create a new (non-shared) IntNum, and initialize to an int.
Method Summary
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)
static IntNumalloc(int nwords)
Allocate a new non-shared IntNum.
BigDecimalasBigDecimal()
BigIntegerasBigInteger()
IntNumcanonicalize()
static intcompare(IntNum x, IntNum y)
Return -1, 0, or 1, depending on which value is greater.
static intcompare(IntNum x, long y)
Return -1, 0, or 1, depending on which value is greater.
intcompare(Object obj)
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()
static booleanequals(IntNum x, IntNum y)
booleanequals(Object obj)
voidformat(int radix, StringBuffer 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()
intintLength()
Calculates {@code ceiling(log2(this < 0 ?
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(long value)
Return a (possibly-shared) IntNum with a given long value.
static IntNummake(int[] words, int len)
Make a canonicalized IntNum from an array of words.
static IntNummake(int[] words)
static IntNummakeU(long value)
Make an IntNum from an unsigned 64-bit value.
static IntNumminusOne()
Return the IntNum for -1.
static IntNummodulo(IntNum x, IntNum y)
Numericmul(Object y)
static IntNumneg(IntNum x)
Numericneg()
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)
static IntNumpower(IntNum x, int y)
Calculate the integral power of an IntNum.
static IntNumquotient(IntNum x, IntNum y, int rounding_mode)
static IntNumquotient(IntNum x, IntNum y)
voidreadExternal(ObjectInput in)
ObjectreadResolve()
voidrealloc(int nwords)
Change words.length to nwords.
static IntNumremainder(IntNum x, IntNum y)
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(long y)
Destructively set the value of this to a long.
voidset(int[] words, int length)
Destructively set the value of this to the given words.
voidset(IntNum y)
Destructively set the value of this to that of y.
voidsetAdd(IntNum x, int y)
Set this to the sum of x and y.
voidsetAdd(int y)
Destructively add an int to this.
voidsetNegative(IntNum x)
Destructively set this to the negative of x.
voidsetNegative()
Destructively negate this.
static IntNumshift(IntNum x, int count)
intsign()
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)
RealNumtoInt(int rounding_mode)
StringtoString(int radix)
static IntNumvalueOf(char[] buf, int offset, int length, int radix, boolean negative)
static IntNumvalueOf(String s, int radix)
static IntNumvalueOf(byte[] digits, int byte_len, boolean negative, int radix)
static IntNumvalueOf(String s)
static intwordsNeeded(int[] words, int len)
Calculate how many words are significant in words[0:len-1].
voidwriteExternal(ObjectOutput out)
static IntNumzero()

Field Detail

ival

public int ival
All integers are stored in 2's-complement form. If words == null, the ival is the value of this IntNum. Otherwise, the first ival elements of words make the value of this IntNum, stored in little-endian order, 2's-complement form.

words

public int[] words

Constructor Detail

IntNum

public IntNum()

IntNum

public IntNum(int value)
Create a new (non-shared) IntNum, and initialize to an int.

Parameters: value the initial value

Method Detail

abs

public static IntNum abs(IntNum x)

add

public static final IntNum add(int x, int y)
Add two ints, yielding an IntNum.

add

public static IntNum add(IntNum x, int y)
Add an IntNum and an int, yielding a new IntNum.

add

public static IntNum add(IntNum x, IntNum y)
Add 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.

add

public Numeric add(Object y, int k)

alloc

public static IntNum alloc(int nwords)
Allocate a new non-shared IntNum.

Parameters: nwords number of words to allocate

asBigDecimal

public BigDecimal asBigDecimal()

asBigInteger

public BigInteger asBigInteger()

canonicalize

public IntNum canonicalize()

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)

denominator

public final IntNum denominator()

div

public Numeric div(Object y)

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 division y the denominator in the division quotient 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.

doubleValue

public double doubleValue()

equals

public static boolean equals(IntNum x, IntNum y)

equals

public boolean equals(Object obj)

format

public void format(int radix, StringBuffer buffer)

gcd

public static final int gcd(int a, int b)
Calculate Greatest Common Divisor for non-negative ints.

gcd

public static IntNum gcd(IntNum x, IntNum y)

getAbsolute

public void getAbsolute(int[] words)
Copy the abolute value of this into an array of words. Assumes words.length >= (this.words == null ? 1 : this.ival). Result is zero-extended, but need not be a valid 2's complement number.

hashCode

public int hashCode()

intLength

public int intLength()
Calculates {@code ceiling(log2(this < 0 ? -this : this+1))}. See Common Lisp: the Language, 2nd ed, p. 361.

intValue

public int intValue()

intValue

public static int intValue(Object obj)
Cast an Object to an int. The Object must (currently) be an IntNum.

isMinusOne

public final boolean isMinusOne()

isNegative

public final boolean isNegative()

isOdd

public final boolean isOdd()

isOne

public final boolean isOne()

isZero

public final boolean isZero()

lcm

public static IntNum lcm(IntNum x, IntNum y)

longValue

public long longValue()

make

public static IntNum make(int value)
Return a (possibly-shared) IntNum with a given int value.

make

public static IntNum make(long value)
Return a (possibly-shared) IntNum with a given long 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)

makeU

public static IntNum makeU(long value)
Make an IntNum from an unsigned 64-bit value.

minusOne

public static IntNum minusOne()
Return the IntNum for -1.

modulo

public static IntNum modulo(IntNum x, IntNum y)

mul

public Numeric mul(Object y)

neg

public static IntNum neg(IntNum x)

neg

public Numeric neg()

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.

numerator

public final IntNum numerator()

one

public static final IntNum one()

power

public Numeric power(IntNum y)

power

public static IntNum power(IntNum x, int y)
Calculate the integral power of an IntNum.

Parameters: x the value (base) to exponentiate y the exponent (must be non-negative)

quotient

public static IntNum quotient(IntNum x, IntNum y, int rounding_mode)

quotient

public static IntNum quotient(IntNum x, IntNum y)

readExternal

public void readExternal(ObjectInput in)

readResolve

public Object readResolve()

realloc

public void realloc(int nwords)
Change words.length to nwords. We allow words.length to be upto nwords+2 without reallocating.

remainder

public static IntNum remainder(IntNum x, IntNum y)

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 multiply neg true if negative remainder 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.

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.

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.

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.

shift

public static IntNum shift(IntNum x, int count)

sign

public int sign()

sub

public static IntNum sub(IntNum x, IntNum y)
Subtract two IntNums, yielding their sum as another IntNum.

ten

public static final IntNum ten()

times

public static final IntNum times(int x, int y)
Multiply two ints, yielding an IntNum.

times

public static final IntNum times(IntNum x, int y)

times

public static final IntNum times(IntNum x, IntNum y)

toExactInt

public IntNum toExactInt(int rounding_mode)

toInt

public RealNum toInt(int rounding_mode)

toString

public String toString(int radix)

valueOf

public static IntNum valueOf(char[] buf, int offset, int length, int radix, boolean negative)

valueOf

public static IntNum valueOf(String s, int radix)

valueOf

public static IntNum valueOf(byte[] digits, int byte_len, boolean negative, int radix)

valueOf

public static IntNum valueOf(String s)

wordsNeeded

public static int wordsNeeded(int[] words, int len)
Calculate how many words are significant in words[0:len-1]. Returns the least value x such that x>0 && words[0:x-1]==words[0:len-1], when words is viewed as a 2's complement integer.

writeExternal

public void writeExternal(ObjectOutput out)

Serial Data: If the value is in the range (int)0xC000000 .. 0x7fffffff (inclusive) write out the value (using writeInt). Otherwise, write (using writeInt) (0x80000000|nwords), where nwords is the number of words following. The words are the minimal 2's complement big-endian representation of the value, written using writeint. (Even if the current value is not canonicalized, the output is).

zero

public static final IntNum zero()