The Cosine operator (COS) returns the two’s complement, 16-bit Cosine of an
angle specified as an 8-bit (0 to 255) value. To understand the COS operator
more completely, let’s look at a typical Cosine function. By definition: given a
circle with a radius of 1 unit (known as a unit circle), the Cosine is the x-coordinate
distance from the center of the circle to its edge at a given angle. Angles are
measured relative to the 3-o'clock position on the circle, increasing as you go
around the circle counterclockwise.
At the origin point (0 degrees) the Cosine is 1, because that point has the
same x (horizontal) coordinate as the circle radius. At 45 degrees the Cosine is
0.707. At 90 degrees, Cosine is 0. At 180 degrees, Cosine is -1. At 270
degrees, Cosine is 0 again.
The BASIC Stamp COS operator breaks the circle into 0 to 255 units instead
of 0 to 359 degrees. Some textbooks call this unit a binary radian or brad.
Each brad is equivalent to 1.406 degrees. And instead of a unit circle,
which results in fractional Cosine values between 0 and 1, BASIC Stamp COS
is based on a 127-unit circle. Results are given in two’s complement form
in order to accommodate negative values. So, at the origin, COS is 127. At 45
degrees (32 brads), COS is 90. At 90 degrees (64 brads), COS is 0. At 180
degrees (128 brads), Cosine is -127. At 270 degrees (192 brads), Cosine is 0 again.
See the image below for the relationship between degrees and brads and how the COS
function values relate to to the 127-unit circle.
degr VAR Word
cosine VAR Word
Main:
FOR degr = 0 TO 359 STEP 45 ' Use degrees
cosine = COS (degr * 128 / 180) ' Convert to brads, do COS
DEBUG "Angle: ", DEC degr, TAB ' Display results
DEBUG "Cosine: ", SDEC cosine, CR
NEXT
END