User Tools

Site Tools


Simplifying arithmetic using strings

Sometimes, rather than building a complicated formula for a piece of code, it can be simplified by using a string of values.

For instance, I was recently updating some Acorn code that read VDU variables so that it would run in BBC BASIC for Windows. The VDU variables are in a slightly different order on BBFW than they are on RISC OS systems:

Acorn VDU(n)
BBFW @vdu%!m
text left side
text left side
text bottom line
text bottom line
text right side
text right side
text top line
text top line

I didn't want to go through the whole program changing every reference, it was easier and safer to build a veneer function to convert Acorn VDU offsets into BBFW VDU offsets.

If you subtract two from the Acorn offset and then multiply by four you get the same range of BBFW offsets, but in a different order. The difficulty was trying to build a formula to get them in the right order.

Looking at the bottom two bits, it needs to convert 0, 1, 2, 3 to 0, 3, 1, 2. I found the easiest way to do that was as follows:

      m=24+4*VALMID$("0312",(n AND 3)+1,1)

This changes 8, 9, 10, 11 to 24, 36, 28, 32. Taking account of @vdu% values being in pixels, not characters, this allowed me to write the following veneer function:

      DEF FNvdu(n)
      IF (bbfw) THEN
        =@vdu%!(24+4*VAL MID$("0312",(n AND 3)+1,1)) DIV (8+8*(n AND 1))
      =VDU n

where bbfw is a variable in the program that indicates that it is running on BBC BASIC for Windows.

Richard Russell, May 2009
The above routine works correctly only in MODEs in which a character cell is 8 pixels wide by 16 pixels high. The alternative code listed below works in all MODEs, and is (arguably) easier to understand:

        DEF FNvdu(n)
        CASE bbfw AND n OF
          WHEN 8: = DIV @vdu%!216
          WHEN 9: = @vdu.tb% DIV @vdu%!220
          WHEN 10: = DIV @vdu%!216
          WHEN 11: = DIV @vdu%!220
        = VDU n

This code relies on bbfw being a Boolean (i.e. having the value 0 or -1).

See also

simplifying_20arithmetic_20using_20strings.txt · Last modified: 2018/04/17 18:47 by tbest3112