Java large number multiplication simple implementation of floating point number multiplication

  • 2020-04-01 02:52:09
  • OfStack


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class BigNumber {
    public static void main(String[] args) throws IOException {
        System.out.println("Input two large integers:");
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        String[] strArray = buffer.readLine().split("\*");
        System.out.println(bigNumberMultiply(strArray[0], strArray[1]));
    }

    
    private static String bigNumberMultiply(String first, String second) {
        //Sign judgment mark
        boolean flag = false;

        if (first.charAt(0) == '-') {
            flag = !flag;
            first = first.substring(1);
        }

        if (second.charAt(0) == '-') {
            flag = !flag;
            second = second.substring(1);
        }

        //Position of the decimal point
        int aPoints = first.length() - first.indexOf('.') - 1;
        int bPoints = second.length() - second.indexOf('.') - 1;
        int pointPos = aPoints + bPoints; //The decimal position of the result

        //Delete decimal point
        StringBuffer aBuffer = new StringBuffer(first.replaceAll("\.", ""));
        StringBuffer bBuffer = new StringBuffer(second.replaceAll("\.", ""));

        int[] a = string2IntArray(aBuffer.toString());
        int[] b = string2IntArray(bBuffer.toString());

        int[] result = new int[a.length + b.length - 1]; //An array that holds the results

        //To calculate
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < b.length; j++) {
                result[i + j] += a[i] * b[j];
            }
        }

        //If one of the bits in result is greater than 9, carry
        for (int i = result.length - 1; i >= 0; --i) {
            if (result[i] > 9) {
                result[i - 1] += result[i] / 10;
                result[i] = result[i] % 10;
            }
        }

        StringBuffer buffer = new StringBuffer(); //Converts the result array to a string
        for (int i = 0; i < result.length; ++i) {
            //Add a decimal point
            if(result.length - i == pointPos) {
                buffer.append(".");
            }
            buffer.append(String.valueOf(result[i]));
        }

        if (buffer.indexOf(".") != -1)
        {
            //Delete the first 0
            int i = 0;
            while (i < buffer.length()) {
                if (buffer.length() > 2 && buffer.charAt(i+1) == '.') { //There's only one zero in front of the decimal point.
                    break;
                } else if (buffer.charAt(i) == '0') { //Delete the leading 0
                    buffer.deleteCharAt(i);
                    i = 0;
                    continue;
                } else { //When the first one is not zero
                    break;
                }
            }

            //Delete the 0 at the end
            i = buffer.length() - 1;
            while (i >= 0) {
                if (buffer.length() > 2 && buffer.charAt(i-1) == '.') { //After the decimal point it's just a number
                    break;
                } else if (buffer.charAt(i) == '0') { //Delete the 0 at the end
                    buffer.deleteCharAt(i);
                    i = buffer.length() - 1;
                    continue;
                } else { //When the last digit is not zero
                    break;
                }
            }
        }

        //Returns a plus or minus flag of the value based on the bit
        if (flag) {
            return "-" + buffer.toString();
        } else {
            return buffer.toString();
        }
    }

    
    private static int[] string2IntArray(String number) {
        //Determines whether the input meets the requirements for floating point Numbers
        Pattern pattern = Pattern.compile("^(-?\d+|\d*)\.?\d*$");
        Matcher matcher = pattern.matcher(number);
        if (!matcher.find()) {
            throw new IllegalArgumentException(" The number entered is incorrect !");
        }

        int[] result = new int[number.length()];
        for (int i = 0; i < number.length(); i++) {
            result[i] = (int) (number.charAt(i) - '0');
        }
        return result;
    }
}

The operation results are as follows:

1. Misinput


Input two large integers:
1a*a22
Exception in thread "main" java.lang.IllegalArgumentException:  The number entered is incorrect !
at BigNumber.string2IntArray(BigNumber.java:132)
at BigNumber.bigNumberMultiply(BigNumber.java:54)
at BigNumber.main(BigNumber.java:22)


  2. Operation with negative Numbers, with zero before and after

Input two large integers:
-23424.2300*02345.23400000
-54935300.61982

  The result of the calculation in python is as follows

Python 2.6.5
>>> -23424.2300*02345.23400000
-54935300.619819999

  You can see that the python results are not true


Related articles: