Big Data operations, Java (BigInteger)

Source: Internet
Author: User
Tags arithmetic

Big Data OperationsBigInteger

The long type in Java is the largest integer type, and how to represent data that is longer than long. In the world of Java, integers longer than long can no longer be called integers, and they are encapsulated as BigInteger objects. In the BigInteger class, Implementing arithmetic is implemented by methods, not by operators.

How to construct the BigInteger class:

BigInteger b = new BigInteger (str);

In the constructor method, an integer is given in the form of a string

Arithmetic code:

public static void Main (string[] args) {

Big Data encapsulation for BigInteger objects

BigInteger big1 = new BigInteger ("12345678909876543210");

BigInteger big2 = new BigInteger ("98765432101234567890");

Add to implement the addition operation

BigInteger Bigadd = big1. Add (BIG2);

Subtract implementing the subtraction operation

BigInteger bigsub = big1. Subtract (BIG2);

Multiply implementing multiplication operations

BigInteger Bigmul = big1. Multiply (BIG2);

Divide implementing Division operations

BigInteger Bigdiv = Big2. Divide (BIG1);

}

BigDecimal

What happens when you execute the following code in your program?

System. Out.println (0.09 + 0.01);

System. out.println (1.0-0.32);

System. out.println (1.015 *);

System. out.println (1.301/100);

double and float types are prone to loss of precision in operations, resulting in inaccurate data, and Java provides our BigDecimal class with high-precision operations that enable floating-point data

The construction method is as follows:

BigDecimal B = new BigDecimal (str);

It is recommended that floating-point data be given as a string because the parameter results are predictable

Implement the addition subtraction multiplication code as follows: ( the Operation method is consistent with BigInteger )

public static void Main (string[] args) {

Big Data encapsulation for BigDecimal objects

BigDecimal big1 = new BigDecimal ("0.09");

BigDecimal big2 = new BigDecimal ("0.01");

Add to implement the addition operation

BigDecimal Bigadd = Big1.add (BIG2);

BigDecimal big3 = new BigDecimal ("1.0");

BigDecimal big4 = new BigDecimal ("0.32");

Subtract implementing the subtraction operation

BigDecimal bigsub = big3.subtract (BIG4);

BigDecimal Big5 = new BigDecimal ("1.105");

BigDecimal big6 = new BigDecimal ("100");

Multiply implementing multiplication operations

BigDecimal Bigmul = big5.multiply (BIG6);

For the division operation of floating-point data, unlike integers, there may be infinite repeating decimal , so you need to preserve and select rounding mode for the number of digits you need

BigDecimal B = big1.divide (big2, number of digits after the decimal point, rounding mode)

BigDecimal B = big1.divide. (Big2,2,bigdecimal.round_down)

Big Data operations, Java (BigInteger)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.