Compartilhar via


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.

Comments

  • Anonymous
    September 26, 2003
    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;196652Thanks again .
  • Anonymous
    September 29, 2003
    > Over the years my cohorts and I have traded numerous unresolved bets about what it must be doing internally for some situation or anotherWell, 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. )
  • Anonymous
    September 06, 2007
    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?