2017-03-20

JavaScript 浮點數計算

一直一來都很少處理 JS 精準運算的問題,大部分的時候 UI 有點不準也不會造成太大的問題,這次用 JS 做一些加減的數字計算,遇到小數的時候就出現運算誤差,既然是 JS 計算機不精準是不行的,User 不可能接受的,找了一堆文章後沒一個靠譜的,但重點都是轉換到整數進行運算,再做浮點位移,整個計算最好盡量避免小數的處理,另外的問題就是位數不能太多。


function transformInt(num1, num2, padZeno, compute) {
    num1 = '' + num1;
    num2 = '' + num2;

    var p1 = 0, p2 = 0;
    try { p1 = num1.split(".")[1].length } catch (e) { }
    try { p2 = num2.split(".")[1].length } catch (e) { }

    if(padZeno){
        while (p1 < p2) { p1++; num1 += '0'; }
        while (p2 < p1) { p2++; num2 += '0'; }
    }

    var int1 = parseInt(num1.replace(".", ""), 10);
    var int2 = parseInt(num2.replace(".", ""), 10);
    return compute(int1, int2, p1, p2);
}


/*浮點數相加*/
function floatAdd(num1, num2) {
    return transformInt(num1, num2, true, function(int1, int2, p1, p2){
        return (int1 + int2) / Math.pow(10, p1);
    });
}

/*浮點數相減*/
function floatSub(num1, num2) {
    return transformInt(num1, num2, true, function(int1, int2, p1, p2){
        return (int1 - int2) / Math.pow(10, p1);
    });
}

/*浮點數相乘*/
function floatMul(num1, num2) {
    return transformInt(num1, num2, false, function(int1, int2, p1, p2){
        return (int1 * int2) / Math.pow(10, p1 + p2);
    });
}

/*浮點數相除*/
function floatDiv(num1, num2) {
    return transformInt(num1, num2, false, function(int1, int2, p1, p2){
        return (int1 / int2) / Math.pow(10, p1 - p2);
    });
}



function unitTest(method, list){
    console.log('=[ ' + method.name + ' ]==============================================');

    for (var i = 0; i < list.length; i++){
        var row = list[i];
        var result = method(row.num1, row.num2);

        if(result == row.ans){
            console.log(row, result);
        }else{
            console.error(row, result);
        }
    };
}

unitTest(floatAdd, [
    {num1:0.11, num2:0.2, ans: 0.31},
    {num1:0.21, num2:0.2, ans: 0.41},
    {num1:0.31, num2:0.2, ans: 0.51},
    {num1:0.41, num2:0.2, ans: 0.61},
    {num1:0.51, num2:0.2, ans: 0.71},
    {num1:0.61, num2:0.2, ans: 0.81},
    {num1:0.71, num2:0.2, ans: 0.91},
    {num1:0.81, num2:0.2, ans: 1.01},
    {num1:0.91, num2:0.2, ans: 1.11},
    {num1:1.01, num2:0.2, ans: 1.21},
]);

unitTest(floatSub, [
    {num1:1.02, num2:0.2, ans: 0.82},
    {num1:1.12, num2:0.2, ans: 0.92},
    {num1:1.22, num2:0.2, ans: 1.02},
    {num1:1.32, num2:0.2, ans: 1.12},
    {num1:1.42, num2:0.2, ans: 1.22},
    {num1:1.52, num2:0.2, ans: 1.32},
    {num1:1.62, num2:0.2, ans: 1.42},
    {num1:1.72, num2:0.2, ans: 1.52},
    {num1:1.82, num2:0.2, ans: 1.62},
    {num1:1.92, num2:0.2, ans: 1.72},
]);

unitTest(floatMul, [
    {num1:10, num2:0.14, ans: 1.4},
    {num1:10, num2:0.24, ans: 2.4},
    {num1:10, num2:0.34, ans: 3.4},
    {num1:10, num2:0.44, ans: 4.4},
    {num1:10, num2:0.54, ans: 5.4},
    {num1:10, num2:0.64, ans: 6.4},
    {num1:10, num2:0.74, ans: 7.4},
    {num1:10, num2:0.84, ans: 8.4},
    {num1:10, num2:0.94, ans: 9.4},
    {num1:10, num2:1.04, ans: 10.4},
]);

unitTest(floatDiv, [
    {num1:1.1, num2:0.1, ans: 11},
    {num1:1.1, num2:0.2, ans: 5.5},
    {num1:0.99, num2:0.3, ans: 3.3},
    {num1:1.1, num2:0.4, ans: 2.75},
    {num1:1.1, num2:0.5, ans: 2.2},
    {num1:1.32, num2:0.6, ans: 2.2},
    {num1:1.54, num2:0.7, ans: 2.2},
    {num1:1.76, num2:0.8, ans: 2.2},
    {num1:2.97, num2:0.9, ans: 3.3},
    {num1:1.1, num2:0.1, ans: 11},
]);

0 回應: