MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

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.

キーワード
【absolute value】

数直線上における原点からの距離。
要するに、正の値はそのまま、負の値は正にした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:

Execution results
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.

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:

Execution results
5の2乗 = 25.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.

キーワード
【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:

Execution results
√100 = 10.000000 : 10.000000 * 10.000000 = 100.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.

function名 Trigonometric functions値
sin サイン
cos コサイン
tan タンジェント
asin アークサイン
acos アークコサイン
atan アークタンジェント


Arc trigonometric functions
Arc trigonometric functionsは、普通のTrigonometric functionsの逆計算をします。
普通のTrigonometric functionsは、角度から辺の長さの割合を求めますが、
Arc trigonometric 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.

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)


キーワード
【radians】

円弧と半径の長さが等しくなる位置を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:

Execution 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) is
This 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.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19