JS 0.1 + 0.2 != 0.3

Why does 0.1+0.2 not equal 0.3

Babbage.cs.qc.cuny.edu/IEEE-754.ol…

  • Floating point numbers in the bottom of the computer, the binary number can be partially discarded [because it is only 64 bits at most], so it is not the same as the original decimal system and there are precision problems
  • When the bottom computer adds 0.1+0.2 it adds binary numbers which are not precise enough and then it gets even less precise
  • The bottom of the computer returns the 64-bit binary to the browser and the browser recognizes the 17 digits after the decimal point and omits it

For example: 0.1+0.2 = 0.3000000000004

0.1+ 0.3 = 0.4000000000000 => abbreviated to 0.4

Conversion from decimal to binary

General processing

/* 1. Simple processing */
var str = 10;
console.log(str.toString(2));
Copy the code

Handwritten base conversion

// Base conversion
~function (){
    function transitionScale(scale){
        / / short division
        let num = Math.floor(this/scale),
            remainder = this % scale,
            remainderAry = [];
            remainderAry.unshift(remainder);
        while(num){
            remainder = num % scale;
            num = Math.floor(num/scale);
            remainderAry.unshift(remainder);
        }
        return Number(remainderAry.join(""))} ['transitionScale'].forEach(v= > {
        Number.prototype[v] = eval(v);
    })
}()

var num = 10;
console.log(num.transitionScale(2)); / / = > 1010
Copy the code
Exact floating-point calculation

The problem of imprecise floating-point numbers is common to all high-level languages

So floating-point operations in the company require floating-point accuracy

/* toFixed */
var num = 0.3004;
console.log(num.toFixed(2));
Copy the code

Handwritten floating-point calculation

// Handwritten floating-point calculation
~function(){
    // The decimal point length comparison returns the longest length
    function getMaxdecimals(args){
        let maxlength = -1
        args.forEach((v,i) = > {
            let itemlen  = String(v).split(".") [1].length
            maxlength < itemlen ? maxlength = itemlen : null;
        })
        return maxlength
    }

    function preciseFloat(type){
        // Remember the longest [0.5003,0.1] returns 4
        let maxlen = getMaxdecimals(this);
        let newAry = this.map((v) = > {
            return v*Math.pow(10,maxlen);
        })
        let sum = newAry.reduce((pre,next) = > {
            return eval(pre +type+ next)
        })
        let result;
        type === "*" ? result = sum/Math.pow(10.2 * maxlen):
        type === "/" ? result = sum:result = sum/Math.pow(10,maxlen)
        return result;
    }

    ['preciseFloat'].forEach((v,i) = > {
        Array.prototype[v] = eval(v);
    })
}()

let a = 109.122,
    b = 10.122;
console.log([a,b].preciseFloat("+"))
Copy the code