Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

arithmetic mistakes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Clive Tolley - 27 Jan 2005 02:01 GMT
I have just debugged various errors in a program for Word as due to VBA's
apparent inability to perform simple arithmetic. A simple piece of code such
as:
   Dim s As Single
   s = 0.05
   For i = 1 To 240
   s = s + 0.1
   MsgBox s
   Next i
produces errors on about a third the numbers generated (and the number type
seems to make no difference - same with Double, anyway).
What is going on?
Jezebel - 27 Jan 2005 02:22 GMT
What you're seeing is the accumulating rounding error that comes from using
a single. Floating types are *always* subject to some element of
imprecision. On my machine the function works exactly as expected up to 221,
after which a superfluous 0.0000000000001 creeps in. This is not unexpected.
Use the Format$() function to display the required level of precision, and
do some research on programming with floats -- this is a basic issue you
have to deal with in any programming language.

This shortcoming with VBA singles is well-documented, to the point that
it's hard to see any justification for ever using this data type.

> I have just debugged various errors in a program for Word as due to VBA's
> apparent inability to perform simple arithmetic. A simple piece of code such
[quoted text clipped - 8 lines]
> seems to make no difference - same with Double, anyway).
> What is going on?

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.