By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,692 Members | 1,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,692 IT Pros & Developers. It's quick & easy.

javascript weird problem ( a newbie)

P: n/a
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?
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Subhash <su************@gmail.comwrote:
>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
Sl********@bls.gov
http://members.cox.net/slatteryt
Jun 27 '08 #2

P: n/a
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

Jun 27 '08 #3

P: n/a
In comp.lang.javascript message <7e003707-02c6-4309-8462-2ab9e2e8822c@q2
7g2000prf.googlegroups.com>, Thu, 22 May 2008 05:07:44, Subhash
<su************@gmail.composted:
>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#DW4etc.

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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.