EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Math Functions in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » Math Functions in C++

Math Functions in C++

Introduction to Math Functions in C++

C++ provides <math.h> library for math functions to perform the complex mathematical functions like trigonometric function, algebraic equations easily. For example, sin() function is used to calculate the value of sin, pow() the function is used to calculate the power of the value, sqrt is used to calculate the square root of the value.

Different Types of Math Functions

C++ provides a huge number of different types of math functions mentioned below with examples:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. Maximum & Minimum function

  • max (p,q): It will return a maximum number between p and q.
  • min (p,q): It will return a minimum number between p and q.
C++ Code to Implement Above Functionality

#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << max(16,18) << "\n";
cout << min(16,18) << "\n";
return 0;
}

Output:

math Function output 1

2. Power functions

  • pow (m,n): It will calculate m raised to the power n.
  • sqrt(m): It will calculate the square root of m.
  • cbrt(n): It will calculate the cube root of n.
  • hypot(m,n): It will calculate the hypotenuse of the right-angled triangle.
C++ code to implement above functionality

#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << pow(2,3) << "\n";
cout << sqrt(16) << "\n";
cout << cbrt(27) << "\n";
cout << hypot(3,4) << "\n";
return 0;
}

Output:

math Function output 2

3. Exponential functions

  • exp(p): It will calculate the exponential e raised to power p.
  • log(p): It will calculate the logarithm of p.
  • log10(p): It will calculate the common logarithm of p.
  • exp2(p): It will calculate the base 2 exponential of p.
  • log2(p): It will calculate the base 2 logarithm of p.
  • logb(p): It will calculate the logarithm of p.
C++ code to implement above functionality

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

Output:

math Function output 3

4. Integer functions

It helps in finding the nearest integer value.

  • ceil(z): it rounds up the value of z.
  • floor(z): it rounds down the value of z.
  • round(z): It rounds off the value of z.
  • fmod(z,y): It calculates the remainder of division z/y.
  • trunc(z): It will round off the z value towards zero.
  • rint(z): It will round off the z value using rounding mode.
  • nearbyint(z): It will round off the z value to a nearby integral value.
  • remainder(z,y): It will calculate the remainder of z/y.
C++ code to implement above functionality

#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << ceil(4580.01) << "\n";
cout << floor(151.999) << "\n";
cout << round(518.5) << "\n";
cout << fmod(5,21) << "\n";
cout << trunc(20.25) << "\n";
cout << rint(21.25) << "\n";
cout << nearbyint(182.55) << "\n";
cout << remainder(12,36) << "\n";
return 0;
}

Output:

math Function output 4

5. Comparison functions

Help in comparing numbers in a quick span doesn’t matter how long the number is. Below are a few examples of Comparison functions:

  • isgreater(p,q): It checks whether p is greater than q or not.
  • islessequal(p,q): It checks whether p is less than or equal to q or not.
  • isgreaterequal(p,q): It checks whether p is greater than or equal to q or not.
  • islessgreater(p,q): It checks whether p is less or greater than y or not.
  • isunordered(p,q): It checks whether p compared or not.
C++ code to implement above functionality

#include <iostream>
#include <math.h>
using namespace std;
int main() {
// cout << less(22,29) << "\n";
cout << isgreater(48,47)<< "\n";
cout << islessequal(11,5)<< "\n";
cout << isgreaterequal(19,72)<< "\n";
cout << islessgreater(59,84)<< "\n";
cout << isunordered(62,84)<< "\n";
return 0;
}

Output:

output 5

6. Using Trigonometric Function

Functions specially used in geometric calculations. The right-angled triangle gives a relation between angle to the ratio of the length of the two sides.

  • sin(y): It will calculate the value of sine y.
  • cos(y): It will calculate the value of cosine y.
  • tan(y): It will calculate the value of tangent y.
  • asin(y): It will calculate the value of inverse sine y.
  • acos(y): It will calculate the value of inverse cosine y.
  • atan(y): It will calculate the value of inverse tangent y.
  • atan2(y,x): It will calculate the value of the inverse tangent of y and x coordinates.
C++ code to implement above functionality

#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout   <<   sin(0)  << "\n";
cout   <<   cos(0)  << "\n";
cout   <<   tan(1)  << "\n";
cout   <<   asin(1)<< "\n";
cout  <<   acos(0)<< "\n";
cout   <<   atan(1)<< "\n";
cout   <<   atan2(0,1)<< "\n";
return 0;
}

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,899 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

Output:

output 6

Here are some more interesting functions that will help in calculating values of hyperbolic trigonometric functions and they are called Hyperbolic functions.

  • sinh(x): It will calculate the value of the hyperbolic sine of x.
  • cosh(x): It will calculate the value of the hyperbolic cosine of x.
  • tanh(x): It will calculate the value of the hyperbolic tangent of x.
  • asinh(x): It will calculate the value of the hyperbolic arc sine of x.
  • acosh(x): It will calculate the value of the hyperbolic arc cosine of x.
  • atanh(x): It will calculate the value of the hyperbolic arc sine of x.
C++ code to implement above functionality

#include <iostream>
#include <math.h>
using namespace std;
int main() {
cout << sinh(0)<< "\n";
cout << cosh(0)<< "\n";
cout << tanh(1)<< "\n";
cout << asinh(1)<< "\n";
cout << acosh(1)<< "\n";
cout << atanh(0)<< "\n";
return 0;
}

Output:

output 7

Conclusion

Math functions play an important role in saving a huge amount of time and space in memory. All the functions are built-in, no need to implement directly use any math function just by adding a header file which will give the option to use the whole library of math class.

Recommended Articles

This is a guide to Math Functions in C++. Here we discuss the C++ provides a huge number of different types of math functions with examples. You can also go through our other suggested articles –

  1. Overriding in C++
  2. Overloading in C++
  3. Square Root in C++
  4. ceil function in C++

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More