The Basic Stamp 2 (tm), although being a very nice little computer to play with, has limited math capabilities. The main limitations are:

- The fact that ony integer numbers are used by the Stamp
- The limited range of these numbers (max from 0 to 65535)

From time to time, one would like to do some "stronger" math. There are indeed ways to try and overcome these limitations (although sometimes at a very high cost in terms of memory!).

The best way, when possible, is to use properly the Stamp's math operations. This is particularly true when, for example, you have to multiply or divide a variable by a constant number, i.e. a number that never changes. This can usually be done with small "tricks". To do a silly exampe, let's say that you where to multiply a variable by a constant fractionary number, like 2.5. Multiplying by 2.5 is equivalent to multplying by 5 and then dividing by 2. See the page about the fractionary approach for more details.

So, what is the difference between the two? The names say it all. The BCD routines store the numbers in a BCD (binary coded decimal) format, the MAT32 store the numbers in a binary format.

BCD format is a quite funny way to keep numbers, and stores one decimal digit in 4 bits, equivalent to half a byte, and to a nibble. As the BS2 is able to work with nibble variables, this is quite handy. You can declare an array of nibbles and store in each nibble a decimal digit:

Binary format is the more usual way of representing numbers. In the Stamp, you have nibble variables (4 bits, can store numbers 0 to 15), byte variables (8 bits, 0 to 255) and word variables (16 bits, 0 to 65535). On other machines, you also have long variables (32 bits, 0 to 4294967295).number var nibble (10) ' This variable can store 10 decimal digits!

The binary math routines somehow implement 32-bit calculations, at the expense of about one third of the program space and of most of the available RAM (you can anyway re-use a lot of the RAM used by MAT32). This include sum, subtract, multiply and divide.

But displaying (through DEBUG, on an LCD etc.) a BCD number is straightforward, while it is quite complex to display a 32-bit binary number. So complex that my routines dont't even attempt to do it, and just display numbers in hexadecimal.

So, if you just need to do calculations, the MAT32 routines are probably the best: more complete, faster, lighter on system resorces etc.

If you also need to display you results in decimal, you should try and play with the BCD routines.

This is only partially, true; for more detail, see the pages about both routines: the BCD routines and the binary routines.