Fabulous Adventures In Coding

Eric Lippert's Blog

What could numeric rounding possibly have to do with MS-DOS?

A reader points out that FormatNumber uses yet a different rounding algorithm.  FormatNumber rounds numbers ending in .5's away from zero, not towards evens. He also asks whether FormatNumber, FormatCurrency, etc, actually call into the VBA Format$ code.

 

To answer the question, no.  The VBA runtime is freely redistributable, but it is also large.  Back in 1996 we did not want to force people to download the large VBA runtime.  IE was on our case for every kilobyte we added to the IE download.

 

However, FormatNumber, etc, were written by the same developer who implemented Format$ in VBA.  That was the legendary Tim Paterson, who you may recall wrote a little program called QDOS that was eventually purchased by Microsoft and called MS-DOS.  And let me tell you, when I was an intern in 1993 I had to debug Format$ -- there are very good reasons why we decided to go with a stripped-down let's-solve-the-90%-usage-case version for VBScript!  Format$ is one of the most enormously complicated hard-core-bit-twiddling functions that I've ever seen.  That's what happens to these "everything but the kitchen-sink" functions that tries to be all things to all callers -- they get enormously bloated and complex.  Porting that thing to a new codebase and maintaining it independently would have been nightmarish.

 

Here's a comment that Tim wrote into the FormatNumber code on September 16th, 1996 which confirms the reader's observation:

 

       // We have more digits that we're not using.    Round if needed.

       // This is simplistic rounding that does not use IEEE

       // rules (rounding even if exactly x.5).    This simple

       // way is how VBA Format$ works, so it's good enough for us.

 

Published Friday, September 26, 2003 7:10 PM by Eric Lippert
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Eric W. Bachtal said:

I've always wanted to see the inside of Format$. Over the years my cohorts and I have traded numerous unresolved bets about what it must be doing internally for some situation or another. What a wonder. How many other routines express so much functionality from such a tiny facade (give me a couple of parameters, and I'll give you the world!) Thanks for the little peek. In so much as you get infinitely more traffic than me, I'd like to repeat this link to KB 196652 here, as a lot of folks want to learn more about rounding and (as importantly) the differences in rounding in various MS products: http://support.microsoft.com/default.aspx?scid=kb;en-us;196652 Thanks again .
September 26, 2003 11:53 PM
 

Eric Lippert said:

> Over the years my cohorts and I have traded numerous unresolved bets about what it must be doing internally for some situation or another Well, I have all the source code on my machine still, so I can settle some of those bets. I was just glancing at it to refresh my memory -- I haven't looked at this thing since 1997 at least. Format$ is about 5500 lines of C++ code (counting blank lines and comments). About 1300 of those lines are the parser that turns the "format picture" into an internal representation. (Incidentally, the implementation of Format in VB.NET is tiny, since most of the work is done by the underlying String.Format method in the framework. )
September 29, 2003 1:50 PM
 

Fabulous Adventures In Coding said:

February 20, 2004 1:52 PM
 

Fabulous Adventures In Coding said:

A number of people have pointed out to me over the years that VBScript's Round function is a bit weird. It seems like it should be pretty straightforward -- you pick the integer closest to the number you've got, end of story. But what about, say, 1.5?

September 6, 2007 6:09 PM

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

About Eric Lippert

Eric Lippert is a senior developer on the Microsoft C# compiler team. Before that he worked on the framework of Visual Studio Tools For Office. Before that, he worked on the compilers, runtimes and tools for VBScript, JScript, Windows Script Host and other Microsoft Scripting technologies. He lives in Seattle and spends his free time editing books about programming languages, playing the piano, and trying to keep his tiny sailboat upright in Puget Sound.

This Blog

Syndication


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker