create account

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

View this thread on: hive.blogpeakd.comecency.com
· @dorodor · (edited)
$30.62
How to Create C++ Trigonometric Functions in Ultimate++
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++](https://www.ultimatepp.org/) 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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514847061/gi3pyhxolpiony2jndbe.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.

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@dorodor/how-to-create-c-trigonometric-functions-in-ultimate">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , ,
properties (23)
authordorodor
permlinkhow-to-create-c-trigonometric-functions-in-ultimate
categoryutopian-io
json_metadata"{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"score":79.76844,"default_branch":"master","watchers":60,"open_issues":0,"forks":19,"license":null,"open_issues_count":0,"archived":false,"mirror_url":null,"forks_count":19,"has_pages":false,"has_wiki":true,"has_downloads":true,"has_projects":true,"has_issues":true,"language":"C++","watchers_count":60,"stargazers_count":60,"size":302882,"homepage":null,"svn_url":"https://github.com/ultimatepp/mirror","clone_url":"https://github.com/ultimatepp/mirror.git","ssh_url":"git@github.com:ultimatepp/mirror.git","git_url":"git://github.com/ultimatepp/mirror.git","pushed_at":"2017-12-31T17:05:08Z","updated_at":"2017-12-24T03:46:02Z","created_at":"2015-10-05T13:02:56Z","deployments_url":"https://api.github.com/repos/ultimatepp/mirror/deployments","releases_url":"https://api.github.com/repos/ultimatepp/mirror/releases{/id}","labels_url":"https://api.github.com/repos/ultimatepp/mirror/labels{/name}","notifications_url":"https://api.github.com/repos/ultimatepp/mirror/notifications{?since,all,participating}","milestones_url":"https://api.github.com/repos/ultimatepp/mirror/milestones{/number}","pulls_url":"https://api.github.com/repos/ultimatepp/mirror/pulls{/number}","issues_url":"https://api.github.com/repos/ultimatepp/mirror/issues{/number}","downloads_url":"https://api.github.com/repos/ultimatepp/mirror/downloads","archive_url":"https://api.github.com/repos/ultimatepp/mirror/{archive_format}{/ref}","merges_url":"https://api.github.com/repos/ultimatepp/mirror/merges","compare_url":"https://api.github.com/repos/ultimatepp/mirror/compare/{base}...{head}","contents_url":"https://api.github.com/repos/ultimatepp/mirror/contents/{+path}","issue_comment_url":"https://api.github.com/repos/ultimatepp/mirror/issues/comments{/number}","comments_url":"https://api.github.com/repos/ultimatepp/mirror/comments{/number}","git_commits_url":"https://api.github.com/repos/ultimatepp/mirror/git/commits{/sha}","commits_url":"https://api.github.com/repos/ultimatepp/mirror/commits{/sha}","subscription_url":"https://api.github.com/repos/ultimatepp/mirror/subscription","subscribers_url":"https://api.github.com/repos/ultimatepp/mirror/subscribers","contributors_url":"https://api.github.com/repos/ultimatepp/mirror/contributors","stargazers_url":"https://api.github.com/repos/ultimatepp/mirror/stargazers","languages_url":"https://api.github.com/repos/ultimatepp/mirror/languages","statuses_url":"https://api.github.com/repos/ultimatepp/mirror/statuses/{sha}","trees_url":"https://api.github.com/repos/ultimatepp/mirror/git/trees{/sha}","git_refs_url":"https://api.github.com/repos/ultimatepp/mirror/git/refs{/sha}","git_tags_url":"https://api.github.com/repos/ultimatepp/mirror/git/tags{/sha}","blobs_url":"https://api.github.com/repos/ultimatepp/mirror/git/blobs{/sha}","tags_url":"https://api.github.com/repos/ultimatepp/mirror/tags","branches_url":"https://api.github.com/repos/ultimatepp/mirror/branches{/branch}","assignees_url":"https://api.github.com/repos/ultimatepp/mirror/assignees{/user}","events_url":"https://api.github.com/repos/ultimatepp/mirror/events","issue_events_url":"https://api.github.com/repos/ultimatepp/mirror/issues/events{/number}","hooks_url":"https://api.github.com/repos/ultimatepp/mirror/hooks","teams_url":"https://api.github.com/repos/ultimatepp/mirror/teams","collaborators_url":"https://api.github.com/repos/ultimatepp/mirror/collaborators{/collaborator}","keys_url":"https://api.github.com/repos/ultimatepp/mirror/keys{/key_id}","forks_url":"https://api.github.com/repos/ultimatepp/mirror/forks","url":"https://api.github.com/repos/ultimatepp/mirror","fork":false,"description":"Official Ultimate++ mirror","html_url":"https://github.com/ultimatepp/mirror","private":false,"owner":{"site_admin":false,"type":"Organization","received_events_url":"https://api.github.com/users/ultimatepp/received_events","events_url":"https://api.github.com/users/ultimatepp/events{/privacy}","repos_url":"https://api.github.com/users/ultimatepp/repos","organizations_url":"https://api.github.com/users/ultimatepp/orgs","subscriptions_url":"https://api.github.com/users/ultimatepp/subscriptions","starred_url":"https://api.github.com/users/ultimatepp/starred{/owner}{/repo}","gists_url":"https://api.github.com/users/ultimatepp/gists{/gist_id}","following_url":"https://api.github.com/users/ultimatepp/following{/other_user}","followers_url":"https://api.github.com/users/ultimatepp/followers","html_url":"https://github.com/ultimatepp","url":"https://api.github.com/users/ultimatepp","gravatar_id":"","avatar_url":"https://avatars1.githubusercontent.com/u/6873503?v=4","id":6873503,"login":"ultimatepp"},"full_name":"ultimatepp/mirror","name":"mirror","id":43684525},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","opensource","programming","howto","tutorial"],"users":["dorodor"],"links":["https://www.ultimatepp.org/","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514847061/gi3pyhxolpiony2jndbe.png"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1514847061/gi3pyhxolpiony2jndbe.png"],"moderator":{"account":"manishmike10","reviewed":true,"pending":false,"flagged":false}}"
created2018-01-01 22:46:57
last_update2018-01-07 00:44:03
depth0
children2
last_payout2018-01-08 22:46:57
cashout_time1969-12-31 23:59:59
total_payout_value21.244 HBD
curator_payout_value9.372 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,449
author_reputation4,393,192,478,685
root_title"How to Create C++ Trigonometric Functions in Ultimate++"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,446,888
net_rshares3,124,226,866,602
author_curate_reward""
vote details (9)
@manishmike10 ·
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/UCvqCsx).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authormanishmike10
permlinkre-dorodor-how-to-create-c-trigonometric-functions-in-ultimate-20180103t053742065z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-03 05:37:42
last_update2018-01-03 05:37:42
depth1
children0
last_payout2018-01-10 05:37:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation20,399,732,899,016
root_title"How to Create C++ Trigonometric Functions in Ultimate++"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,704,484
net_rshares0
@utopian-io ·
### 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. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**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**
properties (22)
authorutopian-io
permlinkre-dorodor-how-to-create-c-trigonometric-functions-in-ultimate-20180104t070709693z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-04 07:07:09
last_update2018-01-04 07:07:09
depth1
children0
last_payout2018-01-11 07:07:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,505
author_reputation152,955,367,999,756
root_title"How to Create C++ Trigonometric Functions in Ultimate++"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,966,657
net_rshares0