When js do four operations, the loss of accuracy problems and solutions

There have been such a bug, is obviously two numbers are the same but it is inexplicable reported on two numbers are not equal before the project, resulting in the process can not continue

Then I put all the data lead out, one to one comparison, only a very time-consuming process, eventually I found

Because there will be loss of accuracy problems after js calculation to find the problem like that, and then began to solve the puzzles me a long time problem

Ado directly on the code

/ * * 
 * Loss of precision numbers together to solve two problems 
 * @param A 
 * @param B 
 * Number The @Returns {} 
 * / 
function floatAdd (A, B) {
     var C, D, E;
     IF (A == undefined || null == A || "" == A || isNaN (A)) A = {0 ;}
     IF (B || undefined == null == B || "" == B || isNaN (B )) {B = 0 ;}
     the try { 
        C = a.toString () Split () [. 1. "." ] .length; 
    } the catch (F) { 
        C = 0 ; 
    } 
    the try { 
        D = b.toString () .split ( ".") [. 1 ] .length;
    }the catch (F) { 
        D = 0 ; 
    } 
    E = Math.pow (10 , Math.max (C, D));
     return   (floatMul (A, E) + floatMul (B, E)) / E; 
} 
/ * * 
 * subtract two numbers to solve problems loss of accuracy 
 * @param A 
 * @param B 
 * number The @Returns {} 
 * / 
function floatSub (A, B) {
     var C, D, E;
     IF (A == undefined || null == A || "" == A || isNaN (A)) A = {0 ;}
     IF (B || undefined == null == B || "" == B || isNaN (B)) B 0 = { ;}
     the try { 
        C. = A.toString () Split ( ".") [. 1 ] .length; 
    } the catch (F) { 
        C = 0 ; 
    } 
    the try { 
        D . = B.toString () Split [. 1 ( ".") ]. length; 
    } the catch (F) { 
        D = 0 ; 
    } 
    E = Math.pow (10 , Math.max (C, D));
     return (floatMul (A, E) - floatMul (B, E)) / E; 
} 
/ * * 
 * loss of precision multiplying two numbers to solve problems 
 * @param A 
 * @param B 
 * number The @Returns {} 
 * / 
function floatMul (A, B) {
     var= 0 C , 
        D = a.toString (), 
        E = b.toString ();
     the try { 
        C + = d.split ( ".") [. 1 ] .length; 
    } the catch (F)} {
     the try { 
        C + e.split = ( ".") [. 1 ] .length; 
    } the catch (F) {}
     return . Number The ( "." d.replace (, "")) * Number The (e.replace ( "", "" )) / Math.pow (10 , C); 
} 
/ * * 
 * loss of precision divide two numbers to solve problems 
 * @param A 
 * @param B 
 * @Returns 
 * / 
function floatDiv (A,
    b) {
    var c, d, e = 0,
        f = 0;
    try {
        e = a.toString().split(".")[1].length;
    } catch (g) {}
    try {
        f = b.toString().split(".")[1].length;
    } catch (g) {}
    return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), floatMul(c / d, Math.pow(10, f - e));
}

Js accuracy to solve more missing

Multiplied also very simple principle is a good use of arithmetic we learned in elementary school fractional / addition / subtraction / variation of decimal places to the accuracy of the conversion division

 

Guess you like

Origin www.cnblogs.com/smile-tianxia/p/11714999.html