# Programming Logic Doubt

## Recommended Posts

Hello Everyone,I executed this looping logic: <html><body><script type="text/vbscript">For i = 0 To 5 step 0.02 document.write("The number is " & i & "<br />")Next</script></body></html>and i got this result:The number is 0The number is 0.02The number is 0.04The number is 0.06The number is 0.08The number is 0.1The number is 0.12The number is 0.14The number is 0.16The number is 0.18The number is 0.2The number is 0.22The number is 0.24The number is 0.26The number is 0.28The number is 0.3The number is 0.32The number is 0.34The number is 0.36The number is 0.38The number is 0.4The number is 0.42The number is 0.44The number is 0.46The number is 0.48The number is 0.5The number is 0.52The number is 0.54The number is 0.56The number is 0.58The number is 0.6The number is 0.62The number is 0.64The number is 0.66The number is 0.68The number is 0.7The number is 0.72The number is 0.74The number is 0.76The number is 0.78The number is 0.8The number is 0.82The number is 0.84The number is 0.86The number is 0.88The number is 0.9The number is 0.92The number is 0.940000000000001The number is 0.960000000000001The number is 0.980000000000001The number is 1The number is 1.02The number is 1.04The number is 1.06The number is 1.08The number is 1.1The number is 1.12The number is 1.14The number is 1.16The number is 1.18The number is 1.2The number is 1.22The number is 1.24The number is 1.26The number is 1.28The number is 1.3The number is 1.32The number is 1.34The number is 1.36The number is 1.38The number is 1.4The number is 1.42The number is 1.44The number is 1.46The number is 1.48The number is 1.5The number is 1.52The number is 1.54The number is 1.56The number is 1.58The number is 1.6The number is 1.62The number is 1.64The number is 1.66The number is 1.68The number is 1.7The number is 1.72The number is 1.74The number is 1.76The number is 1.78The number is 1.8The number is 1.82The number is 1.84The number is 1.86The number is 1.88The number is 1.9The number is 1.92The number is 1.94The number is 1.96The number is 1.98The number is 2The number is 2.02The number is 2.04The number is 2.06The number is 2.08The number is 2.1The number is 2.12The number is 2.14The number is 2.16The number is 2.18The number is 2.2The number is 2.22The number is 2.24The number is 2.26The number is 2.28The number is 2.3The number is 2.32The number is 2.34The number is 2.36The number is 2.38The number is 2.4The number is 2.42The number is 2.44The number is 2.46The number is 2.48The number is 2.5The number is 2.52The number is 2.54The number is 2.56The number is 2.58The number is 2.6The number is 2.62The number is 2.64The number is 2.66The number is 2.68The number is 2.7The number is 2.72The number is 2.74The number is 2.76The number is 2.78The number is 2.8The number is 2.82The number is 2.84The number is 2.86The number is 2.88The number is 2.9The number is 2.92The number is 2.94The number is 2.96The number is 2.98The number is 3The number is 3.02The number is 3.04The number is 3.06The number is 3.08The number is 3.1The number is 3.12The number is 3.14The number is 3.16The number is 3.18The number is 3.2The number is 3.22The number is 3.24The number is 3.26The number is 3.28The number is 3.3The number is 3.32The number is 3.34The number is 3.36The number is 3.38The number is 3.4The number is 3.42The number is 3.44The number is 3.46The number is 3.48The number is 3.5The number is 3.52The number is 3.54The number is 3.56The number is 3.58The number is 3.6The number is 3.62The number is 3.64The number is 3.66The number is 3.68The number is 3.7The number is 3.72The number is 3.74The number is 3.76The number is 3.78The number is 3.8The number is 3.82The number is 3.84The number is 3.86The number is 3.88The number is 3.9The number is 3.92The number is 3.94The number is 3.96The number is 3.98The number is 4The number is 4.02The number is 4.04The number is 4.06The number is 4.08The number is 4.1The number is 4.12The number is 4.14The number is 4.16The number is 4.18The number is 4.2The number is 4.22The number is 4.24The number is 4.26The number is 4.28The number is 4.3The number is 4.32The number is 4.34The number is 4.36The number is 4.37999999999999The number is 4.39999999999999The number is 4.41999999999999The number is 4.43999999999999The number is 4.45999999999999The number is 4.47999999999999The number is 4.49999999999999The number is 4.51999999999999The number is 4.53999999999999The number is 4.55999999999999The number is 4.57999999999999The number is 4.59999999999999The number is 4.61999999999999The number is 4.63999999999999The number is 4.65999999999999The number is 4.67999999999999The number is 4.69999999999999The number is 4.71999999999999The number is 4.73999999999999The number is 4.75999999999999The number is 4.77999999999999The number is 4.79999999999999The number is 4.81999999999999The number is 4.83999999999998The number is 4.85999999999998The number is 4.87999999999998The number is 4.89999999999998The number is 4.91999999999998The number is 4.93999999999998The number is 4.95999999999998The number is 4.97999999999998The number is 4.99999999999998Can anyone please explain this discrepancy??This is an observation with any programming language, not specific to vbScript.

Edited by sktanti007
##### Share on other sites

• 2 months later...

What is wrong with it? It counted to 5.

##### Share on other sites

• 2 months later...

I think maybe he's referring to the instance where the decimal places were greater than 2, and weren't just increments of .02.

##### Share on other sites

Ah, this topic is really old... but if anyone is still curious, the reason why the floats are inaccurate is because in the computer, the numbers have to be stored as combinations of binary fractions, i.e. 1/2, 1/4, 1/8, 1/16 etc., but this system can't neatly represent numbers that may be neatly represented in decimal.Take 4.37, for example, if stored unsigned over 8 bits with 4 for the integer part, the best we can get is 01000110 which is 4.375. Not very close! :)More information: http://en.wikipedia.org/wiki/IEEE_754-2008.

##### Share on other sites

Ah, this topic is really old... but if anyone is still curious
yup, haha. I was a bit bored the other day and was a bit curious. Tried it out myself in javascript and a similar thing happened. Good to know the answer now.