Java Reference
In-Depth Information
for (int i=0; i<50; i++)
{
x = n.add(x.multiply(x)).divide(x.multiply(TWO), s,
BigDecimal.ROUND_DOWN);
if (x.compareTo(lastX) == 0)
break;
lastX = x;
}
return x;
}
}
On the Web
The BigDSqrt program is found in the Chapter 22 folder at www.crc-
press.com.
The BigInteger Class
Thejava.mathlibrarycontainstheBigIntegerclasswhichallowscreation,
manipulation,andbasicarithmeticonarbitrary-precisionintegers.Integer
numbersarelessusefulforscientificandengineeringapplicationsthanthe
DigDecimalclassdescribedpreviously.However,Java'sBigIntegerclassis
not a mirror image of its BigDecimal counterpart. BigInteger implements
operationsthatarenotavailablefordecimalnumbers,inadditiontotheel-
ementary ones that are available in both classes.
The BigInteger class provides methods that are analogs of the ones
that the language provides for primitive integer operators. In addition,
the BigInteger class provides operations for modular arithmetic, greatest
common divisor calculation, testing and generating prime numbers, bit
manipulations, and other operations not implemented for the primtive
types.
BigInteger numbers
Like the primitive integer data types, Java BigInteger numbers are repre-
sented in two's-complement form. Semantics of arithmetic operations are
similartotheonesinJavaintegerarithmetic.Forexample,divisionbyzero
throws an ArithmeticException, and division of a negative by a positive
yieldsanegative(orzero)remainder.Oneexceptionisthehandlingofover-
flow, which is not necessary for big integers since these numbers can be
made as large as necessary to accommodate the results of an operation.
Bitwise shift operators are the same ones used in Java primitive data
types. In this manner a right-shift with a negative shift distance results in
Search WWH ::




Custom Search