gnu.math
Class IntNum

java.lang.Object
  extended by java.lang.Number
      extended by gnu.math.Numeric
          extended by gnu.math.Quantity
              extended by gnu.math.Complex
                  extended by gnu.math.RealNum
                      extended by gnu.math.RatNum
                          extended by gnu.math.IntNum
All Implemented Interfaces:
java.io.Externalizable, java.io.Serializable, java.lang.Comparable
Direct Known Subclasses:
XInteger

public class IntNum
extends RatNum
implements java.io.Externalizable

A class for infinite-precision integers.

See Also:
Serialized Form

Field Summary
 int ival
          All integers are stored in 2's-complement form.
 int[] words
           
 
Fields inherited from class gnu.math.Numeric
CEILING, FLOOR, ROUND, TRUNCATE
 
Constructor Summary
IntNum()
           
IntNum(int value)
          Create a new (non-shared) IntNum, and initialize to an int.
 
Method Summary
static IntNum abs(IntNum x)
           
static IntNum add(int x, int y)
          Add two ints, yielding an IntNum.
static IntNum add(IntNum x, int y)
          Add an IntNum and an int, yielding a new IntNum.
static IntNum add(IntNum x, IntNum y)
          Add two IntNums, yielding their sum as another IntNum.
static IntNum add(IntNum x, IntNum y, int k)
          Add two IntNums, yielding their sum as another IntNum.
 Numeric add(java.lang.Object y, int k)
          Return this + k * obj.
static IntNum alloc(int nwords)
          Allocate a new non-shared IntNum.
 java.math.BigDecimal asBigDecimal()
           
 java.math.BigInteger asBigInteger()
           
 IntNum canonicalize()
           
static int compare(IntNum x, IntNum y)
          Return -1, 0, or 1, depending on which value is greater.
