Home > Web Front-end > JS Tutorial > Summary of js calculation accuracy issues_javascript skills

Summary of js calculation accuracy issues_javascript skills

WBOY
Release: 2016-05-16 17:35:40
Original
1022 people have browsed it

Copy code The code is as follows:

//问题比如:7*0.8 JavaScript算出来就是:5.6000000000000005

            //加法函数,用来得到精确的加法结果
            //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
            //调用:accAdd(arg1,arg2)
            //返回值:arg1加上arg2的精确结果
            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
            }
            //用法:
            //给Number类型增加一个add方法,调用起来更加方便。
            Number.prototype.add = function (arg) {
                return accAdd(arg, this);
            }
            //如:
            var t1 = 6.60;
            var t2 = 1.32;
            var t3 = 1.2;
            var t4 = 1.2;
            var t5 = 1.2;
            alert(Number(t1).add(Number(t2)).add(Number(t3)).add(Number(t4)).add(Number(t5)));

            //减法函数,用来得到精确的减法结果
            function Subtr(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));
                //last modify by deeka
                //动态控制精度长度
                n = (r1 >= r2) ? r1 : r2;
                return ((arg1 * m - arg2 * m) / m).toFixed(n);
            }

// 곱셈 함수는 정확한 곱셈 결과를 얻기 위해 사용됩니다.
// 설명: JavaScript의 곱셈 결과에는 오류가 있으며 두 개의 부동 소수점 숫자를 곱하면 분명해집니다. 이 함수는 더 정확한 곱셈 결과를 반환합니다.
                                                                                                                                    0, s1 = arg1 .toString(), s2 = arg2.toString();
                    try { m = s1.split(".")[1].length } catch (e) {          (". ")[1].length } catch (e) { }
                                                               반환 번호(s1.replace(".", "")) * Number(s2.replace(" .", "")) / 수학. pow(10, m)
                                                                                                                                                               
Number.prototype.mul = 함수(arg) {
return accMul(arg, this);
}

// 정확한 제거 결과를 얻기 위해 제거 함수를 사용합니다.
// 설명: JavaScript의 나누기 결과에는 오류가 있으며 두 개의 부동 소수점 숫자를 제거하면 분명해집니다. 이 함수는 더 정확한 나누기 결과를 반환합니다.
                                                                                                                                  1 = 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) {

>              r2 = Number(arg2.toString().replace (".", ""))
                                                                                             >



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template