﻿ Various calculations - C language learned the hard way
C language learned by suffering

### Various calculations

##### absolute value
To calculate absolute values, use the abs function.
Note that #include <stdlib.h> is required to use the abs function.

Keywords.
##### Absolute value

Distance from the origin on a number line.
In short, a number with positive values left unchanged and negative values made positive.

The usage of the abs function is as follows

abs function
`` power = pow(number, exponent);``

The following program is an example of obtaining absolute values.

Source Code
`````` #include <stdio.h>
#include <stdlib.h>

void main(void)
{
printf("%d\n", abs(10));
printf("%d\n", abs(-10));

return;
}``````

The result of executing this program is as follows

Execution Result
10
10

##### raising a number to a power
Use the pow function to compute powers.
Note that #include <math.h> is required to use the pow function.

The usage of the pow function is as follows
Note that the value will be of type double, so storing it as an int will be inaccurate.

pow function
`````` #include <stdio.h>
#include <stdlib.h>

void main(void)
{
printf("%d\n", abs(10));
printf("%d\n", abs(-10));

return;
}``````

The following program is an example of finding powers.

source code
`````` #include <math.h>
#include <stdio.h>

void main(void)
{
printf("%d squared of %d = %f\n", 5, 2, pow(5, 2));
printf("%d squared of %d = %f\n", 8, 3, pow(8, 3));
printf("%d squared of %d = %f\n", 2, 10, pow(2, 10));

return;
}``````

The result of executing this program is as follows

Execution Result
5 squared = 25.000000
8 squared = 512.000000
10 to the power of 2 = 1024.000000

##### Square root (√)
Use the sqrt function to compute the square root.
Note that #include <math.h> is required to use the sqrt function.

Keywords.
##### Square root

A number that, when squared, becomes that number.
If the original number is the area of the square, the square root is the length of the sides.

The usage of the sqrt function is as follows
Note that the value will be of type double, so storing it as an int will be inaccurate.

sqrt function
`` Square root = sqrt(number);``

The following program is an example of finding the square root.

Source Code
`````` #include <math.h>
#include <stdio.h>

void main(void)
{
printf("√%d = %f : %f * %f = %f\n", 100, sqrt(100), sqrt(100), sqrt(100), sqrt(100) * sqrt(100));
printf("√%d = %f : %f * %f = %f\n", 2, sqrt(2), sqrt(2), sqrt(2), sqrt(2) * sqrt(2));

return;
}``````

The result of executing this program is as follows

Execution Result
√100 = 10.000000 : 10.000000 * 10.000000 = 100.000000
√2 = 1.414214 : 1.414214 * 1.414214 = 2.000000

It appears to be an accurate calculation because it is rounded to the nearest whole number.
Note that repeating the same calculation will result in an error.
##### trigonometric function
The following functions are used to compute trigonometric functions.
Note that #include <math.h> is required to use these functions.

Function name Trigonometric function value
sin Sine
cos Cosine
tan Tangent
asin Arcsine
acos Arc Cosine
atan Arc Tangent

Arc-based trigonometric functions
Arc trigonometric functions are the inverse of ordinary trigonometric functions.
Ordinary trigonometric functions find the ratio of the lengths of the sides from the angles.
Arc trigonometric functions find the angle from the ratio of the lengths of the sides.

Since the usage of these functions is the same for all of them, we will use the tan function as an example in the following sections.
The usage of the tan function is as follows

tan function
`` Tangent = tan(radian angle);``

However, this angle is not the 90-degree right angle we normally use.
We use a unit of angle called the radian.
You can convert from ordinary angles to radians with the following formula

Conversion from ordinary angles to radians
radian = (degrees * 3.14159 / 180)

Keywords.

A unit of angle where the position where the length of the arc and the radius are equal is one radian.
In the computer world, radians are used in most cases.

Since it is tedious to perform this calculation every time, the following macro is created.

Macro to convert from ordinary angles to radians
`` #define RADIAN(ARC) ((ARC)*3.14159 / 180)``

The next program is for a person 160 cm tall looking up at a tree from a distance of 5 m.
This is an example of finding the height of a tree if the angle was 40 degrees.

Source Code
`````` #include <math.h>
#include <stdio.h>

void main(void)
{
double stature = 160;
double distance = 500;
double arc = 40;
double tree;

tree = distance * tan(RADIAN(arc)) + stature;
printf("%fm\n", tree / 100);

return;
}``````

The result of executing this program is as follows

Execution Result
5.795493m

Note that trigonometric functions also have errors when calculations are repeated.

Comment
COMMENT

Open the 💬 comment submission box