Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Potential bug in VB's loop iteration

1 view
Skip to first unread message

Matthew Wills

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
All,

I have come across what (may be) a bug in VB. Can anyone shed some light on
what they think could be going on?

Compare the following three loops and the results

Dim c As Currency

For c = 0@ To 5670000@ Step 5.67@
Next

MsgBox c
'5670005.67

For c = 0@ To 113400000@ Step 567@
Next

MsgBox c
'113400567

For c = 0@ To 1134000@ Step 5.67@
Next

MsgBox c
'858993.66 ???

Matthew

Matthew Wills

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
All,

Don't worry - MS has got back to me and confirmed it as a bug. Pretty scary
in my books.

Matthew

Matthew Wills <Te...@test.com.au> wrote in article
<01be69e4$0ad5db40$149e53a0@ws020227>...

Neila Nessa

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
Matthew,
That is *VERY* scary. I replicated it on VB6. A workaround is to change the
Currency to a Double.
How did you happen to discover this one? Any ideas as to where it might
creap into play and
destroy the fabric of civil-lies-ation?
Neila

Matthew Wills wrote in message <01be69f8$b8ebf570$149e53a0@ws020227>...

matthe...@my-dejanews.com

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
Neila,

Comments inline.

Matthew

In article <Oc0pFTka#GA.128@upnetnews03>,


"Neila Nessa" <neila...@msn.com> wrote:
> Matthew,
> That is *VERY* scary. I replicated it on VB6. A workaround is to change the
> Currency to a Double.

Not a very good workaround - doubles have problems of their own (being fp).

> How did you happen to discover this one? Any ideas as to where it might
> creap into play and
> destroy the fabric of civil-lies-ation?

I was doing some unit testing of my number to words algorithm when I stumbled
onto it.

The MS guy said the following:

<Start>
This is certainly a fairly convincing bug!

A bit more playing around with it reveals that the for/next loop's comparison
will fail if the 32nd bit of the number is set. A currency is represented
internally as a large integer (64 bits), and the decimal point is
left-shifted four places. If the comparison number has the bit equivalent to
214748.3648 set (the 32nd bit), then the comparison will fail. (It basically
forgets about this bit)

This will only occur with for/next loops, and will only be present in the
environment, or in p-code executables.

I will bug this immediately. If you wish to pursue a hotfix please let me
know. <End>

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Joe "Nuke Me Xemu" Foster

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
Matthew Wills <Te...@test.com.au> wrote in article <01be69e4$0ad5db40$149e53a0@ws020227>...

> For c = 0@ To 1134000@ Step 5.67@
> Next

> MsgBox c
> '858993.66 ???

I got the same behavior in VB4-32 and Access '97 VBA, but not in VB3 or
VB4-16. (I don't have VB5, 6, or Office '95 on my poor overloaded Libretto)
Didn't MS just come out with VS 6.0 Service Pack 2 already? Sheesh!

--
Joe Foster <mailto:jfo...@ricochet.net> Space Cooties! <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above They're coming to
because my cats have apparently learned to type. take me away, ha ha!

0 new messages