|
||||||
How to Create Trigonometric Macros in CalcUsing Degrees as Inputs to Trigonometric Functions in OpenOffice
Trigonometric functions in Calc (and other spreadsheets) work in radians rather than degrees; however a few simple macros will put that right.
OpenOffice Calc (like all spreadsheets) has built in trigonometric functions such as cos, sin and tan; and, of course, inverse trigonometric functions - acos, asin and atan. However, also like other spreadsheets such as Microsoft Excel, these functions work in in radians and not degrees (which is what most people work with). Fortunately it is very easy to program Calc macros that enable users to work with degrees rather than radians Using the Calc Trigonometric FunctionsOpenOffice Calc has the trigonometric functions that any user would expect:
However, these expect the input to be in radians; therefore to find the sine of 30 degrees (for example) the following would have to entered into a Calc cell: =SIN(DEGREES(30))
The answer is, of course, 0.5; and to covert back to degrees the inverse trigonometric functions are needed:
and so the following would be used covert from sine back to degrees: =DEGREES(ASIN(0.5))
Although not difficult to use trigonometry can be made even easier by programming a few Calc macros. Using the Calc Trigonometric Functions in a MacroThe Calc trigonometric functions are not actually available to a macro by default - and so the following code will result in an error: rad = radians(angle)
Instead an OpenOffice Uno (Universal network object) must used to call the functions; therefore, since these functions will be used a lot, it's worth incorporating this into it's own macro: Function ooo_function (function_name as String, ip_array)
Dim mFunction
mFunction = createUnoService("com.sun.star.sheet.FunctionAccess")
ooo_function = mFunction.callFunction(function_name,ip_array)
End Function
This macro can now be used to simplify any other macros that need to call the Calc trigonometric functions. Cosine, Sine and Tangent Macros for DegreesEach of the trigonometric macros will need to:
Function Cosine (angle As Double) As Double
Dim rad as Double
rad = ooo_function("RADIANS",Array(angle))
Cosine = Cos(rad)
End Function
Function Sine (angle As Double) As Double
Dim rad as Double
rad = ooo_function("RADIANS",Array(angle))
Sine = Sin(rad)
End Function
Function Tangent (angle As Double) As Double
Dim rad as Double
rad = ooo_function("RADIANS",Array(angle))
Tangent = tan(rad)
End Function
Inverse Cosine, Sine and Tangent Macros for DegreesEach of the inverse trigonometric macros will need to:
Function ArcCosine (cosine_value As Double) As Double
Dim rad as Double
rad = ooo_function("ACOS", Array(cosine_value))
ArcCosine = ooo_function("DEGREES",Array(rad))
End Function
Function ArcSine (sine_value As Double) As Double
Dim rad as Double
rad = ooo_function("ASIN", Array(sine_value))
ArcSine = ooo_function("DEGREES",Array(rad))
End Function
Function ArcTangent (tangent_value As Double) As Double
Dim rad as Double
rad = ooo_function("ATAN", Array(tangent_value))
ArcTangent = ooo_function("DEGREES",Array(rad))
End Function
Using the Trigonometric and Inverse Trigonometric macros in a SpreadsheetThe functions can now be used in a spreadsheet, for example: A1: 30
B1: =SINE(A1)
B2: =ARCSINE(B1)
0.5 will be displayed in B1 and 30 in B2; and if A1 is changed then B1 and B2 will update automatically. SummaryOpenOffice.org Calc has built in trigonometric functions:
and inverse trigonometric functions:
The only drawback is that all of them work in radians rather than degrees. Macros can be written that will convert from degrees into their cosines, sines and tangents (and back again); however, the trigonometric and inverse trigonometric functions are not available by default to the macros - for that an OpenOffice Universal network object (Uno) is needed. Once the macros have be created they can be called from spreadsheet just like the standard trigonometric and inverse trigonometric functions
The copyright of the article How to Create Trigonometric Macros in Calc in Computer Programming is owned by Mark Alexander Bain. Permission to republish How to Create Trigonometric Macros in Calc in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||