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