parseFloat function seems to have a bug when summing? help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ajordan

    parseFloat function seems to have a bug when summing? help!

    hello!
    i think i have a bug when i use parseFoat function:

    i have the following operation :

    parseFloat("10. 664062") + parseFloat("10. 664062")
    +parseFloat("10 .664062");

    the result of following statement must be 31.992186. Does somebody
    explain to me why the result returned is 31.992185999999 997.

    strange no?

    thanks a lot for your help.
  • Tim Fooy

    #2
    Re: parseFloat function seems to have a bug when summing? help!

    No, it is not a bug. The reason for this result is the fact that the
    internal representation of a float (which follows IEEE 754 standard) is
    limited to 7 correct digits, because of the limitation to 32 bits. See e.g.
    http://research.microsoft.com/~holla...ieeefloat.html (that
    looks correct to me, i haven't read it all)

    If you want a higher precision, you'll have to use a double precision
    floating point variable, but i don't know whether this exists in javascript.

    Tim


    Comment

    • DrewM

      #3
      Re: parseFloat function seems to have a bug when summing? help!

      Tim Fooy wrote:
      [color=blue]
      > No, it is not a bug. The reason for this result is the fact that the
      > internal representation of a float (which follows IEEE 754 standard) is
      > limited to 7 correct digits, because of the limitation to 32 bits. See e.g.
      > http://research.microsoft.com/~holla...ieeefloat.html (that
      > looks correct to me, i haven't read it all)
      >
      > If you want a higher precision, you'll have to use a double precision
      > floating point variable, but i don't know whether this exists in javascript.[/color]

      You can use the toFixed() method to convert a Number object to a fixed
      number of decimal places.

      e.g.

      (1.159).toFixed (2)

      would return 1.16

      ref: http://tinyurl.com/3ys5x


      drew.

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: parseFloat function seems to have a bug when summing? help!

        "Tim Fooy" <this_is@invali d.com> writes:
        [color=blue]
        > No, it is not a bug. The reason for this result is the fact that the
        > internal representation of a float (which follows IEEE 754 standard) is
        > limited to 7 correct digits, because of the limitation to 32 bits.[/color]

        Actually, Javascript numbers are 64 bit (double precission) IEEE 754
        floating point numbers, so it has 53 significant bits. The problem is
        not the number of significant digits itself, but merely the fact that it
        is finite. There are some numbers that look simple in base ten that
        cannot be represented exactly in base two. The small error on the
        53rd bit accumulates when you do arithmetic, until it becomse visible.

        Try something as simple as
        alert(0.1 + 0.1 + 0.1);
        which alerts 0.3000000000000 0004 in my browser.
        [color=blue]
        > If you want a higher precision, you'll have to use a double precision
        > floating point variable, but i don't know whether this exists in javascript.[/color]

        It does, it's all there is.
        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Dr John Stockton

          #5
          Re: parseFloat function seems to have a bug when summing? help!

          JRS: In article <408f9481$0$217 77$a0ced6e1@new s.skynet.be>, seen in
          news:comp.lang. javascript, Tim Fooy <this_is@invali d.com> posted at Wed,
          28 Apr 2004 13:25:13 :[color=blue]
          >No, it is not a bug. The reason for this result is the fact that the
          >internal representation of a float (which follows IEEE 754 standard) is
          >limited to 7 correct digits, because of the limitation to 32 bits. See e.g.
          >http://research.microsoft.com/~holla...ieeefloat.html (that
          >looks correct to me, i haven't read it all)
          >
          >If you want a higher precision, you'll have to use a double precision
          >floating point variable, but i don't know whether this exists in javascript.[/color]

          What you write would be true if Numbers in javascript were represented
          by IEEE Singles. They are represented by IEEE Doubles.

          Although the OP only gets 7 digits as desired, the error is actually of
          the order of 1 in 1E16.


          I cannot explain why the OP gets 31.992185999999 997 ; ISTM that one gets
          31.992185999999 996.

          The OP could have tested +"10.664062" + +"10.664062" + +"10.664062" or
          10.664062 + 10.664062 + 10.664062 or 3*10.664062 all of which
          give me 31.992185999999 996 - which shows that the matter has nothing
          to do with parseFloat.

          The OP should have read the FAQ, specifically Sec 4.7, and what it links
          to and/or my site and/or many others.

          Try 0.2 + 0.4 and 0.1 + 0.7.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • Randy Webb

            #6
            Re: parseFloat function seems to have a bug when summing? help!

            DrewM wrote:
            [color=blue]
            > You can use the toFixed() method to convert a Number object to a fixed
            > number of decimal places.
            >
            > e.g.
            >
            > (1.159).toFixed (2)
            >
            > would return 1.16[/color]

            And then wonder why you get errors for .007 ? Its well documented in
            the c.l.j FAQ, http://www.jibbering.com/faq/#FAQ4_6


            --
            Randy
            Chance Favors The Prepared Mind
            comp.lang.javas cript FAQ - http://jibbering.com/faq/

            Comment

            Working...