Tree processing in Java

Source: Internet
Author: User

in Javaapi There are two large numbers of classes: Java.math.BigInteger class and Java.math.BigDecimal class. Java.math.BigInteger class: An integer of immutable arbitrary precision. All operations are represented in twos complement form. BigInteger provides a counterpart to all Java's basic integer operators and provides all the relevant methods of Java.lang.Math. In addition, BigInteger provides the following operations: modulo arithmetic, GCD calculation, prime number test, prime generation, bit manipulation, and some other operations. Code implementation:
Import Java.math.biginteger;public class Bigintegerdemo {public static void main (string[] args) {BigInteger S=biginteger . valueOf (1); Returns the Bigintegerint n=50;//whose value equals the value of the specified long by 50 for the factorial for (int i=1;i<=n;i++) {s=s.multiply (biginteger.valueof (i));//returns its value to (Biginteger.valueof (i)) of Biginteger}system.out.println (s);}}
Java.math.BigDecimal class: A signed decimal number that is immutable and arbitrary in precision. The bigdecimal consists of an integer non-scale value of arbitrary precision and a 32-bit integer scale (scales: a 10-based exponent). The BigDecimal class provides the following operations: arithmetic, scale manipulation, rounding, comparison, hashing algorithm, and format conversion. The ToString () method provides a canonical representation of the BigDecimal. Code implementation:
Import Java.math.bigdecimal;public class Bigdecimaldemo {public static void main (string[] args) {//If the project encounters an operation that could lose precision, It is best to use BigDecimal to do//the high precision operation without BigDecimal is shown first, double sum=0;for (int i=0;i<10;i++) {sum+=0.1;} System.out.println ("sum=" +sum); The following shows the use of BigDecimal for high-precision operations BigDecimal D=new BigDecimal (0), for (int i=0;i<10;i++) {D=d.add (new BigDecimal (0.1));} System.out.println ("d=" +d.doublevalue ());d ouble dd=13.5654654645d; BigDecimal b=new BigDecimal (DD); B=b.divide (new BigDecimal (1.2), 2,bigdecimal.round_down);  Returns a BigDecimal with a value of (this/divisor) with a specified scale of System.out.println ("B +" +b.doublevalue ());}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Tree processing in Java

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.