FAQ Topic - Why does simple decimal arithmetic give strange results?

Discussion in 'Javascript' started by FAQ server, Aug 16, 2007.

  1. FAQ server

    FAQ server Guest

    -----------------------------------------------------------------------
    FAQ Topic - Why does simple decimal arithmetic give strange
    results?
    -----------------------------------------------------------------------

    For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does
    not give exactly 0.07 in javascript.

    Javascript numbers are represented in binary as IEEE-754 (IEC 559)
    Doubles, with a resolution of 53 bits, giving an accuracy of
    15-16 decimal digits; integers up to about 9e15 are precise, but
    few decimal fractions are. Given this, arithmetic is as exact
    as possible, but no more. Operations on integers are exact if
    the true result and all intermediates are integers within that
    range.

    In particular, non-integer results should not normally be
    compared for equality; and non-integer computed results
    commonly need rounding; see 4.6.

    http://msdn.microsoft.com/library/d...html/c7a6bd3a-4b1c-4dbe-8505-106dbf483b41.asp


    ===
    Postings such as this are automatically sent once a day. Their
    goal is to answer repeated questions, and to offer the content to
    the community for continuous evaluation/improvement. The complete
    comp.lang.javascript FAQ is at http://jibbering.com/faq/index.html.
    The FAQ workers are a group of volunteers.
    FAQ server, Aug 16, 2007
    #1
    1. Advertising

  2. In comp.lang.javascript message <46c38575$0$90264$.
    dk>, Wed, 15 Aug 2007 23:00:01, FAQ server <>
    posted:
    >
    >For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does
    >not give exactly 0.07 in javascript.
    >


    True; but "exactly 0.07" is ambiguous. How best can we state, briefly
    BOTH that (0.06+0.01).toString() -> "0.06999999999999999"
    AND that (0.06+0.01) - 0.07 != 0.0 // ??

    The recent short list of FAQ changes has an error.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
    Dr J R Stockton, Aug 16, 2007
    #2
    1. Advertising

  3. FAQ server

    John Hosking Guest

    Dr J R Stockton wrote:
    >
    > The recent short list of FAQ changes has an error.


    Well, you're about as helpful as a government employee.

    --
    John Hosking
    John Hosking, Aug 17, 2007
    #3
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,747
    Smokey Grindel
    Dec 2, 2006
  2. dizzy
    Replies:
    1
    Views:
    271
    James Kanze
    Sep 10, 2008
  3. FAQ server
    Replies:
    24
    Views:
    286
    Randy Webb
    Sep 20, 2006
  4. FAQ server
    Replies:
    6
    Views:
    154
    Dr J R Stockton
    Nov 3, 2006
  5. FAQ server
    Replies:
    0
    Views:
    88
    FAQ server
    Oct 22, 2006
Loading...

Share This Page