<span style="font-size:18px">//Note: The addition of JavaScript will have an error, and it will be more obvious when the two floating-point numbers are added. This function returns a more accurate addition result. //Call: Accadd (ARG1,ARG2)//return value: Arg1 plus arg2 's exact resultfunction Accadd (arg1,arg2) {varr1,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 (Ten, Math.max (R1,R2))return(arg1*m+arg2*m)/m}</SPAN>
<span style="font-size:18px"> // Description: JavaScript subtraction results in error, It will be obvious when the two floating-point numbers are added together. This function returns a more accurate subtraction result. // Call: Accsub (ARG1,ARG2) // return value: Arg1 minus the exact result of Arg2 function Accsub (arg1,arg2) { return accadd (arg1,-arg2); }
<span style="font-size:18px">//Description: JavaScript multiplication results are error-evident when multiplying two floating-point numbers. This function returns a more accurate multiplication result. //Call: Accmul (ARG1,ARG2)//return value: arg1 times the exact result of Arg2function Accmul (arg1,arg2) {varm=0, S1=arg1.tostring (), s2=arg2.tostring (); Try{M+=s1.split (".")[1].length}Catch(e) {}Try{M+=s2.split (".")[1].length}Catch(e) {}returnNumber (S1.replace (".","")) *number (S2.replace (".",""))/math.pow (Ten, M)}</SPAN>
<span style="font-size:18px">//Description: The result of the division of JavaScript will be error, which will be obvious when dividing two floating-point numbers. This function returns a more accurate division result. //Call: Accdiv (ARG1,ARG2)//return value: Arg1 divided by the exact result of Arg2function Accdiv (arg1,arg2) {vart1=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 (Ten, t2-t1); } }</SPAN>
JS floating point accurate calculation (add, subtract, multiply, divide)