PICaxe Basic is fine for adding and subtracting 2's complement numbers (up to +/- 32767) but can't do multiplication or division (directly). However, you only need to negate any negative numbers (i.e. if the MSB = 1), then divide as usual and negate the result (unless both numerator and denominator were negative).google says add the values and divide by number of values. added values<32768 I suppose.no a word
Thanks After some experimentation that's where I ended up, the addition is no problem for positive or negative numbers, just the final division has to be handled appropriately. That's aside from the potential overflow issue, but I can live with reducing the resolution to handle that.Hi,
PICaxe Basic is fine for adding and subtracting 2's complement numbers (up to +/- 32767) but can't do multiplication or division (directly). However, you only need to negate any negative numbers (i.e. if the MSB = 1), then divide as usual and negate the result (unless both numerator and denominator were negative).
Cheers, Alan.
#Macro Add( dst, src )
If src >= $8000 Then
dst = src / 4 | $C000 + dst
Else
dst = src / 4 + dst
End If
#EndMacro
w1 = -400
w2 = -200
w3 = -100
w4 = 100
w0 = 0
Add( w0, w1 )
Add( w0, w2 )
Add( w0, w3 )
Add( w0, w4 )
If w0 >= $8000 Then
w0 = -w0
SerTxd( "-", #w0 )
Else
SerTxd( #w0 )
End If