MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

Various calculations

absolute value
To calculate absolute values, use the abs function.
Note that #include <stdlib.h> is required to use the abs function.

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

abs function
 power = pow(number, exponent);

The following program is an example of obtaining absolute values.

Source Code
 #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

Execution Result
10
10

raising a number to a power
Use the pow function to compute powers.
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.

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

source code
 #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

Execution Result
5 squared = 25.000000
8 squared = 512.000000
10 to the power of 2 = 1024.000000

Square root (√)
Use the sqrt function to compute the square root.
Note that #include <math.h> is required to use the sqrt function.

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

sqrt function
 Square root = sqrt(number);

The following program is an example of finding the 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 result of executing this program is as follows

Execution Result
√100 = 10.000000 : 10.000000 * 10.000000 = 100.000000
√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
The following functions are used to compute trigonometric functions.
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


Arc-based trigonometric functions
Arc trigonometric functions are the inverse of ordinary trigonometric functions.
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

tan function
 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

Conversion from ordinary angles to radians
radian = (degrees * 3.14159 / 180)


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

Macro to convert from ordinary angles to radians
 #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.

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 result of executing this program is as follows

Execution Result
5.795493m

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.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box