C++ Math

Mathematical functions and calculations in C++

🧮 What is C++ Math?

C++ provides built-in mathematical functions through the cmath library. These functions perform complex calculations like trigonometry, logarithms, powers, and rounding operations efficiently.


#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double num = 16.7;
    cout << "Square root: " << sqrt(num) << endl;
    cout << "Rounded: " << round(num) << endl;
    return 0;
}
                                    

Output:

Square root: 4.08656
Rounded: 17

C++ Math Functions

📐

Basic Functions

Essential mathematical operations

sqrt(25);    // 5
pow(2, 3);   // 8
abs(-10);    // 10
🔄

Rounding

Round numbers up, down, or nearest

ceil(4.2);   // 5
floor(4.8);  // 4
round(4.5);  // 5
📊

Min/Max

Find minimum and maximum values

min(5, 10);  // 5
max(5, 10);  // 10
fmax(3.2, 2.8); // 3.2
🌊

Trigonometry

Sine, cosine, and tangent functions

sin(1.57);   // ~1
cos(0);      // 1
tan(0.785);  // ~1

🔹 Basic Math Functions

Basic Math Functions provide essential mathematical operations for computational tasks, including absolute value, exponentiation, and roots. Functions like abs(-15.7) return 15.7, pow(4, 2) computes 16, and sqrt(25) yields 5. These operations are foundational for algorithms, physics simulations, and data analysis, ensuring accurate numerical processing. By implementing or using these functions, developers can handle common mathematical challenges efficiently, making them indispensable in scientific computing, game development, and engineering applications.

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double x = 25.0;
    double y = -7.5;
    
    cout << "Square root of " << x << ": " << sqrt(x) << endl;
    cout << "Power 2^3: " << pow(2, 3) << endl;
    cout << "Absolute value of " << y << ": " << abs(y) << endl;
    cout << "Logarithm of " << x << ": " << log(x) << endl;
    cout << "Log base 10 of " << x << ": " << log10(x) << endl;
    
    return 0;
}

Output:

Square root of 25: 5
Power 2^3: 8
Absolute value of -7.5: 7.5
Logarithm of 25: 3.21888
Log base 10 of 25: 1.39794

🔹 Rounding Functions

Rounding Functions adjust floating-point numbers to integers or specified decimals using methods like floor, ceil, and round. For instance, floor(4.7) returns 4, ceil(4.2) gives 5, and round(4.5) results in 4 (following round-half-to-even). These functions are critical for financial calculations, data discretization, and UI displays where precision control is needed. They help avoid floating-point errors, ensure consistent formatting, and are widely used in statistics, billing systems, and graphical rendering.

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double num1 = 4.3;
    double num2 = 4.7;
    double num3 = 4.5;
    
    cout << "Original numbers: " << num1 << ", " << num2 << ", " << num3 << endl;
    
    cout << "ceil(): " << ceil(num1) << ", " << ceil(num2) << ", " << ceil(num3) << endl;
    cout << "floor(): " << floor(num1) << ", " << floor(num2) << ", " << floor(num3) << endl;
    cout << "round(): " << round(num1) << ", " << round(num2) << ", " << round(num3) << endl;
    
    return 0;
}

Output:

Original numbers: 4.3, 4.7, 4.5
ceil(): 5, 5, 5
floor(): 4, 4, 4
round(): 4, 5, 5

🔹 Min and Max Functions

The min() and max() functions return the smallest and largest values from a given set of arguments, respectively. They are essential for data analysis, boundary checking, and algorithm optimization. For instance, they can find the highest score in a game, the lowest temperature in a dataset, or enforce limits within a simulation, providing a concise and error-free alternative to manual comparisons.

#include <iostream>
#include <algorithm>  // for min/max
#include <cmath>      // for fmin/fmax
using namespace std;

int main() {
    int a = 15, b = 25;
    double x = 3.14, y = 2.71;
    
    cout << "min(" << a << ", " << b << "): " << min(a, b) << endl;
    cout << "max(" << a << ", " << b << "): " << max(a, b) << endl;
    
    cout << "fmin(" << x << ", " << y << "): " << fmin(x, y) << endl;
    cout << "fmax(" << x << ", " << y << "): " << fmax(x, y) << endl;
    
    return 0;
}

Output:

min(15, 25): 15
max(15, 25): 25
fmin(3.14, 2.71): 2.71
fmax(3.14, 2.71): 3.14

🧠 Test Your Knowledge

Which header file is needed for math functions like sqrt() and pow()?