Re: JavaScript Bug?

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 UTC