|
|
|
How to Create C++ Trigonometric FunctionsWriting C++ Code to Convert from Degrees to Radians
Trigonometry is easy: especially when used with C++; and even more so when functions are added then enable the maths to be done with degrees rather than radians.
C++ handles trigonometry very easily - all the programmer has to do is to load the correct header (math.h) and start using the trigonometric functions (cos, sin and tan). However, as often is the case, the C++ trigonometric functions work with radians and not degrees; that may not be an issue, but if it is then the solution is simple - just write a set of trigonometric functions that work with degrees instead of radians. There is, of course, no need to rewrite the C++ trigonometric functions - it is just a case of writing wrappers for each of the existing functions that will:
A similar process will also be needed for the inverse trigonometric functions:
Converting from Degrees to Radians and Vice VersaThe only completely new functionality that the programmer needs introduce is the conversion from degrees to radians and from radians to degrees; however, the calculation is very simple: radians = degrees x Π / 180
degrees = radians x 180 / Π
The first job (as always) when programming functions is to create a header file (for example trig.h): #ifndef TRIG_H
#define TRIG_H
#include <iostream.h>
#include <math.h>
double radians (double d);
double degrees (double r);
#endif
Of course only the function declarations are placed in the header file - the code for the actual functionality is placed in it's own file (for example trig.c): #include "trig.h"
double radians (double d) {
return d * M_PI / 180;
}
double degrees (double r) {
return r * 180/ M_PI;
}
With the degree-radian-degree conversion in place the programmer can turn to the trigonometric functions themselves. Trigonometric FunctionsThe user defined trigonometric functions simply take an input in degrees, convert the value into radians and then return the results of the C++ trigonometric functions, and again the first step is to add function declarations to the header file: double cosine (double d);
double sine (double d);
double tangent (double d);
And then to write the code for the functions into the source file: double cosine (double d) {
return cos(radians(d));
}
double sine (double d) {
return sin(radians(d));
}
double tangent (double d) {
return tan(radians(d));
}
Inverse Trigonometric FunctionsThe user defined inverse trigonometric functions convert the cosine, sine or tangent into degrees - and again must start with a declaration in the header file: double arccosine (double c);
double arcsine (double s);
double arctangent (double t);
And the actual coding in the source file: double arccosine (double c) {
return degrees(acos(c));
}
double arcsine (double s) {
return degrees(asin(s));
}
double arctangent (double t) {
return degrees(atan(t));
}
Testing the Functions: a Trigonometric Program for Calculating the Sine of an AngleWith the declarations in a header files the functions can be called from other programs for example one to calculate the sine of a angle on the command line, for example sine.c: #include "trig.h"
int main (int argc,char *argv[]) {
cout << sine ( atof( argv[1] )) << "\n";
return 0;
}
Once the program has been compiled then it will produce an output something like: $ sine 30
0.5
SummaryC++ has its own trigonometric functions and inverse trigonometric functions but these only work in radians - user defined functions are required in order for the trigonometric functions to accept degrees as inputs. The functions need to be declared in a header file so they can be called from any further C++ programs that are written.
The copyright of the article How to Create C++ Trigonometric Functions in C Programming is owned by Mark Alexander Bain. Permission to republish How to Create C++ Trigonometric Functions in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|