static int compare(IntNum x, long y)
          Return -1, 0, or 1, depending on which value is greater.
 int compare(java.lang.Object obj)
          Return an integer for which of {# code this} or {#code obj} is larger.
 IntNum denominator()
           
 Numeric div(java.lang.Object y)
           
static void divide(IntNum x, IntNum y, IntNum quotient, IntNum remainder, int rounding_mode)
          Divide two integers, yielding quotient and remainder.
static void divide(long x, long y, IntNum quotient, IntNum remainder, int rounding_mode)
           
 double doubleValue()
          The value of the real component, as a double.
static boolean equals(IntNum x, IntNum y)
           
 boolean equals(java.lang.Object obj)
           
 void format(int radix, java.lang.StringBuffer buffer)
           
static int gcd(int a, int b)
          Calculate Greatest Common Divisor for non-negative ints.
static IntNum gcd(IntNum x, IntNum y)
           
 void getAbsolute(int[] words)
          Copy the abolute value of this into an array of words.
 int hashCode()
           
 int intLength()
          Calculates ceiling(log2(this < 0 ? -this : this+1)).
 int intValue()
           
static int intValue(java.lang.Object obj)
          Cast an Object to an int.
 boolean isMinusOne()
           
 boolean isNegative()
           
 boolean isOdd()
           
 boolean isOne()
           
 boolean isZero()
           
static IntNum lcm(IntNum x, IntNum y)
           
 long longValue()
           
static IntNum make(int value)
          Return a (possibly-shared) IntNum with a given int value.
static IntNum make(int[] words)
           
static IntNum make(int[] words, int len)
          Make a canonicalized IntNum from an array of words.
static IntNum make(long value)
          Return a (possibly-shared) IntNum with a given long value.
static IntNum makeU(long value)
          Make an IntNum from an unsigned 64-bit value.
static IntNum minusOne()
          Return the IntNum for -1.
static IntNum modulo(IntNum x, IntNum y)
           
 Numeric mul(java.lang.Object y)
           
 Numeric neg()
           
static IntNum neg(IntNum x)
           
static boolean negate(int[] dest, int[] src, int len)
          Set dest[0:len-1] to the negation of src[0:len-1].
 IntNum numerator()
           
static IntNum one()
           
 Numeric power(IntNum y)
          Return this raised to an integer power.
static IntNum power(IntNum x, int y)
          Calculate the integral power of an IntNum.
static IntNum quotient(IntNum x, IntNum y)
           
static IntNum quotient(IntNum x, IntNum y, int rounding_mode)
           
 void readExternal(java.io.ObjectInput in)
           
 java.lang.Object readResolve()
           
 void realloc(int nwords)
          Change words.length to nwords.
static IntNum remainder(IntNum x, IntNum y)
           
 double roundToDouble(int exp, boolean neg, boolean remainder)
          Convert a semi-processed IntNum to double.
 void set(int y)
          Destructively set the value of this to an int.
 void set(int[] words, int length)
          Destructively set the value of this to the given words.
 void set(IntNum y)
          Destructively set the value of this to that of y.
 void set(long y)
          Destructively set the value of this to a long.
 void setAdd(int y)
          Destructively add an int to this.
 void setAdd(IntNum x, int y)
          Set this to the sum of x and y.
 void setNegative()
          Destructively negate this.
 void setNegative(IntNum x)
          Destructively set this to the negative of x.
static IntNum shift(IntNum x, int count)
           
 int sign()
          Return 1 if >0; 0 if ==0; -1 if <0; -2 if NaN.
static IntNum sub(IntNum x, IntNum y)
          Subtract two IntNums, yielding their sum as another IntNum.
static IntNum ten()
           
static IntNum times(int x, int y)
          Multiply two ints, yielding an IntNum.
static IntNum times(IntNum x, int y)
           
static IntNum times(IntNum x, IntNum y)
           
 IntNum toExactInt(int rounding_mode)
          Converts to an exact integer, with specified rounding mode.
 RealNum toInt(int rounding_mode)
          Converts a real to an integer, according to a specified rounding mode.
 java.lang.String toString(int radix)
           
static IntNum valueOf(byte[] digits, int byte_len, boolean negative, int radix)
           
static IntNum valueOf(char[] buf, int offset, int length, int radix, boolean negative)
           
static IntNum valueOf(java.lang.String s)
           
static IntNum valueOf(java.lang.String s, int radix)
           
static int wordsNeeded(int[] words, int len)
          Calculate how many words are significant in words[0:len-1].
 void writeExternal(java.io.ObjectOutput out)
           
static IntNum zero()
           
 
Methods inherited from class gnu.math.RatNum
add, compare, divide, equals, infinity, isExact, make, rationalize, times, toExact
 
Methods inherited from class gnu.math.RealNum
abs, add, compareTo, divide, exp, im, log, max, min, re, rneg, sin, sqrt, times, toExactInt, toExactInt, toInt, toScaledInt, toScaledInt, toScaledInt, toStringDecimal, toStringScientific, toStringScientific, toStringScientific, toStringScientific
 
Methods inherited from class gnu.math.Complex
add, addReversed, angle, compare, divide, divReversed, doubleImagValue, doubleRealValue, equals, imMinusOne, imOne, make, make, mulReversed, neg, number, polar, polar, power, times
 
Methods inherited from class gnu.math.Quantity
add, compare, compareReversed, dimensions, divide, imValue, make, make, make, reValue, times, unit
 
Methods inherited from class gnu.math.Numeric
add, div_inv, floatValue, geq, grt, mul_ident, sub, toString
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

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

make

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


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.


make

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


makeU

public static IntNum makeU(long value)
Make an IntNum from an unsigned 64-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)

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.


numerator

public final IntNum numerator()
Specified by:
numerator in class RatNum

denominator

public final IntNum denominator()
Specified by:
denominator in class RatNum

isNegative

public final boolean isNegative()
Specified by:
isNegative in class RealNum

sign

public int sign()
Description copied from class: RealNum
Return 1 if >0; 0 if ==0; -1 if <0; -2 if NaN.

