The number operation of javascript

Keywords: github Javascript

1, Precision of floating point numbers

  1. Problem: the data type in JS is Number. This type uses IEEE754 format to represent integer and floating-point numbers. There are often some problems when using 64 bit double precision floating-point numbers in IEEE 754 standard.
  2. Composition: 1 sign digit, s for positive, 1 for negative, 11 for index part e, 52 for decimal part (i.e. significant number) f.
  3. Reason: when floating-point numbers are expressed in binary, they are infinite. Binary numbers truncated due to the limitation of floating-point decimal places are converted to decimal, for example, 0.1 + 0.2 becomes 0.3000000000000004.
//Addition of floating point number
function accAdd(arg1,arg2){
    var r1,r2,m,c;
    try{
        r1 = arg1.toString().split(".")[1].length;
    }catch(e){
        r1 = 0;
    }
    try{
        r2 = arg2.toString().split(".")[1].length;
    }catch(e){
        r2 = 0
    }

    c = Math.abs(r1-r2);
    m = Math.pow(10,Math.max(r1,r2));

    if(c>0){
        var cm = Math.pow(10,c);
        if(r1>r2){
            arg1 = Number(arg1.toString().replace(".",""));
            arg2 = Number(arg2.toString().replace(".",""))*cm;
        }else{
            arg1 = Number(arg1.toString().replace(".",""))*cm;
            arg2 = Number(arg2.toString().replace(".",""));
        }
    }else{
        arg1 = Number(arg1.toString().replace(".",""));
        arg2 = Number(arg2.toString().replace(".",""));
    }

    return (arg1+arg2)/m;
}

//Subtraction of floating point number
function accSub(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;
    }
    // Are you not afraid to go beyond the range of safe integers
    // m = Math.pow(10,Math.max(r1,r2));
    // n = (r1>= r2) ? r1 :r2;
    // return ((arg1*m-arg2*m) / m ).toFixed(n);

    c = Math.abs(r1-r2);
    m = Math.pow(10,Math.max(r1,r2));

    if(c>0){
        var cm = Math.pow(10,c);
        if(r1>r2){
            arg1 = Number(arg1.toString().replace(".",""));
            arg2 = Number(arg2.toString().replace(".",""))*cm;
        }else{
            arg1 = Number(arg1.toString().replace(".",""))*cm;
            arg2 = Number(arg2.toString().replace(".",""));
        }
    }else{
        arg1 = Number(arg1.toString().replace(".",""));
        arg2 = Number(arg2.toString().replace(".",""));
    }

    return (arg1-arg2)/m;
}


2, The problem of integer precision

  1. In JavaScript, the Number type is uniformly treated as floating-point Number, and the integer is calculated as the maximum 54 bits (Number.min ﹤ safe ﹤ integer ﹤ Number.max ﹤ safe ﹤ integer). If it exceeds this range, there will be the problem of rounding off the Number of digits and inaccurate precision calculation.
  2. Arithmetic of large numbers

3, Other mature Libraries

When the precision requirement is high, it is usually processed in the background. If the front end needs to be processed in the corresponding class library.
1. Binary floating point arithmetic standard https://zh.wikipedia.org/wiki/IEEE_754
2. mathjs http://mathjs.org/
3. decimal http://mikemcl.github.io/decimal.js/
4. big http://mikemcl.github.io/big.js

Posted by MarcAndreTalbot on Tue, 07 Apr 2020 08:30:48 -0700