Using Two''s complement math for SIN and COS
An ancient but reliable method is to store a look-up table for SIN, with
enough entries for sufficient accuracy (and memory) for your
application. You need to store just one quadrant (0 - 90 deg), and then
use trig identities (see equantions in Pierce's Tables) for the other 3
quadrants.
Dennis
Original Message
From: Tracy Allen [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=gpP2XUMSP5lqxK7jweEASx9FU3LFeoz0F-z0uytgj5ZZf-a2FW2WYxIDDcC5L1N8lxjC1Kx2AYxVcqkK]tracy@e...[/url
Sent: Thursday, August 08, 2002 9:26 PM
To: basicstamps@yahoogroups.com
Subject: Re: [noparse][[/noparse]basicstamps] Using Two's complement math for SIN and COS
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.
enough entries for sufficient accuracy (and memory) for your
application. You need to store just one quadrant (0 - 90 deg), and then
use trig identities (see equantions in Pierce's Tables) for the other 3
quadrants.
Dennis
Original Message
From: Tracy Allen [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=gpP2XUMSP5lqxK7jweEASx9FU3LFeoz0F-z0uytgj5ZZf-a2FW2WYxIDDcC5L1N8lxjC1Kx2AYxVcqkK]tracy@e...[/url
Sent: Thursday, August 08, 2002 9:26 PM
To: basicstamps@yahoogroups.com
Subject: Re: [noparse][[/noparse]basicstamps] Using Two's complement math for SIN and COS
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.