Shop OBEX P1 Docs P2 Docs Learn Events
Using Two''s complement math for SIN and COS — Parallax Forums

Using Two''s complement math for SIN and COS

ArchiverArchiver Posts: 46,084
edited 2002-08-09 05:26 in General Discussion
You can multiply twos complement values together and get a twos
complement result. That is, so long as the result does not exceed
65535.

On the other hand, division does not work so easily with twos
complement. You have to take the absolute values, divide, and then
restore the sign.

The trig functions COS and SIN in the Stamp are (as I am sure you are
well aware by now!) limited to an input argument of 0 to 255 "brads"
and an output of 0 to +/-127, where unity is represented as 127
(127/128 implied). That will probably be quite limiting for dead
reckoning?!

-- Tracy


>One part of my dead reackoning navigation project involves a vector
>solution of position based on a Lat/Long position, heading and
>distance traveled.
>
>Because I don't really understand how to perform multiply and divide
>operations using two's complement values (which are the form SIN and
>COS come in BS2SX), I have some very messy code (which is
>contributing to a lack of eeprom) to deal with heading directions
>where sin and/or cos return a negative value.
>
>Does anyone have any example code or a good reference to explain how
>to handle SIN and COS in the STAMP. The manual explains the bit
>inversion with add of 1, for negative values, but that doesn't really
>help me.
Sign In or Register to comment.