For your
curiosity:
Java's
BigInteger and
BigDecimal classes
- The
largest
integer value
that can be
stored in a
primitive
data type
is:
Long.MAX_VALUE = 9223372036854775807
|
- The
largest
decimal/floating point
value
that can be
primitive
stored in a data type
is:
Double.MAX_VALUE = 1.7976931348623157 E 308
|
-
$64,000 question:
- What if you need to
use
even larger
numbers ???
|
-
Answer:
- You must write
algorithms that can
perform
computation (+, -, *, /)
on a
portion of
a number
|
|
How can you extend computations on
larger number ?
How can you extend computations on
larger number ?
How can you extend computations on
larger number ?
How can you extend computations on
larger number ?
How can you extend computations on
larger number ?
How can you extend computations on
larger number ?
Java's
BigInteger
and
BigDecimal
classes
DEMO:
demo/12-wrapper/03-big-numbers/BigIntegers.java
Arithmetic operations with
BigInteger objects
- The BigInteger
class contain
instance methods that
perform
airthmetic operations:
add( x ): returns this BigInteger object + BigNumber object x
subtract( x ): returns this BigInteger object - BigNumber object x
multiply( x ): returns this BigInteger object × BigNumber object x
divide( x ): returns this BigInteger object / BigNumber object x
remainder( x ): returns this BigInteger object % BigNumber object x
|
- The BigInteger
methods will
return a
BigInteger object
as result
- Example:
BigInteger a = new BigInteger("123456789123456789123");
BigInteger b = new BigInteger("999999999999999999999");
BigInteger c;
c = a.add(b);
|
|
DEMO:
demo/12-wrapper/03-big-numbers/Arithmetic.java
Application
computing factorial for
large numbers
- Recall the
factorial of
n! is
define as:
- We can write the
factorial
method with a
for-loop as
follows:
public static long factorial(long n)
{
long result = 1;
for ( int i = 1; i <= n; i++ )
{
result = result * i;
}
return result;
}
|
-
However:
we can
only
compute the factorial for
numbers
≤ 20 because
the
largest
integer number in
Java is
9223372036854775807
|
DEMO:
demo/12-wrapper/03-big-numbers/Factorial.java
Application
computing factorial for
large numbers
Application
computing factorial for
large numbers
Application
computing factorial for
large numbers
Application
computing factorial for
large numbers
- We then
multiply
the 2
BigInteger objects
(result and iBig) and
update
result:
BigInteger
public static long factorial(long n)
{
BigInteger result = new BigInteger( "1") ;
for ( int i = 1; i <= n; i++ )
{
BigInteger iBig = new BigInteger( "" + i); // int i --> String
result = result.multiply(iBig);
}
return result;
}
|
Done !
|
DEMO:
demo/12-wrapper/03-big-numbers/BigFactorial.java
DEMO:
demo/12-wrapper/03-big-numbers/BigDecimal.java
BigDecimal
division
that never ends
-
Caveat
using BigDecimal
numbers:
- A
division
can produce in an
infinite number of
digits !!
|
- Example: the
division of
1/3
using
BigDecimal
will cause a
runtime error:
import java.math.BigDecimal;
public class DivisionError
{
public static void main(String[] args)
{
BigDecimal a = new BigDecimal("1");
BigDecimal b = new BigDecimal("3");
BigDecimal c;
c = a.divide(b); // 0.33333333...
// ArithmeticException: Non-terminating decimal expansion
}
}
|
|
DEMO:
demo/12-wrapper/03-big-numbers/InfinteDiv.java
Limiting the
number of digits
in a BigDecimal division
- We can avoid the
infinite dvision
error by
specifying the
number of
digit
(=
scale) of the
result by using:
a.divide(b) ---> a.divide(b, scale, roundingMode.HALF_UP)
scale = # digits in the result of the division
roundingMode.HALF_UP: rounds 0.5 in the last digit up
|
Example:
public static void main(String[] args)
{
BigDecimal a = new BigDecimal("1");
BigDecimal b = new BigDecimal("3");
BigDecimal c;
c = a.divide(b, 20, RoundingMode.HALF_UP);
System.out.println(c);
}
|
|
DEMO:
demo/12-wrapper/03-big-numbers/InfinteDivFix.java
❮
❯