Package gnu.math
Class IntNum
- All Implemented Interfaces:
Externalizable
,Serializable
,Comparable
- Direct Known Subclasses:
XInteger
A class for infinite-precision integers.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionint
All integers are stored in 2's-complement form.int[]
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic IntNum
static final IntNum
add
(int x, int y) Add two ints, yielding an IntNum.static IntNum
Add an IntNum and an int, yielding a new IntNum.static IntNum
Add two IntNums, yielding their sum as another IntNum.static IntNum
Add two IntNums, yielding their sum as another IntNum.Return this + k * obj.static IntNum
alloc
(int nwords) Allocate a new non-shared IntNum.static IntNum
asIntNumOrNull
(Object value) static int
Return -1, 0, or 1, depending on which value is greater.static int
Return -1, 0, or 1, depending on which value is greater.int
Return an integer for which ofthis
orobj
is larger.final IntNum
static void
static void
Divide two integers, yielding quotient and remainder.double
The value of the real component, as a double.static boolean
boolean
void
format
(int radix, StringBuffer buffer) void
format
(int radix, StringBuilder buffer) static final int
gcd
(int a, int b) Calculate Greatest Common Divisor for non-negative ints.static IntNum
void
getAbsolute
(int[] words) Copy the abolute value of this into an array of words.int
hashCode()
boolean
Does this value fit in a signed 32-bitint
?boolean
Does this value fit in a signed 64-bitlong
?boolean
inRange
(long lo, long hi) Is this integer both>= lo
and<= hi
?.int
Calculatesceiling(log2(this < 0 ? -this : this+1))
.int
intValue()
static int
Cast an Object to an int.final boolean
final boolean
final boolean
isOdd()
final boolean
isOne()
final boolean
isZero()
static IntNum
long
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) static IntNum
minusOne()
Return the IntNum for -1.static IntNum
neg()
static IntNum
static boolean
negate
(int[] dest, int[] src, int len) Set dest[0:len-1] to the negation of src[0:len-1].final IntNum
static final IntNum
one()
Return this raised to an integer power.static IntNum
Calculate the integral power of an IntNum.static IntNum
static IntNum
void
void
realloc
(int nwords) Change words.length to nwords.static IntNum
static IntNum
double
roundToDouble
(int exp, boolean neg, boolean remainder) Convert a semi-processed IntNum to double.final void
set
(int y) Destructively set the value of this to an int.final void
set
(int[] words, int length) Destructively set the value of this to the given words.final void
set
(long y) Destructively set the value of this to a long.final void
Destructively set the value of this to that of y.final void
setAdd
(int y) Destructively add an int to this.void
Set this to the sum of x and y.final void
Destructively negate this.void
Destructively set this to the negative of x.static IntNum
int
sign()
Return 1 if>0
; 0 if==0
; -1 if<0
; -2 ifNaN
.static IntNum
Subtract two IntNums, yielding their sum as another IntNum.static final IntNum
ten()
static final IntNum
times
(int x, int y) Multiply two ints, yielding an IntNum.static final IntNum
static final IntNum
toExactInt
(int rounding_mode) Converts to an exact integer, with specified rounding mode.toInt
(int rounding_mode) Converts a real to an integer, according to a specified rounding mode.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
(int value) Return a (possibly-shared) IntNum with a given int value.static IntNum
valueOf
(long value) Return a (possibly-shared) IntNum with a given long value.static IntNum
static IntNum
static IntNum
valueOfUnsigned
(int value) Make an IntNum from an unsigned 32-bit value.static IntNum
valueOfUnsigned
(long value) Make an IntNum from an unsigned 64-bit value.static int
wordsNeeded
(int[] words, int len) Calculate how many words are significant in words[0:len-1].void
static final IntNum
zero()
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
-
Field Details
-
ival
public int ivalAll 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 Details
-
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 Details
-
zero
-
one
-
ten
-
minusOne
Return the IntNum for -1. -
asIntNumOrNull
-
alloc
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
-
denominator
- Specified by:
denominator
in classRatNum
-
isNegative
public final boolean isNegative()- Specified by:
isNegative
in classRealNum
-
sign
public int sign()Description copied from class:RealNum
Return 1 if>0
; 0 if==0
; -1 if<0
; -2 ifNaN
. -
compare
Return -1, 0, or 1, depending on which value is greater. -
compare
Return -1, 0, or 1, depending on which value is greater. -
compare
Description copied from class:Numeric
Return an integer for which ofthis
orobj
is larger. Return 1 ifthis>obj
; 0 ifthis==obj
; -1 ifthis<obj
; -2 ifthis!=obj
otherwise (for example if either is NaN); -3 if not comparable (incompatible types). -
isOdd
public final boolean isOdd() -
isZero
public final boolean isZero() -
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 valuex
such thatx>0 && words[0:x-1]==words[0:len-1]
, whenwords
is viewed as a 2's complement integer. -
canonicalize
-
add
Add two ints, yielding an IntNum. -
add
Add an IntNum and an int, yielding a new IntNum. -
setAdd
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
Destructively set the value of this to that of y. -
add
Add two IntNums, yielding their sum as another IntNum. -
sub
Subtract two IntNums, yielding their sum as another IntNum. -
add
Add two IntNums, yielding their sum as another IntNum. -
times
Multiply two ints, yielding an IntNum. -
times
-
times
-
divide
-
divide
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.
-
quotient
-
quotient
-
toExactInt
Description copied from class:RealNum
Converts to an exact integer, with specified rounding mode.- Overrides:
toExactInt
in classRatNum
-
toInt
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. -
remainder
-
remainder
-
modulo
-
power
Description copied from class:Numeric
Return this raised to an integer power. Implemented by repeated squaring and multiplication. Ify < 0
, returns div_inv of the result. -
power
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. -
gcd
-
lcm
-
shift
-
format
-
format
-
toString
-
intValue
public int intValue() -
intValue
Cast an Object to an int. The Object must (currently) be an IntNum. -
longValue
public long longValue()- Overrides:
longValue
in classQuaternion
-
hashCode
public int hashCode() -
equals
-
equals
-
make
Return a (possibly-shared) IntNum with a given int value. -
valueOfUnsigned
Make an IntNum from an unsigned 64-bit value. -
valueOfUnsigned
Make an IntNum from an unsigned 32-bit value. -
make
Make a canonicalized IntNum from an array of words. The array may be reused (without copying). -
make
-
make
-
valueOf
Return a (possibly-shared) IntNum with a given int value. -
valueOf
Return a (possibly-shared) IntNum with a given long value. -
valueOf
-
valueOf
- Throws:
NumberFormatException
-
valueOf
-
valueOf
- Throws:
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 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
Description copied from class:Numeric
Return this + k * obj. -
mul
-
div
-
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
Destructively set this to the negative of x. It is OK if x==this. -
setNegative
public final void setNegative()Destructively negate this. -
abs
-
neg
-
neg
-
intLength
public int intLength()Calculatesceiling(log2(this < 0 ? -this : this+1))
. See Common Lisp: the Language, 2nd ed, p. 361. -
writeExternal
- Specified by:
writeExternal
in interfaceExternalizable
- Throws:
IOException
-
readExternal
- Specified by:
readExternal
in interfaceExternalizable
- Throws:
IOException
ClassNotFoundException
-
readResolve
- Throws:
ObjectStreamException
-
asBigInteger
-
asBigDecimal
- Overrides:
asBigDecimal
in classRealNum
-
inRange
public boolean inRange(long lo, long hi) Is this integer both>= lo
and<= 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
?
-