×

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us look at multiple categories to understand multiple functions along with their descriptions through the following tables.


Exponential Functions

      FunctionDescription
log(x)It evaluate the natural algorithm of argument x.
log10(x)It evaluate the common algorithm of x.
modf(x)Breaks number into a fraction and integer parts
exp2(x)Evaluates base 2 exponents of x.
log2(x)Evaluates base 2 logarithms of x.
scalbn(x,n)It evaluates the product of x raised to power n.
exp(x)It evaluates the exponential e raised to the power x.
idexp(float x, int e)It evaluates the product of x and 2 raised to the power 2.
log1p(x)It evaluates the natural algorithm of x plus one.

There are many such relevant exponential functions present in the library of C++. Let us now look at some other math functions.

Maximum and minimum difference

FunctionDescription
        fmin()Returns smallest number among two numbers(x and y).
        fmax(x,y)Returns largest among two numbers(x and y).
        fdim(x,y)Calculates positive difference of x and y.

Power functions

FunctionDescription
       pow(x,y)It evaluates power of x raised to y.
       sqrt(x)It evaluates the square-root of x.
       hypot()Calculates hypotenuse of right angled triangle.
       cbrt(x)Finds the cube-root of x.

Trigonometric functions

FunctionDescription
       cosEvaluates cosine.
       sinEvaluates sine.
       tanEvaluates tangent.
      acos, asin, atanEvaluates arc cos, arc sine and arc tangent functions.

These tables above are some basic math functions used in C++. Let us now look at some other function which needs proper description.

Note: In order to use the functions in our program we need to include <math.h> or <cmath> in our program.

Other functions

  1. double sin(double):  It provides verified sine value by taking angle in degree.
  • double cos(double):  It provides verified cosine value by taking angle in degree.
  • double tan(double): It provides verified tangent value by taking angle in degree.
  • double sqrt(double): It provides non-negative square-root by taking the number as an argument.
  • int abs(int): Returns positive absolute value regardless of positive or negative sign.
  • double pow(double, double): It provides one argument as a base and the other as an exponent.
  • double floor(double): It provides integer value less than or equal to the argument.
  • double atan2(double, double): This function returns the arc tangent of (double a)/(double b).
  • double ceil(double): It provides the smallest integer as double, not less than the argument provided.

Let’s look at some programming example to understand the working of these functions.

Example 1:

 #include<iostream>
 #include<math.h>
 using namespace std;
 int main()
 {
                 short int si = 1200;
                 int i = -200;
                 long int li = 4300;
                 float f = 23.41;
                 double d = 210.317;
                 cout<<"Square root of(si): "<<sqrt(si)<<endl;
                 cout<<"Power of(li, 3): "<<pow(li, 3)<<endl;
                 cout<<"Sine of(d): "<<sin(d)<<endl;
                 cout<<"Adsolute of(i) : "<<abs(i)<<endl;
                 cout<<"Floor value of(d): "<<floor(d)<<endl;
                 cout<<"Square-Root of(f): "<<sqrt(f)<<endl;
                 cout<<"Power of(d, 2): "<<pow(d, 2)<<endl;
 } 

Output:

C++ Math Functions

Explanation:

In the above code, we have used some of the functions present in the <math.h> library to demonstrate how these functions work in the program. The functions used in the program are pre-defined in the library of mathematical functions in C++. We have defined several functions and have initialized them with some random values of their respective data type. The values in the output are the result after mathematical calculations.

Example 2:

 #include <iostream>
 #include <math.h>
 using namespace std;
 int main()
 {
     cout << exp(5) << "\n";
     cout << log(4) << "\n";
     cout << log10(3) << "\n";
     cout << exp2(2) << "\n";
     cout << log2(1) << "\n";
     cout << logb(0) << "\n";
 return 0;
 } 

Output:

C++ Math Functions

Explanation:

The above code is another demonstration of using logarithmic functions present in the C++ math functions library. We have used different types of logarithmic functions to display their values after evaluation.

Insights:

Being the superset of C, C++ supports a huge number of useful mathematical functions. The benefit of using these functions is the reduction of space in the memory to define them while writing codes specifically for these operations. It reduces human efforts and errors as the compiler itself takes care of these things. Therefore, instead of focusing on implementation, these functions can directly simplify a large set of operations. Moreover, by using these functions, we can simplify the program itself.


Related Topics

Message Passing in C++

The act of sending and receiving information by an object is referred to as communication, and all communication between objects that takes place via message is known as message passing....

1 minute read.

Virtual Function Vs Pure Virtual Function

Virtual activity is a member function defined in the foundation phase that can be redefined by acquired classes. Let's have a look at an example: #include <iostream>   #include <bits/stdc++.h> #include <stdlib> using namespace std;   class Base   {    ...

5 minutes read.

Top 5 IDEs for C++ That You Should Try Once

In the past decades, creating an application or interface from the very basic idea, the developer has to struggle a lot for it. Because an application is a combination of...

3 minutes read.

C++ Goto

In this article, we will discuss the C++ goto statement with its syntax, use, key features, key points, pseudo code, and examples. What is the goto statement in C++? In C++, the...

4 minutes read.

C++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

C++ Continue in Do-while loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

Diamond Pattern using While-loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

5 minutes read.

User-defined literals in C++

Introduction to User-Defined Literals: User-defined literals, introduced in C++11, are a way to extend the C++ language to allow users to define their literal suffixes and the corresponding behavior. These suffixes...

7 minutes read.

Find the Size of Array in C/C++ without using sizeof() function

We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of...

3 minutes read.

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

Implementing the sets without C++ STL containers

Many practical features and tools in C++ support us in programming competitions. One of these parts is a set from the Standard Template Library (STL), which offers an effective way...

6 minutes read.

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

Single dimension array

C++ Array An array is a collection of data (elements) of the same data types. The elements of an array are allocated in contiguous memory allocation. Elements of the array are accessed through...

1 minute read.