Back to C++
2026-03-175 min read

Common mathematics functions (C++)

Learn Common mathematics functions (C++) step by step with clear examples and exercises.

Why This Matters

Mathematical functions are an essential part of programming, and understanding them can significantly improve your coding skills. In this lesson, we'll delve into common mathematical functions available in C++, focusing on their practical uses, real-world examples, and potential pitfalls to avoid.

Why This Matters

In programming, mathematical functions are used to perform various calculations such as trigonometry, exponential, logarithmic, and more. Understanding these functions is crucial for solving complex problems, debugging issues, and preparing for interviews or exams that require problem-solving skills.

Prerequisites

Before diving into the core concept, it's essential to have a basic understanding of C++ syntax, variables, and data types. Familiarity with functions, function prototypes, and function calls is also required.

#include <iostream> // Include the iostream library for input/output operations
using namespace std; // Use the standard namespace to avoid writing std:: before every standard library element

// Function prototype
void printHello(); // Declare a function called printHello with no return type and no parameters

Core Concept

C++ provides a rich set of mathematical functions, which are part of the `` library. Let's explore some commonly used ones:

Basic Operations

  • abs(int): Returns the absolute value of an integer.
  • fabs(double): Returns the absolute value of a floating-point number.
  • div(int, int): Performs integer division and returns a pair containing quotient and remainder.
  • modf(double, float*): Splits a floating-point number into an integer part and a fractional part.

Exponential Functions

  • exp(double): Returns the value of e raised to the power of the given number.
  • exp2(double): Returns 2 raised to the power of the given number.
  • expm1(double): Returns e^x - 1, where x is the input number.

Trigonometric Functions

  • sin(double), cos(double), tan(double): Return the sine, cosine, and tangent of an angle in radians, respectively.
  • asin(double), acos(double), atan(double): Inverse trigonometric functions that find the angle whose sine, cosine, or tangent is a given value.
  • atan2(double, double): Returns the angle in radians between the positive x-axis and the argument (a, b), where a and b are Cartesian coordinates of a point.

Hyperbolic Functions

  • sinh(double), cosh(double), tanh(double): Return the hyperbolic sine, cosine, and tangent of an angle in radians, respectively.
  • asinh(double), acosh(double), atanh(double): Inverse hyperbolic trigonometric functions that find the angle whose hyperbolic sine, cosine, or tangent is a given value.

Error and Gamma Functions

  • erf(double): Returns the error function of a given number.
  • erfc(double): Returns the complementary error function of a given number.
  • lgamma(double): Computes the natural logarithm of the gamma function of a given number.
  • tgamma(double): Computes the gamma function of a given number.

Nearest Integer Floating Point Operations

  • ceil(double), floor(double): Round a floating-point number up to the nearest integer (ceil) or down to the nearest integer (floor).
  • round(double): Rounds a floating-point number to the nearest integer. If the fractional part is 0.5 or greater, it rounds up; otherwise, it rounds down.
  • lround(long double), llround(long long): Round a long double or long long to the nearest integer and return a long int or long long, respectively.
  • trunc(double): Truncates a floating-point number by removing its fractional part.
  • nearbyint(double): Returns the closest integer to the given number.
  • rint(double): Rounds a double to the nearest integer towards zero (rounds down for negative numbers and rounds up for positive numbers).

Floating Point Manipulation Functions

  • frexp(double, int*): Splits a floating-point number into its mantissa and exponent.
  • ldexp(double, int): Returns the product of a floating-point number and 2 raised to the power of an integer.
  • scalbn(double, int): Scales a floating-point number by multiplying it with 2 raised to the power of an integer.
  • scalbln(double, long): Scales a floating-point number by multiplying it with 2 raised to the power of a long integer.
  • ilogb(double): Returns the exponent part of a normalized floating-point number (the mantissa is 1 and the fractional part is nonzero).
  • logb(double): Returns the exponent part of a floating-point number.
  • ldexp(long double, int): Returns the product of a long double and 2 raised to the power of an integer.
  • nextafter(double, double): Returns the next representable value towards the specified value (closest representable value if the specified value is representable).

Worked Example

Let's create a simple program that demonstrates some mathematical functions in action:

#include <iostream>
#include <cmath> // Include the cmath library for mathematical functions
using namespace std;

int main() {
double x = 3.14;
int y = -5;

cout << "Absolute value of x: " << abs(x) << endl;
cout << "Square root of x: " << sqrt(x) << endl;
cout << "Sine of x: " << sin(x * M_PI / 180.0) << endl; // Convert degrees to radians
cout << "Cosine of y: " << cos(-y) << endl;
cout << "Tangent of y: " << tan(y * M_PI / 4.0) << endl; // Convert degrees to radians and divide by 4

return 0;
}

Compile and run the program, and you'll see output similar to this:

Absolute value of x: 3.14
Square root of x: 1.77245
Sine of x: 0.540302
Cosine of y: -0.857863
Tangent of y: -1.76776

Common Mistakes

Neglecting to include the `` library

Remember to include the `` library at the beginning of your code, as mathematical functions are part of this library.

Forgetting to convert degrees to radians when using trigonometric functions

Trigonometric functions in C++ expect their arguments in radians. To convert degrees to radians, multiply the angle by M_PI / 180.0.

Using floating-point operations without considering precision issues

Floating-point arithmetic can suffer from precision loss due to rounding errors. Be aware of this when working with floating-point numbers and consider using higher-precision data types like long double if necessary.

Practice Questions

  1. Write a program that calculates the area of a circle given its radius. Use the M_PI constant for π.
  2. Write a program that finds the roots of a quadratic equation (ax² + bx + c = 0). Use the sqrt() function to find the square root.
  3. Write a program that calculates the factorial of a given number using recursion and the accumulate() algorithm from the `` library.

FAQ

What is the difference between exp(x) and exp2(x)?

  • exp(x) returns e raised to the power of x, while exp2(x) returns 2 raised to the power of x.

Why do trigonometric functions in C++ expect their arguments in radians?

  • Trigonometric functions are defined using angles measured in radians, so they require input in this format for consistency and accuracy.

How can I handle precision issues when working with floating-point numbers?

  • To minimize precision loss, consider using higher-precision data types like long double or implementing custom functions to perform calculations with multiple precision steps.
Common mathematics functions (C++) | C++ | XQA Learn