floating point numbers question

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

    floating point numbers question

    dear group!

    <script language="javas cript">
    var a = 60381.11;
    var b = 1437261.58;
    var c = a + b;
    alert (c); -> 1497642.6900000 002

    var a = 60381.11;
    var b = 1437261.50;
    var c = a + b;
    alert (c); -> 1497642.61

    </script>

    can anybody help me with this? Obviously some kind of precision
    problem, but those numbers are not big at all. Any ideas where to find
    documentation of internal data representation in js? Any hint is
    welcome.


    Benedikt
  • Douglas Crockford

    #2
    Re: floating point numbers question

    > var a = 60381.11;[color=blue]
    > var b = 1437261.58;
    > var c = a + b;
    > alert (c); // 1497642.6900000 002[/color]
    [color=blue]
    > can anybody help me with this? Obviously some kind of precision
    > problem, but those numbers are not big at all. Any ideas where to find
    > documentation of internal data representation in js?[/color]

    JavaScript Numbers are 64-bit floating point as specified in IEEE 754. It is the
    same representation that Java calls Double.

    The scheme is intended to preserve as much precision as possible, but it has
    some significant weakness. For example, it is not able to exactly represent
    common fractions such as 1/10 or 1/100. It can at best approximate them. As you
    can see, with a single operation you begin accumulating noticeable error. Also,
    be aware that the associative law and distributive law do not apply.

    I think floating point is the wrong representation for numbers in most
    applications. However, it is extremely popular, and popularity is much more
    important these days than suitability or reliability. I recommend that in
    applications that require exactness (for example, when working with money) scale
    your values by a suitable factor (such as 100) so that all of your arithmetic
    will be on whole numbers, which are exact.

    See http://docs.sun.com/db/doc/800-7895/6hos0aoua?a=view

    Comment

    • Lee

      #3
      Re: floating point numbers question

      Benedikt Wismans said:[color=blue]
      >
      >dear group!
      >
      ><script language="javas cript">
      >var a = 60381.11;
      >var b = 1437261.58;
      >var c = a + b;
      >alert (c); -> 1497642.6900000 002
      >
      >var a = 60381.11;
      >var b = 1437261.50;
      >var c = a + b;
      >alert (c); -> 1497642.61
      >
      ></script>
      >
      >can anybody help me with this? Obviously some kind of precision
      >problem, but those numbers are not big at all. Any ideas where to find
      >documentatio n of internal data representation in js? Any hint is
      >welcome.[/color]



      Comment

      • Richard Cornford

        #4
        Re: floating point numbers question

        "Benedikt Wismans" <benedikt.wisma ns@wiai.uni-bamberg.de> wrote in
        message news:f997040e.0 309221355.214f7 ae6@posting.goo gle.com...
        <snip>[color=blue]
        >... . Any ideas where to find documentation of internal
        >data representation in js? Any hint is welcome.[/color]

        ANSI/IEEE Std 754-1985: IEEE Standard for Binary
        Floating-Point Arithmetic. Institute of Electrical
        and Electronic Engineers, New York (1985).

        Richard.


        Comment

        • VK

          #5
          Re: floating point numbers question

          Try this:
          ....
          // case 1
          var a = 60381.11;
          var b = 1437261.58;
          var c = a + b;
          alert (c); //-> 1497642.6900000 002

          // case 2
          var a = 60381.11;
          var b = 1437261.50;
          var c = a + b;
          alert (c); //-> 1497642.61 !

          // case 3
          a = 60381.11;
          b = 1437261.58;
          c = a + b;
          alert (c); //-> 1497642.6900000 002 again!

          // case 4
          var d = 60381.11;
          var e = 1437261.50;
          var f = a + b;
          alert (f); //-> 1497642.6900000 002 !
          ....

          So it's not so much problem of floating point, but how JavaScript allocates
          new variables (case 1 and 4), reinitializes existing variables (case 2, it
          evidently makes some kind of auto-rounding or hell knows what), assign new
          value to existing variables (case 3)

          Never noticed that before... Very interesting...
          Any way, if you have to work with floating numbers, make a paper sticker for
          yourself:
          "(0 == -0) = false"
          and place it near of your screen as a reminder that all math on the computer
          is approximate and it's never equal to the real value, only up to some
          point. This is why for example 0 never equal -0, unless you are using
          special tricks and/or libraries.


          Benedikt Wismans <benedikt.wisma ns@wiai.uni-bamberg.de> wrote in message
          news:f997040e.0 309221355.214f7 ae6@posting.goo gle.com...[color=blue]
          > dear group!
          >
          > <script language="javas cript">
          > var a = 60381.11;
          > var b = 1437261.58;
          > var c = a + b;
          > alert (c); -> 1497642.6900000 002
          >
          > var a = 60381.11;
          > var b = 1437261.50;
          > var c = a + b;
          > alert (c); -> 1497642.61
          >
          > </script>
          >
          > can anybody help me with this? Obviously some kind of precision
          > problem, but those numbers are not big at all. Any ideas where to find
          > documentation of internal data representation in js? Any hint is
          > welcome.
          >
          >
          > Benedikt[/color]


          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: floating point numbers question

            "VK" <schools_ring@y ahoo.com> writes:
            [color=blue]
            > // case 4
            > var d = 60381.11;
            > var e = 1437261.50;
            > var f = a + b;[/color]

            do you mean "d + e"?
            [color=blue]
            > So it's not so much problem of floating point,[/color]

            Yes it is :).
            [color=blue]
            > Never noticed that before... Very interesting...
            > Any way, if you have to work with floating numbers, make a paper sticker for
            > yourself:
            > "(0 == -0) = false"[/color]

            That would be confuzing, since
            alert(0 == -0)
            alerts "true". But yes, they have different internal representations .
            There are also several million different versions of NaN in IEEE
            floating point numbers, but Javascript/ECMAScript makes them all
            equal.

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Dr John Stockton

              #7
              Re: floating point numbers question

              JRS: In article <f997040e.03092 21355.214f7ae6@ posting.google. com>, seen
              in news:comp.lang. javascript, Benedikt Wismans
              <benedikt.wisma ns@wiai.uni-bamberg.de> posted at Mon, 22 Sep 2003
              14:55:48 :-
              [color=blue]
              ><script language="javas cript">
              >var a = 60381.11;
              >var b = 1437261.58;
              >var c = a + b;
              >alert (c); -> 1497642.6900000 002
              >
              >var a = 60381.11;
              >var b = 1437261.50;
              >var c = a + b;
              >alert (c); -> 1497642.61
              >
              ></script>
              >
              >can anybody help me with this? Obviously some kind of precision
              >problem, but those numbers are not big at all. Any ideas where to find
              >documentatio n of internal data representation in js? Any hint is
              >welcome.[/color]

              See the Mon/Fri newsgroup FAQ, and js-maths.htm on my site, among
              numerous others.

              --
              © 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> JS maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

              Comment

              • Dr John Stockton

                #8
                Re: floating point numbers question

                JRS: In article <bko047$ojs$1@t itan.btinternet .com>, seen in
                news:comp.lang. javascript, Richard Cornford
                <richard@litote s.demon.co.uk> posted at Mon, 22 Sep 2003 23:20:39 :-[color=blue]
                >"Benedikt Wismans" <benedikt.wisma ns@wiai.uni-bamberg.de> wrote in
                >message news:f997040e.0 309221355.214f7 ae6@posting.goo gle.com...
                ><snip>[color=green]
                >>... . Any ideas where to find documentation of internal
                >>data representation in js? Any hint is welcome.[/color]
                >
                >ANSI/IEEE Std 754-1985: IEEE Standard for Binary
                >Floating-Point Arithmetic. Institute of Electrical
                >and Electronic Engineers, New York (1985).[/color]

                <URL:http://www.merlyn.demo n.co.uk/js-maths.htm#Float >
                <URL:http://www.merlyn.demo n.co.uk/pas-real.htm>
                <URL:http://www.merlyn.demo n.co.uk/pas-type.htm>
                are on-line, containing information and links.

                Present javascript numbers are IEEE Doubles, and so have one sign bit,
                51 magnitude bits with an additional implicit 1, and 11 exponent bits.

                Any integer from 0 up to 2^53 = 9,007,199,254,7 40,992 in magnitude
                should be stored exactly. The range slightly exceeds +-1.7×10^308.

                --
                © 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> JS maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                Comment

                • asdf asdf

                  #9
                  Re: floating point numbers question

                  For a simpler demonstration:

                  alert(2-1.1);

                  This is also a problem in Java and databases. Solutions include:
                  -doing your math using whole numbers and remembering where you want
                  the decimal point just for formatting
                  -BigInteger, BigDecimal
                  -Currency datatype

                  Comment

                  Working...