Mathematical functions are like the “magic buttons” of computers, helping to quickly perform complex calculations. However, to truly understand these functions, one must first learn the underlying mathematical concepts, then relate them to real-life scenarios, and finally examine their implementation in C++.
1. fabs — Absolute Value
Mathematical Concept
The absolute value represents the distance of a number from 0.
-
|-5| = 5, because -5 is 5 units away from 0;
-
|8| = 8, because 8 is 8 units away from 0.
Real-Life Analogy
On a thermometer, -5°C and +5°C have the same temperature difference, even though they are in opposite directions.
C++ Code
cout << fabs(-5) << endl; // Outputs 5
cout << fabs(8) << endl; // Outputs 8
2. pow and sqrt — Square and Square Root
Mathematical Concept
-
Square pow(a, b): a raised to the power of b, meaning “a multiplied by itself b times”.
-
Example: pow(2, 3) = 2 × 2 × 2 = 8.
-
Square Root sqrt(x): asking “which number multiplied by itself equals x”.
-
Example: sqrt(9) = 3, because 3 × 3 = 9.
Real-Life Analogy
Square:
-
Side length 3 → Area 9 (square).
-
Area 9 → Side length 3 (square root).
C++ Code
cout << pow(2, 3) << endl; // Outputs 8
cout << sqrt(9) << endl; // Outputs 3
3. round / floor / ceil — Rounding and Integer Conversion
Mathematical Concept
-
round(x): rounds to the nearest integer, rounding up if ≥5.
-
floor(x): rounds down to the nearest lower integer.
-
ceil(x): rounds up to the nearest higher integer.
Real-Life Analogy
Elevator:
-
round = determining which floor (3.6 floors ≈ 4 floors).
-
floor = going down to the nearest floor (3.9 floors → 3 floors).
-
ceil = going up to the nearest floor (3.1 floors → 4 floors).
C++ Code
cout << round(3.6) << endl; // Outputs 4
cout << floor(3.9) << endl; // Outputs 3
cout << ceil(3.1) << endl; // Outputs 4
4. sin and cos — Sine and Cosine
Mathematical Concept
-
In a right triangle:
-
sin(θ) = opposite side ÷ hypotenuse
-
cos(θ) = adjacent side ÷ hypotenuse
In the unit circle:
-
cos(θ) = x-coordinate of the point
-
sin(θ) = y-coordinate of the point
Real-Life Analogy
-
Ferris Wheel: the height of the car changes = sin, the horizontal position changes = cos.
-
Clock Minute Hand: as the minute hand rotates, vertical direction = sin, horizontal direction = cos.
C++ Code
(Note: angles must be converted to radians, 90° = π/2)
double pi = 3.1415926;
cout << sin(pi/2) << endl; // Outputs 1
cout << cos(pi/3) << endl; // Outputs 0.5
5. exp — Exponential Function
Mathematical Concept
exp(x) = e raised to the power of x, where e ≈ 2.71828.
-
exp(1) ≈ 2.718
-
exp(2) ≈ 7.389
Represents “rapid growth”.
Real-Life Analogy
-
Saving money with interest: money grows exponentially.
-
Bacterial reproduction: starting with a few, then multiplying to thousands in no time.
C++ Code
cout << exp(1) << endl; // Approximately 2.718
cout << exp(2) << endl; // Approximately 7.389
6. log — Logarithmic Function
Mathematical Concept
log(y) = asking “to what power is e raised to equal y”.
-
log(e) = 1, because e^1 = e.
-
log(exp(2)) = 2, because e^2 = exp(2).
Real-Life Analogy
If exp is the “accelerator”, log is the “reverse gear”.
-
exp: How much has the money grown?
-
log: What was the original amount that led to this growth?
C++ Code
cout << log(exp(2)) << endl; // Outputs 2
cout << log(2.71828) << endl; // Approximately 1
-
fabs: Absolute value = distance from 0
-
pow / sqrt: Square and square root = area and side length of a square
-
round / floor / ceil: Methods of rounding = going up and down in an elevator
-
sin / cos: Angle functions = height and horizontal position of a Ferris wheel/clock
-
exp: Exponential function = rapid growth (interest, reproduction)
-
log: Logarithmic function = reverse of exponentiation (inverse operation of growth)
These functions are not just for solving math problems; they can also be applied in game development, graphics rendering, scientific simulations, and more. Understanding the mathematics and then applying it in code allows one to truly experience the “mathematical magic” in programming.
Wild Bison Programmer teaches children programming and informatics Olympiad
Yibin Wild Bison Network Technology Co., Ltd. specializes in WeChat mini-program development, website construction, software development, etc.


