// "Copyright Notice", please do not remove. // Written by Kevin Ng // The full tutorial on this subject can be found @ http://kevinhng86.iblog.website or http://programming.world.edu. // Release date to http://programming.world.edu will lag one week after release on http://kevinhng86.iblog.website // This source code file is a part of Kevin Ng's Z library. // This source code is licenses under CCDL-1.0 A copy of CDDL1.0 can be found at https://opensource.org/licenses/CDDL-1.0 // End "Copyright Notice" // // Notice: Version: InfiX-vr-2 (Infinity Multiplication) | Normal Usage | Bound by some limitation. // The first limitation of this version is how much memory the Java Programming Language allow the string to be store. // The second limitation is how much memory Java's environment can assign to the program. // If memory allow, for this version a string can only be less than 1,073,741,823 digits // There is a tester code ready to go to test against the bigDecimal // // Example of memory usage calculation: // 500 megabyte string of digits multiply to another 500 megabyte string of digits. // 1000 Mb of memory to store the strings outside of the function. // 1000 Mb of memory for when the strings are passing into the function. // 1000 Mb for the answer string. // 100 Mb (this can vary but most likely to be less than this) for script operation. // 3100 Mb of memory is requires for adding two strings of digits that costed 500MB per string. public class libZ { public static void main(String[] args) { String x = "1234567890123456789012345678901234567890"; String y = "1234567890123456789012354678901234567890"; String xy = infiX(x,y); System.out.println(xy); } public static String infiX(String a, String b) { boolean isaNeg = a.charAt(0) == '-'; boolean isbNeg = b.charAt(0) == '-'; a = a.replaceAll("^[-+]+", ""); b = b.replaceAll("^[-+]+", ""); a = a.replaceAll("^0+", ""); b = b.replaceAll("^0+", ""); int aDecPos = a.indexOf('.'); int bDecPos = b.indexOf('.'); int oDecPos = 0; if ( aDecPos > -1){ a = a.replaceAll("0+$", ""); aDecPos = ((a.length() - 1 - aDecPos) > 0)? (a.length() - 1 - aDecPos) : 0; a = a.replaceAll("[.]", ""); } if ( bDecPos > -1){ b = b.replaceAll("0+$", ""); bDecPos = ((b.length() - 1 - bDecPos) > 0)? (b.length() - 1 - bDecPos) : 0; b = b.replaceAll("[.]", ""); } if ( (a.length() < 1 || b.length() < 1 ) ){ return "0"; } if ( a.equals("1") && aDecPos < 1 ){ if ( bDecPos > 0 ){ b = b.substring(0, b.length() - bDecPos).concat(".").concat(b.substring(b.length() - bDecPos)); b = b.charAt(0) == '.'? "0".concat(b): b; return ((isaNeg != isbNeg)? "-".concat(b) : b); } return ((isaNeg != isbNeg)? "-".concat(b) : b); } if ( b.equals("1") && bDecPos < 1 ){ if ( aDecPos > 0 ){ a = a.substring(0, a.length() - aDecPos).concat(".").concat(a.substring(a.length() - aDecPos)); a = a.charAt(0) == '.'? "0".concat(a): a; return ((isaNeg != isbNeg)? "-".concat(a) : a); } return ((isaNeg != isbNeg)? "-".concat(a) : a); } if ( aDecPos > -1 || bDecPos > -1 ){ aDecPos = (aDecPos > -1)? aDecPos : 0; bDecPos = (bDecPos > -1)? bDecPos : 0; oDecPos = aDecPos + bDecPos; } byte digit = 9; long temp = 0; int alen = a.length(); int blen = b.length(); int bi = blen; int posr = 0; int posw = 0; String output = ""; String pad0 = ""; String tempstr = ""; String leftoverstr = ""; while ( pad0.length() < digit - 1 ){ pad0 = "0".concat(pad0); } while ( bi > 0 ){ int outlen = output.length(); int ai = alen; long z = Long.parseLong(b.substring((bi - digit > -1? bi - digit : 0), bi)); long leftover = 0; if ( outlen < 1 ){ while ( ai > 0){ temp = Long.parseLong(a.substring((ai - digit > -1? ai - digit: 0), ai)) * z + leftover; tempstr = String.valueOf(temp); leftover = tempstr.length() > digit? Long.parseLong(tempstr.substring(0,tempstr.length() - digit)) : 0 ; tempstr = pad0.concat(tempstr); output = tempstr.substring(tempstr.length() - digit).concat(output); ai = ai - digit; } if ( leftover > 0){ leftoverstr = pad0.concat(String.valueOf(leftover)); output = leftoverstr.substring(leftoverstr.length() - digit).concat(output); } } else { long tempadd = 0; long outposval = 0; int remainder = 0; int loopidx = 0; String tempaddstr = ""; String outposstr = ""; while ( ai > 0){ posw = (posr + loopidx) * digit; temp = Long.parseLong(a.substring((ai - digit > -1? ai - digit: 0), ai)) * z + leftover + remainder; tempstr = String.valueOf(temp); leftover = tempstr.length() > digit? Long.parseLong(tempstr.substring(0,tempstr.length() - digit)) : 0 ; tempstr = pad0.concat(tempstr); outlen = output.length(); outposstr = output.substring((outlen - posw - digit > 0 ? outlen - posw - digit : 0), (outlen - posw > 0 ? outlen - posw : 0)); outposval = outposstr.isEmpty()? 0 : Long.parseLong(outposstr); tempadd = Long.parseLong(tempstr.substring(tempstr.length() - digit )) + outposval; tempaddstr = String.valueOf(tempadd); remainder = tempaddstr.length() > digit? 1 : 0; tempaddstr = pad0.concat(String.valueOf(tempadd)); output = output.substring(0, (outlen - posw - digit > 0? outlen - posw - digit : 0)).concat(tempaddstr.substring(tempaddstr.length() - digit)).concat(output.substring((outlen - posw > 0? outlen - posw : 0), outlen)); loopidx = loopidx + 1; ai = ai - digit; } if ( leftover + remainder > 0){ leftoverstr = pad0.concat(String.valueOf(leftover + remainder)); output = leftoverstr.substring(leftoverstr.length() - digit).concat(output); } } posr = posr + 1; bi = bi - digit; } if ( oDecPos > 0 ){ while (output.length() < oDecPos){ output = "0".concat(output); } output = (output.length() < oDecPos)? "0".concat(output) : output; output = output.substring(0, output.length() - oDecPos).concat(".").concat(output.substring(output.length() - oDecPos)); output = output.replaceAll("^0+", ""); output = output.replaceAll("0+$", ""); output = output.replaceAll("[.]$", ""); output = output.charAt(0) == '.' ? "0".concat(output) : output; return (isaNeg != isbNeg? "-".concat(output): output); } output = output.replaceAll("^0+", ""); return (isaNeg != isbNeg? "-".concat(output): output); } }
Java Function – Infinity Multiplication – Version vr-2 – Normal Usage Production Version
Random Article You May Like
Apple Seeds Fifth Beta of iOS 12 to Developers
Apple today seeded the fifth beta of an upcoming iOS 12 update to developers for testing purposes, two weeks after […]
Dusty Mabe: Easy PXE boot testing with only HTTP using iPXE and libvirt
Introduction Occasionally I have a need to test out a PXE install workflow. All of this is super easy if […]
European Commission Made 'Fundamental Errors' in Irish Tax Ruling, Says Apple
Apple has claimed that the European Commission made “fundamental errors” when it ruled last year that the company owed Ireland […]
More Evidence Emerges to Suggest 2018 iPhones Won't Be Bundled With Lightning to Headphone Jack Adapter
It has been nearly two years since Apple released the iPhone 7 without a headphone jack, forcing users who still […]