floating point numbers don't add right

Discussion in 'Javascript' started by janice, Sep 23, 2004.

  1. janice

    janice Guest

    Hi all,

    I read a lot of info on this issue, but still don't get it. Why does
    this happen?

    document.write(".05" + " \+ " + ".05" + " \= " + (.05 + .05) +
    "<br>");
    document.write("1.05" + " \+ " + ".05" + " \= " + (1.05 + .05) +
    "<br>");
    document.write("2.05" + " \+ " + ".05" + " \= " + (2.05 + .05) +
    "<br>");

    Outputs:

    ..05 + .05 = 0.1
    1.05 + .05 = 1.1
    2.05 + .05 = 2.0999999999999996


    I also tried the code provided in this group's FAQ, and it worked
    except for when it reaches an "even" number which should display 6.00
    for example, it displays 5.100.

    thx!

    J.
     
    janice, Sep 23, 2004
    #1
    1. Advertising

  2. janice

    Philip Ronan Guest

    On 23/9/04 9:14 pm, janice wrote:

    > Hi all,
    >
    > I read a lot of info on this issue, but still don't get it. Why does
    > this happen?
    >

    (snip)
    > 2.05 + .05 = 2.0999999999999996
    >


    In binary, these numbers go on for ever:

    2.05 = 10.0000110011001100110011001100...
    0.05 = 0.0000110011001100110011001100...
    2.10 = 10.0001100110011001100110011001...

    With a fixed number of bits it is impossible to represent these numbers
    exactly, so they contain errors right from the start. Addition makes the
    errors even larger. For example, if I added together the first two binary
    numbers shown above, the last digit of the result would be 0, not 1.

    It's just like adding 1/3 to 2/3 in base 10:

    2/3 = 0.666666666666666
    + 1/3 = 0.333333333333333
    = 0.999999999999999 (not 1.000000)

    It is just as impossible to write 1/3 in decimal as it is to write 0.05 in
    binary.

    I hope that makes sense.

    --
    Philip Ronan

    (Please remove the "z"s if replying by email)
     
    Philip Ronan, Sep 23, 2004
    #2
    1. Advertising

  3. > I read a lot of info on this issue, but still don't get it. Why does
    > this happen?


    > .05 + .05 = 0.1
    > 1.05 + .05 = 1.1
    > 2.05 + .05 = 2.0999999999999996


    That's really annoying, isn't it? JavaScript uses binary floating point
    notation internally to represent numbers. Floating point is very useful
    for some scientific applications, but for other applications it has some
    severe shortcomings.

    In case, you are seeing that it cannot exactly represent decimal
    fractions. That would be ok on a planet where people do not commonly use
    decimal fractions. It might seem that floating point was a poor choice.

    http://www.crockford.com/#javascript
     
    Douglas Crockford, Sep 23, 2004
    #3
  4. janice

    Lee Guest

    Douglas Crockford said:
    >
    >> I read a lot of info on this issue, but still don't get it. Why does
    >> this happen?

    >
    >> .05 + .05 = 0.1
    >> 1.05 + .05 = 1.1
    >> 2.05 + .05 = 2.0999999999999996

    >
    >That's really annoying, isn't it? JavaScript uses binary floating point
    >notation internally to represent numbers. Floating point is very useful
    >for some scientific applications, but for other applications it has some
    >severe shortcomings.
    >
    >In case, you are seeing that it cannot exactly represent decimal
    >fractions. That would be ok on a planet where people do not commonly use
    >decimal fractions. It might seem that floating point was a poor choice.



    The advantages of using standard IEEE floating point representation
    far outweigh any disadvantage. The infinitesimal error encountered
    in typical web-page arithmetic is easily corrected by rounding.

    When representing numbers in binary, each bit represents either
    0 or 1 times 2 raised to some positive or negative integer power,
    so a binary representation of a value between 0 and 1 is the sum
    of the series (b1)/2 + (b2)/4 + (b3)/16 + (b4)/32 + ...
    where each (bn) value is either 0 or 1. It's mathematically
    impossible to represent some decimal fractions as a finite
    series of such terms.
     
    Lee, Sep 24, 2004
    #4
  5. Lee wrote:
    > Douglas Crockford said:
    >
    >>>I read a lot of info on this issue, but still don't get it. Why does
    >>>this happen?

    >>
    >>>.05 + .05 = 0.1
    >>>1.05 + .05 = 1.1
    >>>2.05 + .05 = 2.0999999999999996

    >>
    >>That's really annoying, isn't it? JavaScript uses binary floating point
    >>notation internally to represent numbers. Floating point is very useful
    >>for some scientific applications, but for other applications it has some
    >>severe shortcomings.
    >>
    >>In case, you are seeing that it cannot exactly represent decimal
    >>fractions. That would be ok on a planet where people do not commonly use
    >>decimal fractions. It might seem that floating point was a poor choice.

    >
    > The advantages of using standard IEEE floating point representation
    > far outweigh any disadvantage. The infinitesimal error encountered
    > in typical web-page arithmetic is easily corrected by rounding.
    >
    > When representing numbers in binary, each bit represents either
    > 0 or 1 times 2 raised to some positive or negative integer power,
    > so a binary representation of a value between 0 and 1 is the sum
    > of the series (b1)/2 + (b2)/4 + (b3)/16 + (b4)/32 + ...
    > where each (bn) value is either 0 or 1. It's mathematically
    > impossible to represent some decimal fractions as a finite
    > series of such terms.


    That is precisely why floating point is poorly suited to most
    applications. For applications that require decimal precision (such as
    those involving money), there are other representations that are much
    better. Scaled integers and BCD, for example.

    2.0999999999999996 shows 3.0 to 16 decimal places, nearly all of them
    wrong. This result, to most people, is surprising.
     
    Douglas Crockford, Sep 24, 2004
    #5
  6. janice

    Lee Guest

    Douglas Crockford said:

    >That is precisely why floating point is poorly suited to most
    >applications. For applications that require decimal precision (such as
    >those involving money), there are other representations that are much
    >better. Scaled integers and BCD, for example.
    >
    >2.0999999999999996 shows 3.0 to 16 decimal places, nearly all of them
    >wrong. This result, to most people, is surprising.



    When handling money, all you need to do is round to thousandths
    and display to hundredths. It's pretty trivial.

    The fact that it's surprising to neophytes isn't much of
    a reason to change it.
     
    Lee, Sep 24, 2004
    #6
  7. JRS: In article <>,
    dated Thu, 23 Sep 2004 13:14:59, seen in news:comp.lang.javascript,
    janice <> posted :
    >
    >I also tried the code provided in this group's FAQ,


    The FAQ says : Operations on integers are exact if the true result and
    all intermediates are integers within that range.

    If you want results exact to the cent, then work in cents, not in euros.

    Integers are exact up to as little over 9,000,000,000,000,000; enough
    for most budgets.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Sep 24, 2004
    #7
  8. Lee <> writes:

    > When handling money, all you need to do is round to thousandths
    > and display to hundredths. It's pretty trivial.


    No code is so trivial that it can't contain an error (except perhaps
    the empty statement :).

    > The fact that it's surprising to neophytes isn't much of
    > a reason to change it.


    It's perhaps too late to change it, although I don't think a change to
    BCD-numbers like C#'s "decimal" type would be noticed by most
    applications.

    However, as a purely theoretical observation, I'd say that for a
    script language that is used by so many (until recently)
    non-programmers, it would probably be better to use a decimal number
    format to avoid exactly these questions. It happens often enough
    to be in the FAQ.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 24, 2004
    #8
  9. JRS: In article <>, dated Fri, 24 Sep 2004
    19:20:58, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
    <> posted :
    >
    >However, as a purely theoretical observation, I'd say that for a
    >script language that is used by so many (until recently)
    >non-programmers, it would probably be better to use a decimal number
    >format to avoid exactly these questions. It happens often enough
    >to be in the FAQ.


    The same applies in Delphi groups of news:borland.*, except that they
    have no newsgroup FAQs.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    For news:borland.*, use their server newsgroups.borland.com ; but first read
    Guidelines <URL:http://www.borland.com/newsgroups/guide.html> ff. with care.
     
    Dr John Stockton, Sep 25, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H aka N
    Replies:
    15
    Views:
    15,668
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,493
  3. Replies:
    4
    Views:
    1,291
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,344
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    258
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page