Large number processing

Source: Internet
Author: User
Tags greatest common divisor

In Java, a variety of operations are encapsulated in the BigInteger class, with the exception of the basic add, subtract, multiply, and divide operations, as well as the absolute, inverse, greatest common divisor, and the determination of prime numbers. When you use the BigInteger class, you can instantiate a BigInteger object and automatically invoke the appropriate constructor. The BigInteger class has many constructors, but the most straightforward is that the parameters represent the numbers to be processed in string form;

BigDecimal and BigInteger can achieve large numbers of operations, the difference is that BigDecimal added the concept of decimals. The general float type and double type data can only be used for scientific calculations or engineering calculations, but because of the high number of digital accuracy required in commercial computing, the Java.math.BigDecimal class is used. The BigDecimal class supports fixed-point numbers of any precision that can be used to accurately calculate currency values.

Cases:

1  Public classBigintegerdemo {2      Public Static voidMain (string[] args) {3BigInteger biginstance =NewBigInteger ("4");//instantiate a large number4         //take this big number plus 2 operation5SYSTEM.OUT.PRINTLN ("addition operation:" + biginstance.add (NewBigInteger ("2")));6         //take this big number minus 2 operation7System.out.println ("Subtraction Operation:"8+ Biginstance.subtract (NewBigInteger ("2")));9         //multiply the large number by the 2 operationTenSystem.out.println ("Multiplication Operation:" One+ biginstance.multiply (NewBigInteger ("2"))); A         //take the large number divided by 2 operation -SYSTEM.OUT.PRINTLN ("Division Operation:" -+ Biginstance.divide (NewBigInteger ("2"))); the         //Divide the large number by the quotient of 3 -System.out.println ("Pick-up:" -+ Biginstance.divideandremainder (NewBigInteger ("3")) [0]); -         //Divide the large number by the remainder of 3 +System.out.println ("Take remainder:" -+ Biginstance.divideandremainder (NewBigInteger ("3")) [1]); +         //take the big number 2 times ASystem.out.println ("Do 2 square Operations:" + BIGINSTANCE.POW (2)); at         //take the opposite number of the large number -System.out.println ("Take the opposite number of actions:" +biginstance.negate ()); -     } -}

Operation Result:

Addition Operation: 6
Subtraction Operation: 2
Multiplication Operation: 8
Division Operation: 2
Pick-up: 1
Take remainder: 1
Do 2 square Operations: 16
Take the opposite number operation: 4

Large number processing

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.