BigInteger and BigDecimal
Arbitrary mathematical calculations cannot be represented with Java’s primitive data types like float and double. Due to this the java.math package provides BigInteger and BigDecimal.
BigInteger supports arbitrary-precision integers. This means it can accurately represent integral values of any size without losing any information during operations.
For example,
import java.math.*;
class Blog6 {
public static void main(String args[]) {
// Create BigInteger with a string
BigInteger bi1 = new BigInteger("1234567890123456890");
/* float f=1234567890123456890; - will show a compilation error as number is too large */
// Create BigInteger with a long
BigInteger bi2 = BigInteger.valueOf(123L);
bi1 = bi1.add(bi2);
System.out.println(bi1);
bi1 = bi1.multiply(bi2);
System.out.println(bi1);
bi1 = bi1.subtract(bi2);
System.out.println(bi1);
bi1 = bi1.divide(bi2);
System.out.println(bi1);
bi1 = bi1.negate();
System.out.println(bi1);
int exponent = 2;
bi1 = bi1.pow(exponent);
System.out.println(bi1);
}
}
The output of the above program will be :-
1234567890123457013
151851850485185212599
151851850485185212476
1234567890123457012
-1234567890123457012
1524157875323884225636330993811968144
BigDecimal is for arbitrary-precision fixed-point numbers.
For example,
import java.math.*;
class Blog61 {
public static void main(String args[]) {
BigDecimal bdi1 = new BigDecimal("1234567890123.456890");
/*float f=1234567890123.456890; - will show a compilation error due to loss of precision */
// Create via a long
BigDecimal bd2 = BigDecimal.valueOf(123L,2);
bd1 = bd1.add(bd2);
System.out.println(bd1);
bd1 = bd1.multiply(bd2);
System.out.println(bd1);
bd1 = bd1.subtract(bd2);
System.out.println(bd1);
bd1 = bi1.divide(bd2,6);
System.out.println(bd1);
bd1 = bdi1.negate();
System.out.println(bd1);
}
}
The output of the above program will be :-
1234567890124.686890
1518518504853.36487470
1518518504852.13487470
1234567890123.68689000
-1234567890123.68689000
Calculations with such large numbers with such huge decimal values are not possible with any of the primitive datatypes in java like int, float or double. But the BigInteger and BigDecimal classes allows the user to do calculations with such number as shown.
Arbitrary mathematical calculations cannot be represented with Java’s primitive data types like float and double. Due to this the java.math package provides BigInteger and BigDecimal.
BigInteger supports arbitrary-precision integers. This means it can accurately represent integral values of any size without losing any information during operations.
For example,
import java.math.*;
class Blog6 {
public static void main(String args[]) {
// Create BigInteger with a string
BigInteger bi1 = new BigInteger("1234567890123456890");
/* float f=1234567890123456890; - will show a compilation error as number is too large */
// Create BigInteger with a long
BigInteger bi2 = BigInteger.valueOf(123L);
bi1 = bi1.add(bi2);
System.out.println(bi1);
bi1 = bi1.multiply(bi2);
System.out.println(bi1);
bi1 = bi1.subtract(bi2);
System.out.println(bi1);
bi1 = bi1.divide(bi2);
System.out.println(bi1);
bi1 = bi1.negate();
System.out.println(bi1);
int exponent = 2;
bi1 = bi1.pow(exponent);
System.out.println(bi1);
}
}
The output of the above program will be :-
1234567890123457013
151851850485185212599
151851850485185212476
1234567890123457012
-1234567890123457012
1524157875323884225636330993811968144
BigDecimal is for arbitrary-precision fixed-point numbers.
For example,
import java.math.*;
class Blog61 {
public static void main(String args[]) {
BigDecimal bdi1 = new BigDecimal("1234567890123.456890");
/*float f=1234567890123.456890; - will show a compilation error due to loss of precision */
// Create via a long
BigDecimal bd2 = BigDecimal.valueOf(123L,2);
bd1 = bd1.add(bd2);
System.out.println(bd1);
bd1 = bd1.multiply(bd2);
System.out.println(bd1);
bd1 = bd1.subtract(bd2);
System.out.println(bd1);
bd1 = bi1.divide(bd2,6);
System.out.println(bd1);
bd1 = bdi1.negate();
System.out.println(bd1);
}
}
The output of the above program will be :-
1234567890124.686890
1518518504853.36487470
1518518504852.13487470
1234567890123.68689000
-1234567890123.68689000
Calculations with such large numbers with such huge decimal values are not possible with any of the primitive datatypes in java like int, float or double. But the BigInteger and BigDecimal classes allows the user to do calculations with such number as shown.
No comments:
Post a Comment