Velocity Reviews > javascript weird problem ( a newbie)

javascript weird problem ( a newbie)

Subhash
Guest
Posts: n/a

 05-22-2008
See following code:

<script type="text/javascript">
</script>

It shows : 2168954000534143200
It should have shown: 2168954000534143302

Is it because it is a very big number?

Tim Slattery
Guest
Posts: n/a

 05-22-2008
Subhash <(E-Mail Removed)> wrote:

>See following code:
>
>
> <script type="text/javascript">
> </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://www.velocityreviews.com/forums/(E-Mail Removed)
http://members.cox.net/slatteryt

Rich Grise
Guest
Posts: n/a

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

> See following code:
>
> <script type="text/javascript">
> </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

Dr J R Stockton
Guest
Posts: n/a

 05-23-2008
In comp.lang.javascript message <7e003707-02c6-4309-8462-2ab9e2e8822c@q2
7g2000prf.googlegroups.com>, Thu, 22 May 2008 05:07:44, Subhash
<(E-Mail Removed)> posted:
>See following code:
>
> <script type="text/javascript">
> </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.