|
|||||||||||
|
RE: MSIE integer overflows
From: Cameron Brown <cameron(at)greyzone.com>
Date: Thu May 15 2003 - 19:37:03 EDT I'm not a Javascript expert, but I think the issue isn't one of overflow, it's that the engine doesn't really store those ints with 64 bits of precision. Therefore, with high numbers, large == large+1; because there isn't enough precision to know the difference. Observe: <script language="javascript"> for (test=56; test<64; test++) { large = Math.pow(2,test); document.write('Starting with 2^'+test+' ('+large+')<br>'); for (n=0, i=0; n<5; n++) {
while (large+i == large) i++;
larger = large + i;
document.write(large+' != '+larger+'} document.write('<br><br>'); } </script> Starting with 2^57 (144115188075855870) 144115188075855870 != 144115188075855900 144115188075855900 != 144115188075855940 144115188075855940 != 144115188075855970 144115188075855970 != 144115188075856000 144115188075856000 != 144115188075856030 Starting with 2^58 (288230376151711750) 288230376151711750 != 288230376151711800 288230376151711800 != 288230376151711900 288230376151711900 != 288230376151711930 288230376151711930 != 288230376151712000 288230376151712000 != 288230376151712060 Starting with 2^59 (576460752303423500) 576460752303423500 != 576460752303423600 576460752303423600 != 576460752303423700 576460752303423700 != 576460752303423900 576460752303423900 != 576460752303424000 576460752303424000 != 576460752303424100 Starting with 2^60 (1152921504606847000) 1152921504606847000 != 1152921504606847200 1152921504606847200 != 1152921504606847500 1152921504606847500 != 1152921504606847700 1152921504606847700 != 1152921504606848000 1152921504606848000 != 1152921504606848200 Starting with 2^61 (2305843009213694000) 2305843009213694000 != 2305843009213694500 2305843009213694500 != 2305843009213695000 2305843009213695000 != 2305843009213695500 2305843009213695500 != 2305843009213696000 2305843009213696000 != 2305843009213696500 Starting with 2^62 (4611686018427388000) 4611686018427388000 != 4611686018427389000 4611686018427389000 != 4611686018427390000 4611686018427390000 != 4611686018427391000 4611686018427391000 != 4611686018427392000 4611686018427392000 != 4611686018427393000 Starting with 2^63 (9223372036854776000) 9223372036854776000 != 9223372036854777000 9223372036854777000 != 9223372036854780000 9223372036854780000 != 9223372036854781000 9223372036854781000 != 9223372036854784000 9223372036854784000 != 9223372036854786000This shows the resolution of the integer space at various value ranges. Cameron
-----Original Message-----
Yes:
i=32*256*256*256*256*256*256*256; a=i; b=i+1; alert(a+'=='+b+' evaluates to '+(a==b)); -- evaluates to true Berend-Jan Wever
> In-Reply-To: < 004e01c319fb$7ec41050$0100a8c0@grotedoos> > > > > >Not true: "++i" will increase i first and return the result of that > > >increased i where "i++" will return i and then increase it: > > >-- example.js -- > > >var i=1; > > >document.write(++i); // prints 2, i=2; > > >document.write(i++); // prints 2, i=3; > > >-- cut here -- > > > > > > > Yes, of course. > > > > Again, I'm talking about C here, simply because I don't know JS to > this > > level of detail. But... > > > > document.write((i==++i) + ' ' + (i==++i) + 'Received on Fri May 16 11:51:50 2003 This archive was generated by hypermail 2.1.8 : Wed Aug 23 2006 - 14:07:39 EDT |
||||||||||
|
|||||||||||