W3C home > Mailing lists > Public > site-comments@w3.org > May 2011

Re: JavaScript Bug?

From: Danny Ayers <danny.ayers@gmail.com>
Date: Thu, 5 May 2011 11:20:21 +0200
Message-ID: <BANLkTi=Fw2b1df3XwHaJ8SVnhSOC+qxnjg@mail.gmail.com>
To: Khurram Adeeb Noorani <khurramnoorani@gmail.com>
Cc: site-comments@w3.org
I've no connection with the W3C, nor ISO who maintain the ECMAScript
specification on which Javascript is based. But I was recently reading
up some material related to the 'bug' you describe.

Javascript uses 64 bit floating point numbers, so that's the accuracy
you're generally dealing with. In this representation you have 0.14 +
0.01 = 0.1500...002. Ok, you've got a (probably rounded) binary fp
number represented in decimal, and it does look wrong.

But imagine if Javascript used a base 10 representation - what does
2/3 look like? In base 3 it's *exactly* 0.2.

I'm sure there are arbitrary-precision numerical libraries for
Javascript available if you need greater accuracy.

See: http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference

Hope that helps.

Cheers,
Danny.

On 4 May 2011 23:25, Khurram Adeeb Noorani <khurramnoorani@gmail.com> wrote:
> Hello!
> I am writing you for the very first time in my life. Today I came across
> something really weired in JavaScript, I would call it a bug and it's my
> request that please have a look at it.
> Please run the following script in your browser and you will see what I
> mean.
> <script type="text/javascript">
> num=0.14;
> num+=0.01;
> alert(num);
> </script>
> Please don't say that rounding up will solve the problem. I know rounding up
> will solve the problem but the point is that there is something wrong. And
> it took hours to debug it because I really had no idea that there could have
> been such type of mistake/bug/issue in JavaScript. I was doing debugging
> somewhere else and the root cause was somewhere else. And the most
> interesting thing is that if you have num=0.13 and you add 0.01 to it, it
> will work fine. Problem occurs ONLY when you have num=0.14 and you add 0.01
> to it.
> It tested the above code on different machines (both Windows and Mac) and
> different browsers (Chrome, Firefox and Safari).
> I've really no idea where to write and who to address therefore I'm sending
> an email to this address, kindly forward it to the concerned authorities.
> --
> regards,
> K.A.Noorani
>



-- 
http://danny.ayers.name
Received on Thursday, 5 May 2011 09:20:48 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 24 October 2012 16:21:33 GMT