# WTF, VB6? Part 3

This is the third post in the WTF, VB6? series.The previous ones can be found here and here.

How many times do you think the loop bellow will run?

Sub Main()

Const BIG_NUMBER = 9.00719925474099E+15

Dim I As Double

For I = BIG_NUMBER To BIG_NUMBER + 4

Debug.Print "-";

Next

End Sub

It will run forever.

WTF, VB6?!?

This is not itself a VB6 bug, although one can say VB6 makes it easier to happen.

See,

**Double**s have 64 bits, even in 32-bit systems. They follow IEEE 754's spec.

One bit is reserved to be used as a sign. If it is clear (0), then the number is positive. If it is set (1), then the number is negative.

11 bits are used to its bias. The bias is used as an exponent to the number contained in the remaining bits. That's what allows doubles to represent numbers as big as ones having 309 digits!

1 bit is implicit, meaning it is taken into account but does not need to be "physically" "reserved".

So, we're left with 53 bits to represent our number. That's 52 plus the implicit one.

The greater number that can be represented with 53 bits is 9,007,199,254,740,991, or 9.00719925474099E+15 as VB6 likes to call it.

Now, maybe you noticed that there's a difference between 9,007,199,254,740,991 and 9.00719925474099E+15 (that's 9,007,199,254,740,988 "abbreviated" by VB6).

That difference gets us two bits to use yet. Meaning we can add three to it, but not four.

When you increment it for the fourth time, nothing happens, as all bits are already taken. It does not overflow either.

We can keep adding one to it forever, that it will never get to 9.00719925474099E+15 + 4.

*Presto*, an infinite loop!

Andrej Biasic

2020-04-29