Knowledge about
Javascript numbers are stored using the Number type. The Number type has 64-bit floating-point numbers, so it’s bound to be overrun.
On the verge of death
But we can still test the waters
Parse var c='{"num": parse var c='{"num": 9007199254740992}' JSON.parse(c) // {num: 90071992547410000}Copy the code
High precision calculation
High Accuracy Algorithm is a mathematical calculation method that deals with large numbers. In ordinary scientific calculations, calculations are often made to hundreds of decimal places or more, but of course they may be in the hundreds of billions. In general, such numbers are collectively referred to as high-precision numbers. High-precision algorithms are used to simulate the addition, subtraction, multiplication, division, multiplication, factorial, square root and other operations of large data by computers. For very large numbers that cannot be stored normally in the computer, the number is then broken down into bits and bits, or four bits and stored in an array to represent a number in an array, which is called a high precision number. The high-precision algorithm is an algorithm that can deal with various operations of high precision, but because of its particularity, it is separated from the algorithm of ordinary numbers.
// The division function is used to get the exact result of the division. This function returns a more accurate division result. Function accDiv(arg1,arg2){var t1=0,t2=0,r1,r2; try{t1=arg1.toString().split(".")[1].length}catch(e){} try{t2=arg2.toString().split(".")[1].length}catch(e){} with(Math){ r1=Number(arg1.toString().replace(".","")) r2=Number(arg2.toString().replace(".","")) return (r1/r2)*pow(10,t2-t1); }} // Add a div method to the Number type to make it easier to call. Number.prototype.div = function (arg){ return accDiv(this, arg); } // the multiplication function is used to get the exact multiplication result // Note: javascript multiplication results have errors, when two floating point numbers are multiplied will be obvious. This function returns a more accurate multiplication result. AccMul (arg1,arg2) Function accMul(arg1,arg2) {var m=0,s1= arg1.tostring (),s2= arg2.tostring (); try{m+=s1.split(".")[1].length}catch(e){} try{m+=s2.split(".")[1].length}catch(e){} return Number (s1) replace (". ", "")) * Number (s2) replace (".", ""))/math.h pow (10 m)} / / to add a Number type the mul method, call up more convenient. Number.prototype.mul = function (arg){ return accMul(arg, this); } // add function, used to get exact add result // note: javascript add result will have error, when two floating point numbers add will be more obvious. This function returns a more accurate addition result. AccAdd (arg1,arg2){function accAdd(arg1,arg2){var r1,r2,m; try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0} try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0} M = math.pow (10, math.max (r1,r2)) return (arg1*m+arg2*m)/m} Number.prototype.add = function (arg){ return accAdd(arg,this); } // Include these functions where you want to use them, and then call them to calculate. // If you want to calculate: 7*0.8, change it to (7).mul(8) // If you want to calculate: 7*0.8, change it to (7).mul(8) Function Subtr(arg1,arg2){var r1,r2,m,n; try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0} try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0} m=Math.pow(10,Math.max(r1,r2)); //last modify by deeka n=(r1>=r2)? r1:r2; return ((arg1*m-arg2*m)/m).toFixed(n); }Copy the code
Common JS library
- BigInt
- bigDecimal
- json-bigint
var JSONbig = require('json-bigint');
var json = '{ "value" : 9223372036854775807, "v2": 123 }';
console.log('Input:', json);
console.log('');
Copy the code
The interview guide
Refining + system + self-consistency
- Basic concept: use high-precision processing method for exceeding parts.
review
- The ability to articulate high-precision computing concepts is key.