I tried following html code
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
alert(99999999999999.99)
//-->
</SCRIPT>
</BODY>
</HTML>
output i expecting alert popup 99999999999999.99
But output is 99999999999999.98
WHY????
1.- Because in JS decimal numbers are represented internally in binary
"IEEE754 fp double (binary64)" format: 53 bits long mantissa * 10 bits
exponent * 1 bit sign.
2.- Because 0.99 isn't a sum of powers of 2, it can't be represented
exactly in a binary format: the best approximation possible in 53 bits
is: "0.1111110101110000101000111101011100001010001111010111".
3.- As 99999999999999 occupies 47 bits of the 53 bits internally
available: "10110101111001100010000011110100011111111111111"...
4.- ... there are only 6 bits left for the fractional part, so .99
becomes "0.111111" that is : (2^-1) + (2^-2) + (2^-3) + (2^-4) +
(2^-5) + (2^-6) === 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 === 0,984375.
5.- You can check it yourself:
javascript:alert(99999999999999.99-99999999999999) //-> 0.984375
Reducing the integer part of the number would "leave more bits
available" for the fractional part:
(2 "nines" less
javascript:alert(999999999999.99-999999999999) //-> 0.989990234375
(closer to .99)
(4 "nines" less
javascript:alert(9999999999.99-9999999999) //-> 0.9899997711181641
(even closer)
(7 "nines" less
javascript:alert(9999999.99-9999999) //-> 0.9900000002235174 (even
closer)
etc.
You can "see" the numbers here:
http://jorgechamorro.com/cljs/016/