πŸ› οΈ C++ Tools & Ecosystem
Estimated reading: 4 minutes 275 views

C++ <cmath> – Perform Mathematical Operations with Standard Functions


Introduction – Why Use <cmath> in C++

C++ provides a rich set of mathematical functions for scientific and numeric computing via the <cmath> header. Whether you’re calculating powers, roots, trigonometric values, or logarithms, <cmath> offers reliable and optimized functions for accurate computation.

In this guide, you’ll learn:

  • What functions <cmath> provides
  • Categories: arithmetic, trigonometric, exponential, rounding
  • Examples of common math functions
  • Best practices for precision and performance

What Is <cmath> in C++?

<cmath> is the C++ header that provides standard math functions, originally from the C library (<math.h>). It includes:

  • Basic arithmetic
  • Trigonometry
  • Exponential/logarithmic functions
  • Rounding and remainder operations

Include it with:

#include <cmath>

Functions are available in the std namespace.


Common <cmath> Functions

Power and Root

FunctionDescriptionExample
pow(x, y)x raised to the power ypow(2, 3) β†’ 8
sqrt(x)Square rootsqrt(9) β†’ 3
cbrt(x)Cube root (C++11)cbrt(8) β†’ 2

Trigonometric Functions

FunctionDescription
sin(x)Sine (x in radians)
cos(x)Cosine (x in radians)
tan(x)Tangent
asin(x)Arc sine
acos(x)Arc cosine
atan(x)Arc tangent
atan2(y, x)Arc tangent of y/x considering signs

Exponential and Logarithmic

FunctionDescription
exp(x)e raised to the power x
log(x)Natural log (base e)
log10(x)Log base 10
log2(x)Log base 2 (C++11)

Rounding Functions

FunctionDescription
floor(x)Largest integer ≀ x
ceil(x)Smallest integer β‰₯ x
round(x)Nearest integer (rounds .5 away)
trunc(x)Truncates decimal (C++11)
fabs(x)Absolute value (floating-point)
fmod(x, y)Floating-point remainder of x/y

Code Examples – With Output

Example: Basic Math

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

int main() {
    double a = 9.0;
    double b = 2.0;
    cout << "sqrt(a): " << sqrt(a) << endl;
    cout << "pow(a, b): " << pow(a, b) << endl;
    cout << "log(a): " << log(a) << endl;
    cout << "sin(PI/2): " << sin(3.14159265 / 2) << endl;
    return 0;
}

Output:

sqrt(a): 3  
pow(a, b): 81  
log(a): 2.1972  
sin(PI/2): 1

Mathematical Constants (C++20)

If using C++20, <numbers> provides constants like:

#include <numbers>
double pi = std::numbers::pi;

Data Types and Overloads

Most <cmath> functions are overloaded for:

  • float
  • double
  • long double

Example:

float f = sqrtf(9.0f);       // for float
long double ld = sqrtl(9.0L); // for long double

Best Practices & Tips

Always include <cmath> for math-related operations
Prefer M_PI or std::numbers::pi for constants
Avoid using raw values like 3.14β€”use library constants for precision
Use std::fixed and std::setprecision() to format float outputs


Use Cases for <cmath>

Scientific Calculators – Trig, log, power functions
Graphics & Games – Coordinate rotation, vector math
Simulations – Physics models and formulas
Geometry Applications – Area, volume, angles
Finance & Analytics – Exponentials, logarithmic growth


Summary – Recap & Next Steps

Key Takeaways:

  • <cmath> provides essential math functions for C++ applications
  • Covers power, root, trig, log, and rounding operations
  • Use math functions with std:: prefix and ensure correct types

Real-World Relevance:
Used in every mathematical C++ applicationβ€”finance, engineering, simulation, robotics, and gaming.

Next Steps:
Explore C++ <string> to perform string concatenation, manipulation, and formatting.


FAQ – C++ <cmath>

What’s the difference between abs() and fabs()?
abs() works with integers; fabs() is for floating-point types.

Can I use degrees with trigonometric functions?
No. Convert degrees to radians first: radians = degrees * (Ο€ / 180)

What does fmod(x, y) do?
It returns the remainder of floating-point division x / y.

Are <math.h> and <cmath> the same?
Mostly, but <cmath> puts functions in the std namespace and is preferred in C++.

Is <cmath> thread-safe?
Yes, in modern C++ implementations using reentrant functions.


Share Now :
Share

C++ <cmath>

Or Copy Link

CONTENTS
Scroll to Top