PDA

View Full Version : Negative Numbers



Nerd2
04-19-2007, 08:22 AM
How do you debug and have the Basic Stamp 2 do equations with negative numbers.

Mike Green
04-19-2007, 08:39 AM
The short answer is that you can't. The Stamp does all its calculations using 16-bit unsigned arithmetic with values over the range 0 to 65535.

It turns out that some arithmetic operators work on both signed 15-bit numbers and unsigned 16-bit ones like addition (+) and subtraction (-). The Stamp Editor will accept negative numbers and compile them as signed 15-bit numbers in the range -32768 to 32767. Some other operators will also work including ABS(). Crucially, comparisons will not work as expected. Primarily, comparing two negative numbers for less than or greater than will come out backwards. Multiplication and division are unsigned (as well as most shift operators)

The long answer is that you can debug and compute expressions using negative numbers, but it must be with constant awareness of the range of numbers allowed and that certain operators won't work properly with negative values.