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.
Distance from the origin on the number line. In short, positive values remain unchanged, while negative values are converted to positive values.
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】
Distance from the origin on the number line. In short, positive values remain unchanged, while negative values are converted to positive values.
The following is how to use the abs function.
abs function
Power = pow(number, exponent);
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 to the power of %d = %f\n", 5, 2, pow(5, 2));
printf("%d to the power of %d = %f\n", 8, 3, pow(8, 3));
printf("%d to the power of %d = %f\n", 2, 10, pow(2, 10));
return;
}
The output of this program is as follows.
Results
5 squared = 25.000000 8 cubed = 512.000000 2 to the power of 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.
A number that, when squared, becomes that number.
If the original number is the area of a square, the square root corresponds to the length of its side.
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】
A number that, when squared, becomes that number.
If the original number is the area of a square, the square root corresponds to the length of its side.
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.
An angle unit where one radian corresponds to the position where the arc length equals the radius length.
In computing, radians are used in almost all cases.
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 name | Trigonometric values |
|---|---|
| sin | sine |
| cos | cosine |
| tan | tangent |
| asin | arc sine |
| acos | arc cosine |
| atan | arc tangent |
Arc trigonometric functions
Arc trigonometric functions perform the inverse calculation of standard trigonometric functions. Standard trigonometric functions determine the ratio of side lengths from an angle, while arc trigonometric functions determine the angle from the ratio of side lengths.
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
tangent = tan(radian angle);
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】
An angle unit where one radian corresponds to the position where the arc length equals the radius length.
In computing, radians are used in almost all cases.
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.




