Accurate floating point number calculation in JavaScript

Summary

JavaScript's use of IEEE754 for floating-point numbers can introduce accuracy issues during arithmetic operations. To mitigate this, a custom `calc` function is introduced, designed to perform accurate calculations. This function addresses precision problems by first converting floating-point numbers into integers, based on a specified length parameter, before executing the arithmetic. After the operation, the result is scaled back to a floating-point number, effectively handling addition, subtraction, multiplication, and division with improved precision. This approach offers a practical workaround for common floating-point inaccuracies in JavaScript.

In JavaScript, since floating point is defined with IEEE754,there will be some accuracy issues while doing floating point number arithmetic. Here is a function which can resolve the accuracy issue.

var calc = function(num1,operator,num2,len){
    var numFixs = function(num){
        var arr = num.toFixed(len).toString().split('.');
        return parseInt(arr.join(''));
    }
    switch(operator){
        case '+':
            return ( numFixs(num1) + numFixs(num2) )/ Math.pow(10,len);
        break;
        case '-':
            return ( numFixs(num1) - numFixs(num2) )/ Math.pow(10,len);
        break;
        case '*':
            var tmp = ( numFixs(num1) * numFixs(num2) )/ Math.pow(10,len) / Math.pow(10,len);
            return parseFloat(tmp.toFixed(len));
        break;
        case '/':
        if (num2 == 0) {return 'Error'}
            return ( numFixs(num1) / numFixs(num2) )/ Math.pow(10,len);
        break;
    }
}

//Examples
calc(2.01,'-',2.11 ,2);
calc(2.1,'-',2.111111 ,2);
calc(1,'-',2.111111 ,2);
calc(5,'-',2.111111 ,3);

If you have better idea. Please feel free to share with us.

Author : 苏洋 Source : http://soulteary.com/2012/12/26/javascript-%E6%B5%AE%E7%82%B9%E6%95%B0%E7%9A%84%E7%B2%BE%E7%A1%AE%E8%AE%A1%E7%AE%97.html

JAVASCRIPT FLOATING POINT IEEE 754 ACCURACY

  RELATED

  COMMENTS

0

No comment for this article.