How to Create C++ Trigonometric Functions in Ultimate++

in #utopian-io7 years ago (edited)

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 in Ultimate++ 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:

  • allow the user to enter a value in degrees
  • convert the degrees into radians
  • use the existing trigonometric functions to return the cosine, sine or tangent to the user

A similar process will also be needed for the inverse trigonometric functions:

  • allow the user to enter a cosine, sine or tangent
  • use the existing inverse trigonometric functions to obtain the result in radians
  • convert the radians into degrees
  • return value in degrees to the user

image.png

Converting from Degrees to Radians and Vice Versa

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

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

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

With 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

Summary

C++ 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.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @dorodor I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.19
TRX 0.16
JST 0.032
BTC 63891.49
ETH 2753.67
USDT 1.00
SBD 2.66