Cos
Syntax
Assignment Variable = Cos(pVariable)
Overview
Deduce the Cosine of a floating point value
Parameter
pVariable can be a constant, variable or expression that requires the Cosine extracted. The value expected and returned by Cos is in radians. Assignment Variable can be any valid variable type.
Example
Device = 18F26K40 ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Set the Baud rate for HRsoutLn
Dim Floatin as Float ' Holds the value to Cos with
Dim Floatout as Float ' Holds the result of the Cos
Floatin = 123 ' Load the variable
Floatout = Cos(Floatin) ' Extract the Cos of the value
HRsoutLn Dec Floatout ' Display the result
Stop
Notes
With 12, and 14-bit core devices, Cos returns the 8-bit cosine of a value, compatible with the BASIC Stamp syntax. The result is in two's complement form (i.e. -127 to 127). Cos starts with a value in binary radians, 0 to 255, instead of the customary 0 to 359 degrees.
However, with the extra functionality, and more linear memory offered by the 18F devices, full 32-bit floating point Cosine is implemented.
Floating point trigonometry is extremely memory hungry, so do not be surprised if a large chunk of the microcontroller’s code memory is used with a single operator. This also means that floating point trigonometry is comparatively slow to operate.
When implementing trigonometry, or other built in, functions within an expression, always wrap them in parenthesis, otherwise the parser may consider the extra operands as part of the trigonometry parameter and produce an incorrect result. For example:
MyAssignment = (Cos(MyVar1)) + MyVar2
