Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Why is the result of eval(" 1*0.2+2*0.2+0*0.6 ") in js 0.60000000000 1?
Why is the result of eval(" 1*0.2+2*0.2+0*0.6 ") in js 0.60000000000 1?
You get 0.6 under IE8 and 0.6000000000 1 under chrom and Firefox.

In fact, this is a bug of js floating-point operation. In JavsScript, variables are stored through float instead of number. Javascript uses the 64-bit floating-point format defined by the IEEE 754-2008 standard to store numbers. According to the definition of IEEE 754:?

The plastic part length corresponding to decimal64 is 10, and the decimal part length is 16, so the default calculation result is "7.0000000000 1". If the last decimal place is 0, 1 is regarded as a valid number symbol.

Solution: Note: The following code is not original.

( 1).mul(0.2)+(2)。 mul(0.2)+0*0.6

& lt script & gt

//Addition function, used to get accurate addition result.

//Description: There will be errors in the addition result of javascript, which will be more obvious when adding two floating-point numbers. This function returns a more accurate addition result.

//Call: accAdd(arg 1, arg2)

//Return value: arg 1 plus the exact result of arg2.

Function? accAdd(arg 1,arg2){

var? r 1,r2,m;

Please try {r1= arg1.tostring (). Split (".") [1]. length} catch (e) {r1= 0}

Please try {r2=arg2.toString (). Split (".") [1]. Length} Capture (e){r2=0}

m=Math.pow( 10,Math.max(r 1,r2))

Return? (arg 1*m+arg2*m)/m

}

//add an add method to the numeric type, which is more convenient to call, similar to the following.

Number.prototype.add? =? Function? (Parameter) {

Return? accAdd(arg,this);

}

Number.prototype.mul? =? Function? (Parameter) {

Return? accMul(arg,this);

}

//Multiplication function, used to get accurate multiplication results.

//Description: There will be errors in the multiplication result of javascript, which will be more obvious when two floating-point numbers are multiplied. This function returns a more accurate multiplication result.

//Call: accMul(arg 1, arg2)

//Return value: arg 1 times arg2.

Function? accMul(arg 1,arg2)

{

var? m=0,s 1=arg 1.toString(),S2 = arg 2 . tostring();

Please try {m+= s1.split (".") [1]. length} catch (e) {}

Try {m+= s2.split (".") [1]. length} catch (e) {}

Return? Number(s 1.replace (".,"")) * number (s2.replace (".","")) /Math.pow( 10, m).

}

Number.prototype.div? =? Function? (Parameter) {

Return? accDiv(arg,this);

}

//Division function, used to get accurate division results.

//Description: There will be errors in the division result of javascript, which will be more obvious when two floating-point numbers are divided. This function returns a more accurate division result.

//Call: accDiv(arg 1, arg2)

//Return value: arg 1 The exact result of dividing by arg2.

Function? accDiv(arg 1,arg2){

var? t 1=0,t2=0,r 1,R2;

Please try {t1= arg1.tostring (). Split (".") [1].length}catch(e){}

Please try {t2=arg2.toString (). Split (".") [1].length}catch(e){}

Use (mathematics) {

r 1 = Number(arg 1 . tostring()。 Replace (".",""))

r2=Number(arg2.toString()。 Replace (".",""))

Return? (r 1/r2)*pow( 10,T2-t 1);

}

}

& lt/script & gt;