Implementing an arithmetic expression in Java (1234324234324 + 8938459043545)/5 + 343434343432.59845
Because the maximum number of variable values defined by long in the Java language is limited, such as 123456789987654321, an integer cannot be stored in a long variable, and if such two large numbers are added or multiplied, the result will be greater. For example, if you use long L = 1000000000 in the Java language, the definition is correct, but if you add 2000000000 to 1000000000+2000000000, the test result is-1294967296, a negative number. The reason is that the number of results is too large to represent the number of results. So what can be done to solve the above calculation problem?
Here we use array methods, such as
Long A = 123456789876
Long B = 123412341234
First we can define two arrays to hold these numbers in groups
Long ary_a[3];
Long ary_b[3];
The two numbers can be divided into three parts, each of which is four bits, which is:
ary_a [0] = 1234;
ary_a [1] = 5678;
ary_a [2] = 9876;
ary_b [0] = 1234;
Ary_b [1] = 1234;
Ary_b [2] = 1234;
The next thing is to carry it.
A small number, such as 38 + 25 decomposition
Single-digit addition 8+5=13 need to carry, the result of the digit is 3
10-bit Add 1+3+2 = 6
Can get a pattern
Number of rounding = 13/10
Digits left by bit = 13 10
The program is implemented as follows
public class Test1 {
public static void Main (string[] args) {
int a = 38;
int B = 25;
Int[] Ary_a = {3, 8};
Int[] Ary_b = {2, 5};
int r_1 = ary_a[0] + ary_b[0];
int r_2 = ary_a[1] + ary_b[1];
/* r_1 = 5, r_2 = 13 */
System.out.println (r_1);
System.out.println (r_2);
/* Rounding of results */
int carry = R_2/10;
SYSTEM.OUT.PRINTLN (carry);
/* Digit of the result */
int g = r_2% 10;
System.out.println (g);
/* 10-bit */
int s = carry+r_1;
System.out.println (s);
/* Results are 63*/
System.out.println (s+ "" +g);
}
}
If it is a three-digit number, etc.
A simple example is given below
Package com;
public class Bignumberoperation {
/* Add */
Public int[] Add (int[] F, int[] s, int n) {
/* Define remainder, initially 0*/
int carry = 0;
/* Define the returned array */
Int[] C = new Int[f.length];
/* Iterate through the incoming two arrays and calculate the */
for (int i = f.length-1; I >= 0; i--) {
C[i] = F[i] + s[i] + carry;
if (C[i] < n) {
carry = 0;
}
else {
C[i] = c[i]-N;
carry = 1;
}
}
return C;
}
/* multiplication, B is within the number */
Public int[] Mul (int[] A, int b) {
int carry = 0;
Int[] C = new Int[a.length];
if (b>10) {
return C;
}
for (int i = a.length-1; I >=0; i--) {
int tmp = a[i] * b + carry;
C[i] = tmp% 10;
carry = TMP/10;
}
return C;
}
/* string to Integer array */
Public int[] Getints (String s) {
int[] temp = null;
if (S!=null && s.length () >0) {
temp = new Int[s.length ()];
for (int i = 0; i < s.length (); i++) {
Temp[i] = Integer.parseint (s.substring (i,i+1));
}
}
return temp;
}
/* Test */
public static void Main (string[] args) {
Bignumberoperation bno = new Bignumberoperation ();
/* Two array of integers */
Int[] f = {1234,3242,3432,4};
Int[] s = {8938,4590,4354,5};
/* Call the two-number addition method */
int[] t = Bno.add (f, S, 100000);
StringBuffer sb = new StringBuffer ();
/* Results added to StringBuffer */
for (int j = 0; J < T.length; J + +) {
Sb.append (T[j]);
}
System.out.println (Sb.tostring ());
int[] bb = bno.getints (Sb.tostring ());
/* Multiplication */
bb = Bno.mul (BB, 5);
/* Output Results */
for (int i = 0; i < bb.length; i++) {
System.out.print (Bb[i]);
}
System.out.println ();
}
}
}
The decimal number on the back is simple.
343434343432.59845
In Java, you can also use the
Java.math.BigDecimal
Java.math.BigInteger
To perform a large number operation.
Java large number operations [go]