various calculations
absolute value
Use the abs function to calculate the absolute value.
Please note that you need to include `` to use the `abs` function.
数直Line上における原点Fromの距離。
要するに、正の値はそのまま、負の値は正にしたnumerics。
The following is how to use the abs function.
The following program is an example of calculating absolute value.
The output of this program is as follows.
Please note that you need to include `
Keyword
【absolute value】
数直Line上における原点Fromの距離。
要するに、正の値はそのまま、負の値は正にしたnumerics。
The following is how to use the abs function.
abs function
exponent = pow(numerics, 指数);
The following program is an example of calculating absolute value.
Source code
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
printf("%d\n", abs(10));
printf("%d\n", abs(-10));
return;
}
The output of this program is as follows.
Results
10
10
10
exponent
To calculate exponents, use the pow function.
Please note that you need to include `` to use the pow function.
The pow function is used as follows:
Please note that the value will be a double type, so storing it as an integer will result in inaccuracy.
The following program is an example of calculating exponents.
The output of this program is as follows.
Please note that you need to include `
The pow function is used as follows:
Please note that the value will be a double type, so storing it as an integer will result in inaccuracy.
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 calculating exponents.
Source code
#include <math.h>
#include <stdio.h>
void main(void)
{
printf("%dの%d乗 = %f\n", 5, 2, pow(5, 2));
printf("%dの%d乗 = %f\n", 8, 3, pow(8, 3));
printf("%dの%d乗 = %f\n", 2, 10, pow(2, 10));
return;
}
The output of this program is as follows.
Results
5の2乗 = 25.000000
8の3乗 = 512.000000
2の10乗 = 1024.000000
8の3乗 = 512.000000
2の10乗 = 1024.000000
square root (√)
To calculate a square root, use the sqrt function.
Please note that you need to include `` to use the sqrt function.
2乗するとその数になるnumerics。
元のnumericsを正方形の面積とすると、square rootは辺の長さに当たる。
The following is how to use the sqrt function.
Please note that the value will be a double type, so storing it as an integer will result in inaccuracy.
The following program is an example of calculating a square root.
The output of this program is as follows.
It looks like the calculations are accurate because of rounding.
Caution is needed because repeating similar calculations will result in error.
Please note that you need to include `
Keyword
【square root】
2乗するとその数になるnumerics。
元のnumericsを正方形の面積とすると、square rootは辺の長さに当たる。
The following is how to use the sqrt function.
Please note that the value will be a double type, so storing it as an integer will result in inaccuracy.
sqrt function
square root = sqrt(numerics);
The following program is an example of calculating a 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 output of this program is as follows.
Results
√100 = 10.000000 : 10.000000 * 10.000000 = 100.000000
√2 = 1.414214 : 1.414214 * 1.414214 = 2.000000
√2 = 1.414214 : 1.414214 * 1.414214 = 2.000000
It looks like the calculations are accurate because of rounding.
Caution is needed because repeating similar calculations will result in error.
Trigonometric functions
Use the following functions to calculate trigonometric functions.
Note that you need to include <math.h> to use these functions.
Since all of these functions work in the same way, I will use the tan function as an example from now on.
Here's how to use the tan function.
However, this angle is not the 90 degrees that we typically use as a right angle.
We use a unit of angle called radians.
You can convert from degrees to radians using the following formula.
Circle弧と半径の長さが等しくなる位置を1radiansとする角度の単位。
コンピュータの世界ではほとんどの場合にradiansを使用する。
It's tedious to perform this calculation every time, so I'm creating a macro like this.
The following program calculates the angle of elevation when a person with a height of 160cm looks up at a tree from a distance of 5 meters.
Here's an example of finding the height of the tree when the angle is 40 degrees.
The output of this program is as follows.
Because trigonometric function calculations also produce errors if repeated, caution is needed.
Note that you need to include <math.h> to use these functions.
| function名 | Trigonometric functions値 |
|---|---|
| sin | サイン |
| cos | コサイン |
| tan | タンジェント |
| asin | アークサイン |
| acos | アークコサイン |
| atan | アークタンジェント |
Arc trigonometric functions
Arc trigonometric functionsは、普通のTrigonometric functionsの逆計算をします。
普通のTrigonometric functionsは、角度From辺の長さの割合を求めますが、
Arc trigonometric functionsは、辺の長さの割合From角度を求めます。
普通のTrigonometric functionsは、角度From辺の長さの割合を求めますが、
Arc trigonometric functionsは、辺の長さの割合From角度を求めます。
Since all of these functions work in the same way, I will use the tan function as an example from now on.
Here's how to use the tan function.
tan function
タンジェント = tan(radians角度);
However, this angle is not the 90 degrees that we typically use as a right angle.
We use a unit of angle called radians.
You can convert from degrees to radians using the following formula.
Converting from degrees to radians
Radians = (Degrees * 3.14159 / 180)
Keyword
【radians】
Circle弧と半径の長さが等しくなる位置を1radiansとする角度の単位。
コンピュータの世界ではほとんどの場合にradiansを使用する。
It's tedious to perform this calculation every time, so I'm creating a macro like this.
A macro to convert from degrees to radians.
#define RADIAN(ARC) ((ARC)*3.14159 / 180)
The following program calculates the angle of elevation when a person with a height of 160cm looks up at a tree from a distance of 5 meters.
Here's an example of finding the height of the tree when the angle is 40 degrees.
Source code
#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 output of this program is as follows.
Results
5.795493m
Because trigonometric function calculations also produce errors if repeated, caution is needed.
About This Site
Learning C language through suffering (Kushi C) isThis is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.




