learn through suffering C language learn through suffering 
C 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.

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

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

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

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 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) 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. line break
  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

Loading comment system...