C++ provides a wide range of math functions to perform calculations efficiently.
1. Basic Arithmetic Operations
C++ supports standard arithmetic operations such as addition, subtraction, multiplication, and division.
Code Example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << "Addition: " << a + b << endl;
cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl; // Integer division
cout << "Modulus: " << a % b << endl; // Remainder
return 0;
}
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
Explanation:
- Division: Since a and b are integers, the division result is truncated to an integer.
- Modulus: The % operator gives the remainder of the division (10 divided by 3 leaves a remainder of 1).
Explanation:
- Division: Since a and b are integers, the division result is truncated to an integer.
- Modulus: The % operator gives the remainder of the division (10 divided by 3 leaves a remainder of 1).
2. Trigonometric Functions
Trigonometric functions like sin, cos, and tan are used for angle calculations. M_PI (π) is a predefined constant in <cmath>.
Code Example:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double angle = M_PI / 6; // 30 degrees in radians
cout << "sin(30°): " << sin(angle) << endl;
cout << "cos(30°): " << cos(angle) << endl;
cout << "tan(30°): " << tan(angle) << endl;
return 0;
}
Output:
sin(30°): 0.5
cos(30°): 0.866025
tan(30°): 0.57735
Explanation:
- Input: The angle is given in radians (not degrees). M_PI/6 corresponds to 30°.
- Outputs: These are the standard trigonometric values for 30°:
- sin(30°) = 0.5
- cos(30°) ≈ 0.866025
- tan(30°) ≈ 0.57735
3. Explanation of M_PI
M_PI is a predefined constant in <cmath> that represents the value of π (pi) in mathematics. Its approximate value is 3.141592653589793. If M_PI is not available in some environments, you can define it manually:
#define M_PI 3.141592653589793
4. Square Root and Power
The sqrt function calculates the square root, and pow calculates the power of a number.
Code Example:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Square root of 25: " << sqrt(25) << endl;
cout << "2^5: " << pow(2, 5) << endl;
return 0;
}
Output:
Square root of 25: 5
2^5: 32
Explanation:
- Square root: sqrt(25) returns 5 because 5×5=25.
- Power: pow(2, 5) returns 32 because 2^5 =2×2×2×2×2.
5. Rounding Functions
Functions like ceil, floor, round, and trunc handle decimal numbers differently.
Code Example:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num = 7.65;
cout << "Ceil: " << ceil(num) << endl; // Rounds up
cout << "Floor: " << floor(num) << endl; // Rounds down
cout << "Round: " << round(num) << endl; // Nearest integer
cout << "Truncate: " << trunc(num) << endl; // Removes fractional part
return 0;
}
Output:
Ceil: 8
Floor: 7
Round: 8
Truncate: 7
Explanation:
- ceil: Always rounds up (7.65 → 8).
- floor: Always rounds down (7.65 → 7).
- round: Rounds to the nearest integer (7.65 → 8).
- trunc: Removes the fractional part without rounding (7.65 → 7).
6. Min and Max
The fmin and fmax functions find the smallest and largest of two numbers.
Code Example:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = 5.7, y = 8.2;
cout << "Minimum: " << fmin(x, y) << endl;
cout << "Maximum: " << fmax(x, y) << endl;
return 0;
}
Output:
Minimum: 5.7
Maximum: 8.2
Explanation:
- fmin: Returns the smaller value.
- fmax: Returns the larger value.
7. Modulus and Remainder
The fmod function calculates the floating-point remainder, while remainder gives the remainder closest to zero.
Code Example:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num1 = 8.75, num2 = 3.4;
cout << "fmod(8.75, 3.4): " << fmod(num1, num2) << endl;
cout << "remainder(8.75, 3.4): " << remainder(num1, num2) << endl;
return 0;
}
Output:
fmod(8.75, 3.4): 1.95
remainder(8.75, 3.4): -1.45
Explanation:
- fmod: Computes the remainder after division (8.75 - (2 * 3.4) = 1.95).
- remainder: Returns the remainder closest to zero (-1.45). remainder(x,y)=x−(nearest multiple of y)
8. Infinity and NaN
INFINITY represents positive infinity, and NaN (Not a Number) represents undefined results.
Code Example:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Positive Infinity: " << INFINITY << endl;
cout << "Negative Infinity: " << -INFINITY << endl;
double invalid = 0.0 / 0.0;
cout << "NaN: " << invalid << endl;
return 0;
}
Output:
Positive Infinity: inf
Negative Infinity: -inf
NaN: nan
Explanation:
- INFINITY: Represents values that exceed the range of floating-point numbers.
- NaN: Results from undefined operations (e.g., dividing 0 by 0).
Conclusion
The functions we covered in this tutorial (like sqrt, pow, fmod, remainder, ceil, floor, etc.) are all part of the C++ Math Library provided in the <cmath> header. These functions help solve common mathematical problems efficiently.
However, the <cmath> library includes many other powerful functions that were not covered here, such as:
- Hyperbolic functions: sinh, cosh, tanh, etc.
- Exponential and logarithmic functions: exp, log, log10, etc.
- Angle conversion: degrees, radians (available in C++20).
- More rounding functions: nearbyint, copysign, etc.
For a complete list of all mathematical functions available in <cmath>, you can refer to the official documentation:
'C++ Beginner' 카테고리의 다른 글
cpp_006_Introduction to getline and cin.getline in C++: Handling User Input with Spaces (0) | 2025.01.05 |
---|---|
cpp_005_Introduction to Strings (5) | 2025.01.01 |
cpp_003_climits_and_sizeof_in_C++ (0) | 2024.12.25 |
cpp_002_if Statement in C++ (0) | 2024.12.25 |
cpp_001_Introduction to Functions (1) | 2024.12.23 |