
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <cmath>
The C++ <cmath> header in C++, provides a set of built-in functions that perform common mathematical calculations. These functions cover a range of operations, including trigonometric, exponential, logarithmic, and power functions.
These functions can be applied to both individual values (scalars) and collections of values (vectors), making <cmath> a versatile tool for programmers.
Including <cmath> Header
To use the cmath in C++, we need to include the cmath header file as shown below.
#include <cmath>
Functions from <cmath>
Below is list of all functions from <cmath> header.
Trigonometric Functions
These functions handle standard trigonometric calculations like sine, cosine, and tangent.
S.NO | Functions & Description |
---|---|
1 |
This function returns the cosine of an angle of x radians. |
2 |
This function returns the sine of an angle of x radians. |
3 |
This function returns the tangent of an angle of x radians. |
4 |
This function returns the principle value of the arc cosine of x, expressed in radians. |
5 |
This function returns the principal value of the arc sine of x, expressed in radians. |
6 |
This function returns the principal value of the arc tangent of x, expressed in radians. |
Computing the Sine of an Angle in Radians
In the following example we are going to use sin() to compute the sine of an angle in radians.
#include <iostream> #include <cmath> int main() { double angle = 1.0; // Angle in radians double result = sin(angle); std::cout << "The sine of " << angle << " radians is " << result << std::endl; return 0; }
Output
The sine of 1 radians is 0.841471
Hyperbolic Functions
These functions are the hyperbolic counterparts of the trigonometric functions.
S.NO | Functions & Description |
---|---|
1 |
This function returns the hyperbolic cosine of x. |
2 |
This function returns the hyperbolic sine of x. |
3 |
This function returns the hyperbolic tangent of x. |
4 |
This function returns the non-negative area hyperbolic cosine of x. |
5 |
This function returns the hyperbolic cosine of x. |
6 |
This function returns the area hyperbolic tangent of x. |
Calculating the Hyperbolic Sine of a Number
In the following example we are going to use sinh() computes the hyperbolic sine of a number.
#include <iostream> #include <cmath> int main() { double value = 1.0; double result = sinh(value); std::cout << "The hyperbolic sine of " << value << " is " << result << std::endl; return 0; }
Output
The hyperbolic sine of 1 is 1.1752
Exponential and logarithmic Functions
These functions handling with exponential and logarithmic calculations.
S.NO | Functions & Description |
---|---|
1 |
This function return the base-e exponential function of x, which is e raised to the power x: e^x. |
2 |
This function Breaks the floating point number x into its binary significant and an integral exponent for 2. |
3 |
This function returns the natural logarithm of x. |
4 |
This function returns the common (base-10) logarithm of x. |
5 |
Breaks x into an integral and a fractional part. |
6 |
This function returns the base-2 exponential function of x, which is 2 raised to the power x: 2^x. |
7 |
This function returns the integral part of the logarithm of |x|, using RADIX as base for the logarithm. |
8 |
This function returns the natural logarithm of one plus x. |
Evaluating the Exponential Function
In the following example we are going to use exp() to compute e^2. calculates the exponential value of e (Euler's number) raised to the power of the given input.
#include <iostream> #include <cmath> int main() { double exponent = 2.0; double result = exp(exponent); std::cout << "The value of e^" << exponent << " is " << result << std::endl; return 0; }
Output
The value of e^2 is 7.38906
Power Functions
These functions handles with calculations related to power and roots.
S.NO | Functions & Description |
---|---|
1 |
This function returns the base raised to the power exponent. |
2 |
This function returns the square root of x. |
3 |
This function returns the cubic root of x. |
4 |
This function returns the hypotenuse of a right-angled triangle whose legs are x and y. |
Raising a Base to a Given Exponent
In the following example we are going to use pow() function to return the result of raising base to the power exponent.
#include <iostream> #include <cmath> int main() { double base = 2.0, exponent = 3.0; double result = pow(base, exponent); std::cout << base << " raised to the power of " << exponent << " is " << result << std::endl; return 0; }
Output
2 raised to the power of 3 is 8
Error and gamma Functions
The cmath library includes special functions like error function (erf) and gamma function (tgamma), used in various fields such as statistics, physics etc.
S.NO | Functions & Description |
---|---|
1 |
This function return the Returns the error function value for x. |
2 |
This function returns the complementary error function. |
3 |
This function returns the gamma function of x. |
4 |
This function returns the natural logarithm of the absolute value of the gamma function of x. |
Rounding and remainder Functions
These functions handle rounding and remainder calculations.
S.NO | Functions & Description |
---|---|
1 |
Rounds x upward, returning the smallest integral value that is not less than x. |
2 |
Rounds x downward, returning the largest integral value that is not greater than x. |
3 |
This function returns the integral value that is nearest to x, with halfway cases rounded away from zero. |
4 |
Rounds x toward zero, returning the nearest integral value that is not larger in magnitude than x. |
5 |
Returns the floating-point remainder of numer/denom (x/y) rounded towards zero. |
Finding the Ceiling of a given Number
In the following example we are going to use ceil() which rounds a floating-point number upward, returning the smallest integer greater than or equal to the input.
#include <iostream> #include <cmath> int main() { double value = 3.45; double result = ceil(value); std::cout << "The ceiling of " << value << " is " << result << std::endl; return 0; }
Output
The ceiling of 3.45 is 4
Floating-point manipulation Functions
These floating point manipulation functions work on handling floating-point numbers.
S.NO | Functions & Description |
---|---|
1 |
This function returns a value with the magnitude of x and the sign y. |
2 |
This function returns a quiet NaN (Not-A-Number) value of type double. |
3 |
This function returns the next representable value after x in the direction of y. |
4 |
This function returns the next representable value after x in the direction of y. |
Minimum, Maximum, Difference Functions
These functions finds the minimum, maximum, and absolute differences between two numbers.
S.NO | Functions & Description |
---|---|
1 |
This function returns the smaller of its arguments: either x or y. |
2 |
This function returns the larger of its arguments: either x or y. |
3 |
This function returns the positive difference between x and y. |
Other Functions
The cmath header provides other functions, especially to handle floating-point numbers.
S.NO | Functions & Description |
---|---|
1 |
This function returns the absolute value of x: |x|. |
2 |
This function computes the absolute value for integers or floating-point types. |
3 |
This multiply-add function computes the product of x and y, then adds z to the result (x*y+z). |