Bob Balaban's Blog


    Bob Balaban


    And just when you thought you had division down....

    Bob Balaban  March 3 2008 01:45:58 PM
    Greetings, Geeks!

    Everyone who knows me knows that I'm a big fan of LotusScript (it's not dead yet!). I've been writing code using the language for about 15 years now, and I was pretty sure that I'd explored most of the sometimes strange nooks and crannies of its functionality (with the possible exception of "Like", which I have yet to find a real use for....but that's another article).

    Until this morning, that is, when I tripped over a subtle issue with, of all things, arithmetic division.

    I won't go into all the details of my app, they're not relevant. Let's just say I wanted to know the maximum number of 2s in some number, call it N. If N did not have an even number of 2s in it (i.e., if N is not a multiple of 2), then I wanted the nearest possible integer result -- that is, I wanted to do a division and have the result truncated if there was a fractional value.

    So, for example, if N is 6, the result should be 3. If N is 7, the result should be 3, if N is 8, the result should be 4. Simple, right?? You just divide N by 2 (you know both are integers when you start), and assign the result to another integer variable, which should force it to truncate any remainder. Here's the basic code:

      Dim two as Integer, N as Integer, result as Integer
      two = 2
      '....obtain N from somewhere
      result = N / two

    Easy, right? Simple, right? WRONGWRONGWRONG!!

    Rather surprisingly, I noticed I was getting the wrong answer sometimes! When N was 4, "result" was 2 (well, it better be!). But when N was 5, "result" was 3!

    So I muttered to myself, "Self! someone or something evil is rounding on me! But the variables are all integers! What gives?" I went to the Designer Help, which contains surprisingly good and thorough documentation on the LotusScript language. I noticed that "division" is defined as always returning a floating point number!! And, unlike in C and other languages, when LotusScript converts a float to an integer, it automatically  ROUNDS the number (C and C++ truncate).

    As an interesting (to me, anyway) aside, this has always been confusing for people, there's nothing in many (or even most) programming languages that makes it obvious whether float-to-int assignment will either truncate or round. Perhaps that's why in Pascal if you code a direct assignment, the compiler will complain. You have to specify either the Trunc() or Round() function explicitly in that case.

    All very interesting, but I needed a way to force the assignment in LS to give me a truncated integer result. So I thought to myself, "Self! There must be another operator for integer division!" I know I'd never heard of trunc/round functions in LS before, and I sure didn't want to write them. I hadn't ever heard of there being 2 division operators before either, but given the observed behavior of "/" (the floating point division operator), I assumed there would be another one for integer division. I knew about the Cint() function, which converts any data type to integer, but I already knew that that would also round a floating point value.

    Back to Designer Help. Found it! It's the backslash: "\". Thank goodness "\" isn't used as an escape character in LotusScript, as it is in C/C++!

    I replaced "/" with "\" in my code, and all was well. Go figure!

    (Need expert application development architecture/coding help? Contact me at: bbalaban,
    Follow me on Twitter @LooseleafLLC
    This article ┬ęCopyright 2009 by Looseleaf Software LLC, all rights reserved. You may link to this page, but may not copy without prior approval.