javascript weird problem ( a newbie)

Discussion in 'Javascript' started by Subhash, May 22, 2008.

  1. Subhash

    Subhash Guest

    See following code:


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


    It shows : 2168954000534143200
    It should have shown: 2168954000534143302

    Is it because it is a very big number?
     
    Subhash, May 22, 2008
    #1
    1. Advertising

  2. Subhash

    Tim Slattery Guest

    Subhash <> wrote:

    >See following code:
    >
    >
    > <script type="text/javascript">
    > alert (2168954000534143302);
    > </script>
    >
    >
    >It shows : 2168954000534143200
    >It should have shown: 2168954000534143302
    >
    >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:
    http://en.wikipedia.org/wiki/Floating_point

    --
    Tim Slattery

    http://members.cox.net/slatteryt
     
    Tim Slattery, May 22, 2008
    #2
    1. Advertising

  3. Subhash

    Rich Grise Guest

    On Thu, 22 May 2008 05:07:44 -0700, Subhash wrote:

    > See following code:
    >
    > <script type="text/javascript">
    > alert (2168954000534143302);
    > </script>
    >
    >
    > It shows : 2168954000534143200
    > It should have shown: 2168954000534143302
    >
    > Is it because it is a very big number?


    Yes.

    Try this:
    > <script type="text/javascript">
    > alert ("2168954000534143302"); // note quotes
    > </script>


    Have Fun!
    Rich
     
    Rich Grise, May 22, 2008
    #3
  4. In comp.lang.javascript message <7e003707-02c6-4309-8462-2ab9e2e8822c@q2
    7g2000prf.googlegroups.com>, Thu, 22 May 2008 05:07:44, Subhash
    <> posted:
    >See following code:
    >
    > <script type="text/javascript">
    > alert (2168954000534143302);
    > </script>
    >
    >It shows : 2168954000534143200
    >It should have shown: 2168954000534143302
    >
    >Is it because it is a very big number?


    The actual value stored for that number, in an IEEE Double, is
    +2168954000534143232.0 but the default conversion to string zeroes
    meaningless digits.
    <URL:http://www.merlyn.demon.co.uk/js-misc0.htm#DW4> etc.

    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.com/faq/index.html>.
    <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 J R Stockton, May 23, 2008
    #4
    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. dorayme
    Replies:
    1
    Views:
    630
    richard
    Jan 21, 2011
  2. richard
    Replies:
    0
    Views:
    592
    richard
    Jan 21, 2011
  3. richard
    Replies:
    0
    Views:
    620
    richard
    Jan 21, 2011
  4. Beauregard T. Shagnasty

    Re: A Weird Appearance for a Weird Site

    Beauregard T. Shagnasty, Jan 21, 2011, in forum: HTML
    Replies:
    1
    Views:
    446
    Captain Paralytic
    Jan 21, 2011
  5. will
    Replies:
    6
    Views:
    422
    Phrogz
    Dec 27, 2006
Loading...

Share This Page