C++ Math Operations
Introduction to C++ Math
C++ provides a wide range of mathematical operations using both built-in operators and the
The
Basic Arithmetic Operators
Operator | Operation | Example | Result |
---|---|---|---|
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Division | 5.0 / 2 | 2.5 |
% | Modulus (remainder) | 5 % 2 | 1 |
++ | Increment | x++ (x starts 5) | x becomes 6 |
-- | Decrement | x-- (x starts 5) | x becomes 4 |
Common Math Functions
The
Function | Description | Example | Result |
---|---|---|---|
sqrt(x) | Square root | sqrt(25) | 5 |
pow(x,y) | x raised to the power y | pow(2,3) | 8 |
abs(x) | Absolute value | abs(-5) | 5 |
ceil(x) | Round upward | ceil(3.2) | 4 |
floor(x) | Round downward | floor(3.8) | 3 |
round(x) | Round to nearest | round(3.5) | 4 |
fmod(x,y) | Floating-point remainder | fmod(5.5,2) | 1.5 |
Trigonometric Functions
C++ includes standard trigonometric functions that use radians:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
constexpr double PI = 3.141592653589793;
double angle = 45.0;
double radians = angle * PI / 180.0;
cout << "sin(45°): " << sin(radians) << endl;
cout << "cos(45°): " << cos(radians) << endl;
cout << "tan(45°): " << tan(radians) << endl;
return 0;
}
sin(45°): 0.707107 cos(45°): 0.707107 tan(45°): 1
Exponential and Logarithmic Functions
For exponents and logarithms,
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "exp(1): " << exp(1) << endl; // e^1
cout << "log(10): " << log(10) << endl; // Natural logarithm
cout << "log10(100): " << log10(100) << endl; // Base-10 logarithm
return 0;
}
exp(1): 2.71828 log(10): 2.30259 log10(100): 2
Random Number Generation
Modern C++ (C++11 and later) provides robust random number facilities in
#include <iostream>
#include <random>
using namespace std;
int main() {
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dist(1, 6);
cout << "Dice roll: " << dist(gen) << endl;
uniform_real_distribution<> float_dist(0.0, 1.0);
cout << "Random float: " << float_dist(gen);
return 0;
}
Mathematical Constants
C++20 introduced standardized mathematical constants in
// C++20 (preferred)
#include <iostream>
#include <numbers>
using namespace std;
int main() {
cout << "\xCF\x80: " << std::numbers::pi << endl; // pi
cout << "e: " << std::numbers::e << endl; // Euler's number
// Pre-C++20 fallback
constexpr double PI = 3.141592653589793;
constexpr double E = 2.718281828459045;
cout << "pi (fallback): " << PI << "\n";
cout << "e (fallback): " << E << "\n";
return 0;
}
π: 3.14159 e: 2.71828
Practical Math Examples
Examples of math in everyday programs:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// Circle area
constexpr double PI = 3.141592653589793;
double radius = 5.0;
double area = PI * pow(radius, 2);
cout << "Circle area: " << area << endl;
// Compound interest
double principal = 1000.0;
double rate = 0.05;
int years = 10;
double amount = principal * pow(1 + rate, years);
cout << "Investment after " << years << " years: " << amount;
return 0;
}
Circle area: 78.5398 Investment after 10 years: 1628.89
Best Practices
- Prefer double over float for greater precision
- Be aware of floating-point rounding errors
- Use parentheses to clarify complex expressions
- For advanced needs, consider libraries like Boost.Math
- Always initialize variables before using them in calculations