JS floating-point calculation bug

Source: Internet
Author: User
Tags pow

Before writing the project, directly to the data with the decimal point operation, found that the resulting value is not what you want.

After a series of studies, found in JavaScript, floating-point arithmetic is first converted to binary, in the turn into binary when there is an infinite loop decimal, so after the operation has a problem (this is based on the IEEE754 value of floating-point calculation of common problems).

So, I went through the JS Tool Library of the former company and found some functions for the operation.

Addition function Accadd (ARG1,ARG2) {var r1,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 (10,math.max (R1,R2)) return ((arg1*m+arg2*m)/m). ToFixed (2);   }//Division function Accdiv (arg1,arg2) {var t1=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 (10,T2-T1);   }}//multiplication function Accmul (arg1,arg2) {var m=0,s1=arg1.tostring (), s2=arg2.tostring (); Try{m+=s1.split (".") [1].length}catch (e) {} try{m+=s2.split (".") [1].length}catch (e) {} return Number (S1.replace (".", "")) *number (S2.replace (".", ""))/math.pow (10,m)}//subtraction function   ACCSUBTR (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} M=math.pow(10,math.max (R1,R2));   Dynamic control accuracy Length n= (R1>=R2)? R1:R2; Return ((arg1*m-arg2*m)/m). ToFixed (n);}

Find these tool functions when there is an idea to strengthen these functions. For example, when a function is not more than two.

So, in my mind there are two ideas, can achieve its effect.

1. Too many parameters, over and over again recursively (this method will not write, anyway, recursion).

2. Refactoring This tool function

Addition function Accadd () {let    arr = Array.prototype.slice.call (arguments);    Let num = 0;    Arr.map (res = {Let        r1, R2, M;        try {r1 = res.tostring (). Split (".") [1].length} catch (e) {r1 = 0};        try {r2 = num.tostring (). Split (".") [1].length} catch (e) {r2 = 0};        m = Math.pow (Ten, Math.max (R1, r2));        num = (RES * m + num * m)/M    });    return num;}

  

Subtraction function accsubtr () {let   arr = Array.prototype.slice.call (arguments);   Let num = arr[0];   Arr.shift ();   Arr.map (res = {Let        r1, R2, M, N;        try {r1 = res.tostring (). Split (".") [1].length} catch (e) {r1 = 0};        try {r2 = num.tostring (). Split (".") [1].length} catch (e) {r2 = 0};        m = Math.pow (Ten, Math.max (R1, r2));        n = (r1 >= r2)? R1:R2;        num = ((num * m-res * m)/m). ToFixed (n);    });     return num;  }

  

JS floating-point calculation bug

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.