Having a problem with this code.
my coding is the following:
fitvalchild1 = 0
fitvalchild2 = 0
For i = 1 To 30
counter = 30
Do While counter > 0
fitvalchild1 = fitvalchild1 + child1(counter) * 2 ^ (i - 1)
fitvalchild2 = fitvalchild2 + child2(counter) * 2 ^ (i - 1)
counter = counter - 1
Loop
Next i
all the variables are declared as long…
Still i get the Error : overflow and the line highlighted is:
fitvalchild1 = fitvalchild1 + child1(counter) * 2 ^ (i - 1)
The
Longdatatype has a maximum size of 2,147,483,647.2 ^ (30-1) = 536,870,912
so, depending on the value of
child1(counter)andfitvalchild1, you’ll easily exceed the maximum.You’re adding the value to itself with each loop, so after 4-5 loops, you’ll likely get the error.
If you don’t mind switching away from an integer type, you might try using
Currencydatatype. It has a maximum size of 922,337,203,685,477.5807