javascript weird problem ( a newbie)

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

    javascript weird problem ( a newbie)

    See following code:


    <script type="text/javascript">
    alert (21689540005341 43302);
    </script>


    It shows : 216895400053414 3200
    It should have shown: 216895400053414 3302

    Is it because it is a very big number?
  • Tim Slattery

    #2
    Re: javascript weird problem ( a newbie)

    Subhash <subhashsubhash @gmail.comwrote :
    >See following code:
    >
    >
    <script type="text/javascript">
    > alert (21689540005341 43302);
    </script>
    >
    >
    >It shows : 216895400053414 3200
    >It should have shown: 216895400053414 3302
    >
    >Is it because it is a very big number?
    Yes. It's stored as a floating point number, which consists of an
    exponent and a number. Very similar to scientific notation, where
    numbers are expressed as something times ten to some power. This being
    computers which use binary arithmetic, the exponent is a power of two.

    What you're running into is that the base number (the non-exponent
    part) is of a finite length, and therefore you can cram only so many
    digits of precision into it. Your number is longer than that, so the
    machine stores what it can and you get an approximation of the
    original.

    There's a discussion of floating point number in Wikipedia:


    --
    Tim Slattery
    Slattery_T@bls. gov

    Comment

    • Rich Grise

      #3
      Re: javascript weird problem ( a newbie)

      On Thu, 22 May 2008 05:07:44 -0700, Subhash wrote:
      See following code:
      >
      <script type="text/javascript">
      alert (21689540005341 43302);
      </script>
      >
      >
      It shows : 216895400053414 3200
      It should have shown: 216895400053414 3302
      >
      Is it because it is a very big number?
      Yes.

      Try this:
      <script type="text/javascript">
      alert ("2168954000534 143302"); // note quotes
      </script>
      Have Fun!
      Rich

      Comment

      • Dr J R Stockton

        #4
        Re: javascript weird problem ( a newbie)

        In comp.lang.javas cript message <7e003707-02c6-4309-8462-2ab9e2e8822c@q2
        7g2000prf.googl egroups.com>, Thu, 22 May 2008 05:07:44, Subhash
        <subhashsubhash @gmail.composte d:
        >See following code:
        >
        <script type="text/javascript">
        alert (21689540005341 43302);
        </script>
        >
        >It shows : 216895400053414 3200
        >It should have shown: 216895400053414 3302
        >
        >Is it because it is a very big number?
        The actual value stored for that number, in an IEEE Double, is
        +21689540005341 43232.0 but the default conversion to string zeroes
        meaningless digits.
        <URL:http://www.merlyn.demo n.co.uk/js-misc0.htm#DW4et c.

        It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

        --
        (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE7 FF2 Op9 Sf3
        news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
        <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        Working...