Specified by:
sign in class RealNum

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(java.lang.Object obj)
Description copied from class: Numeric
Return an integer for which of {# code this} or {#code obj} is larger. Return 1 if this>obj; 0 if this==obj; -1 if this<obj; -2 if this!=obj otherwise (for example if either is NaN); -3 if not comparable (incompatible types).

Overrides:
compare in class Complex

isOdd

public final boolean isOdd()

isZero

public final boolean isZero()
Overrides:
isZero in class RatNum

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 value x such that x>0 && words[0:x-1]==words[0:len-1], when words is 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.


add

public static IntNum add(IntNum x,
                         int y)
Add an IntNum and an int, yielding a new 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.


times

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

times

public static final IntNum times(IntNum x,
                                 IntNum 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.

quotient

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

quotient

public static IntNum quotient(IntNum x,
                              IntNum y)

toExactInt

public IntNum toExactInt(int rounding_mode)
Description copied from class: RealNum
Converts to an exact integer, with specified rounding mode.

Overrides:
toExactInt in class RatNum

toInt

public RealNum toInt(int rounding_mode)
Description copied from class: RealNum
Converts a real to an integer, according to a specified rounding mode. Note an inexact argument gives an inexact result, following Scheme. See also toExactInt.

Overrides:
toInt in class RatNum

remainder

public static IntNum remainder(IntNum x,
                               IntNum y)

modulo

public static IntNum modulo(IntNum x,
                            IntNum y)

power

public Numeric power(IntNum y)
Description copied from class: Numeric
Return this raised to an integer power. Implemented by repeated squaring and multiplication. If y < 0, returns div_inv of the result.

Overrides:
power in class RatNum

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)

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)

lcm

public static IntNum lcm(IntNum x,
                         IntNum y)

shift

public static IntNum shift(IntNum x,
                           int count)

format

public void format(int radix,
                   java.lang.StringBuffer buffer)

toString

public java.lang.String toString(int radix)
Overrides:
toString in class Complex

intValue

public int intValue()
Overrides:
intValue in class Numeric

intValue

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


longValue

public long longValue()
Overrides:
longValue in class Complex

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public static boolean equals(IntNum x,
                             IntNum y)

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class RatNum

valueOf

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

valueOf

public static IntNum valueOf(java.lang.String s,
                             int radix)
                      throws java.lang.NumberFormatException
Throws:
java.lang.NumberFormatException

valueOf

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

valueOf

public static IntNum valueOf(java.lang.String s)
                      throws java.lang.NumberFormatException
Throws:
java.lang.NumberFormatException

doubleValue

public double doubleValue()
Description copied from class: Quantity
The value of the real component, as a double. This is relative to the unit().dims - i.e. unit()/doubleValue() is factored in.

Overrides:
doubleValue in class Complex

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.

add

public Numeric add(java.lang.Object y,
                   int k)
Description copied from class: Numeric
Return this + k * obj.

Specified by:
add in class RealNum

mul

public Numeric mul(java.lang.Object y)
Specified by:
mul in class RealNum

div

public Numeric div(java.lang.Object y)
Specified by:
div in class RealNum

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.


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.


abs

public static IntNum abs(IntNum x)

neg

public static IntNum neg(IntNum x)

neg

public Numeric neg()
Overrides:
neg in class Complex

intLength

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


writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Specified by:
writeExternal in interface java.io.Externalizable
Throws:
java.io.IOException

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Specified by:
readExternal in interface java.io.Externalizable
Throws:
java.io.IOException
java.lang.ClassNotFoundException

readResolve

public java.lang.Object readResolve()
                             throws java.io.ObjectStreamException
Throws:
java.io.ObjectStreamException

asBigInteger

public java.math.BigInteger asBigInteger()

asBigDecimal

public java.math.BigDecimal asBigDecimal()
Overrides:
asBigDecimal in class RealNum