How to deal with floating point number precision in JavaScript?

I have the following dummy test script:

function test(){
    var x = 0.1 * 0.2;
    document.write(x);
}
test();

This will print the result 0.020000000000000004 while it should just print 0.02 (if you use your calculator). As far as I understood this is due to errors in the floating point multiplication precision.

Does anyone have a good solution so that in such case I get the correct result 0.02 ? I know there are functions like toFixed or rounding would be another possibility, but I'd like to really have the whole number printed without any cutting and rounding. Just wanted to know if one of you has some nice, elegant solution.

Of course, otherwise I'll round to some 10 digits or so.


From the Floating-Point Guide:

What can I do to avoid this problem?

That depends on what kind of calculations you're doing.

  • If you really need your results to add up exactly, especially when you work with money: use a special decimal datatype.
  • If you just don't want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it.
  • If you have no decimal datatype available, an alternative is to work with integers, eg do money calculations entirely in cents. But this is more work and has some drawbacks.
  • Note that the first point only applies if you really need specific precise decimal behaviour. Most people don't need that, they're just irritated that their programs don't work correctly with numbers like 1/10 without realizing that they wouldn't even blink at the same error if it occurred with 1/3.

    If the first point really applies to you, use BigDecimal for JavaScript, which is not elegant at all, but actually solves the problem rather than providing an imperfect workaround.


    I like Pedro Ladaria's solution and use something similar.

    function strip(number) {
        return (parseFloat(number).toPrecision(12));
    }
    

    Unlike Pedros solution this will round up 0.999...repeating and is accurate to plus/minus one on the least significant digit.

    Note: When dealing with 32 or 64 bit floats, you should use toPrecision(7) and toPrecision(15) for best results. See this question for info as to why.


    For the mathematically inclined: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

    The recommended approach is to use correction factors (multiply by a suitable power of 10 so that the arithmetic happens between integers). For example, in the case of 0.1 * 0.2 , the correction factor is 10 , and you are performing the calculation:

    > var x = 0.1
    > var y = 0.2
    > var cf = 10
    > x * y
    0.020000000000000004
    > (x * cf) * (y * cf) / (cf * cf)
    0.02
    

    A (very quick) solution looks something like:

    var _cf = (function() {
      function _shift(x) {
        var parts = x.toString().split('.');
        return (parts.length < 2) ? 1 : Math.pow(10, parts[1].length);
      }
      return function() { 
        return Array.prototype.reduce.call(arguments, function (prev, next) { return prev === undefined || next === undefined ? undefined : Math.max(prev, _shift (next)); }, -Infinity);
      };
    })();
    
    Math.a = function () {
      var f = _cf.apply(null, arguments); if(f === undefined) return undefined;
      function cb(x, y, i, o) { return x + f * y; }
      return Array.prototype.reduce.call(arguments, cb, 0) / f;
    };
    
    Math.s = function (l,r) { var f = _cf(l,r); return (l * f - r * f) / f; };
    
    Math.m = function () {
      var f = _cf.apply(null, arguments);
      function cb(x, y, i, o) { return (x*f) * (y*f) / (f * f); }
      return Array.prototype.reduce.call(arguments, cb, 1);
    };
    
    Math.d = function (l,r) { var f = _cf(l,r); return (l * f) / (r * f); };
    

    In this case:

    > Math.m(0.1, 0.2)
    0.02
    

    I definitely recommend using a tested library like SinfulJS

    链接地址: http://www.djcxy.com/p/4714.html

    上一篇: 比较浮点值有多危险?

    下一篇: 如何处理JavaScript中的浮点数精度?