×

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

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++ Fibonacci Series

What is a Fibonacci series? A Fibonacci series or sequence is a very popular programming paradigm. The next element occurring in the N terms series is determined by the sum of...

2 minutes read.

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

C++ Program to move all zeros to the end of the array

Write a program to move all the zeros in the arr[] to the end. The order of the non-zero elements should not be altered and all the zeros should be...

3 minutes read.

C++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

3 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

C++ Range-based For Loop

In C++ language, the range-based for loop was added, which is far superior than the ordinary For loop. The implementation of a range-based for loop doesn't really need much code. It's a...

4 minutes read.

How to initialize a dynamic array in C++

Regular arrays or static arrays have a predetermined size or fixed size. Change in the size of regular arrays is not possible. The memory size for static arrays determines at compile...

4 minutes read.

goto statement in C and C++

goto statement in C and C++ The goto statement is a jump statement, also sometimes referred to as an unconditional jump statement. Within a function, the goto statement can be used...

3 minutes read.

Do-While Loop Examples in C++

Before we move on the examples of Do-While loop, let’s learn little bit about the Do-While loop in C++ language. Do While loop An iterative loop that checks the condition at the...

5 minutes read.

OOPs Concepts in C++

C++ Object-Oriented Programming Concepts C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features: Object Class Data abstraction Encapsulation Polymorphism Inheritance Message passing Object An object is the basic unit...

2 minutes read.

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

3 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

1 minute read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.

Unary Operators in C++

Unary operators in C++ Unary operator: is operations that function to produce a new value on a single operand. a) unary minus: A minus operator modifies the argument's symbol. A positive number...

3 minutes read.

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

3 minutes read.