How to Create Trigonometric Macros in Calc

Using Degrees as Inputs to Trigonometric Functions in OpenOffice

© Mark Alexander Bain

Nov 28, 2008
Trigonometric Macros for OpenOffice Calc, Mark Alexander Bain
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 Functions

OpenOffice Calc has the trigonometric functions that any user would expect:

  • Cos
  • Sin
  • Tan

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:

  • Acos
  • Asin
  • Atan

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 Macro

The 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 Degrees

Each of the trigonometric macros will need to:

  • accept an angle(in degrees) as an input
  • use the OpenOffice Uno to convert the angle into radians
  • call the the trigonometric functions with the OpenOffice Uno
  • return the cosine, sine or tangent of the angle to the user

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 Degrees

Each of the inverse trigonometric macros will need to:

  • accept a cosine, sine or tangent as an input
  • call the the inverse trigonometric functions with the OpenOffice Uno
  • use the OpenOffice Uno to convert the radians into degrees
  • return the angle to the user

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 Spreadsheet

The 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.

Summary

OpenOffice.org Calc has built in trigonometric functions:

  • Cos
  • Sin
  • Tan

and inverse trigonometric functions:

  • Acos
  • Asin
  • Atan

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.


Trigonometric Macros for OpenOffice Calc, Mark Alexander Bain
A User Defined Trigonometric  Function in Calc, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo