Various calculations
absolute value
Note that #include <stdlib.h> is required to use the abs function.
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
power = pow(number, exponent);
The following program is an example of obtaining absolute values.
#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
10
raising a number to a power
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.
#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.
#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
8 squared = 512.000000
10 to the power of 2 = 1024.000000
Square root (√)
Note that #include <math.h> is required to use the sqrt function.
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.
Square root = sqrt(number);
The following program is an example of finding the square root.
#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
√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
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 |
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
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
Radian
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.
#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.
#include <math.h>
#include <stdio.h>
#define RADIAN(ARC) ((ARC)*3.14159 / 180)
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
Note that trigonometric functions also have errors when calculations are repeated.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.