Inbuilt Functions

Mathematical Functions

We can do the mathematical calculations easily in the C++ programming language with the use of the mathematical functions that are contained within the math or the cmath library. Moreover, these mathematical functions are defined to perform complex mathematical calculations as well. Thus, one of these functions are available below.

mathematical functions

fabs()

Function prototype:

double fabs(double x);

Description:

It returns the absolute value of ‘x’.

Example:

cout<< fabs(3.14161);

**returns 3.14161

pow()

Function prototype:

double pow (double base, double exponent).

Function Parameters:

base=> base value.

Exponent=> exponent value

Return value:

The value gained after raising the base to the exponent.

Description:

The function pow() takes in 2 arguments i.e. the base and the exponent. After that, it increases the base to the power of the exponent.

When the base is finite negative and the exponent is negative but is not an integer value, the domain error arises. Moreover, certain implementations can cause the domain error when the base and the exponent are both zero and if the base is 0 and the exponent is negative.

If the result of the function is too small or too big for the return type, it can result in a range error.

Browse more Topics under Inbuilt Functions

Example:

                    
#include 

#include 

using namespace std;

int main ()

{

cout<< "2 ^ 4 = "<
                

Output:

                    

2 ^ 4 = 16

4 ^ 12 = 1.67772e+07

7 ^ 3 = 343

sqrt()

Function prototype:

double sqrt (double x);

Function Parameters:

x=>value whose square root is to be calculated.

If ‘x’ is negative, domain_error occurs.

Return value:

A double value that indicates the square root of ‘x’.

If ‘x’ is negative, then the domain_error arises.

Description:

The sqrt() function takes in the number in the form of a parameter and calculates their squares root. Moreover, if the argument is negative, then a domain error will arise.

Example:

                    

#include 

#include 

using namespace std;

int main ()

{

double param, result;

param = 1024.0;

result = sqrt (param);

cout<<"Square root of "<<param<<"(sqrt("<<param<<")):"<<result<<endl;

param = 25;

result = sqrt (param);

cout<<"Square root of "<<param<<"(sqrt("<<param<<")):"<<result<<endl;

return 0;

}

Output:

                    

Square root of 1024(sqrt(1024)):32

Square root of 25(sqrt(25)):5

sin()

Function prototype:

double sin(double x);

Description:

It returns the sine of angle ‘x’ in the radians.

Example:

cout<< sin ( 60.0 * PI / 180.0 );

(here PI = 3.142)

**returns 0.841471

cos()

Function prototype:

double cos (double x);

Description:

It returns the cosine of the angle ‘x’ in the radians.

Example:

cout<< cos ( 60.0 * PI / 180.0 );

(here PI = 3.142)

**returns 0.540302

abs()

Function prototype:

return_type abs (data_type x);

Function Parameters:

x=> value whose absolute value is to be returned.

x can be of the following types:

double

float

long double

Return value:

It returns the exact value of ‘x’.

As the parameters, the return value can be of the following categories:

double

float

long double

Description:

We use the function abs() to return the exact value of the parameter that is passed to the function.

Example:

                    

#include 

#include 

using namespace std;

int main ()

{

cout << "abs (10.57) = " << abs (10.57) << '\n';

cout << "abs (-25.63)  = " << abs (-25.63) << '\n';

return 0;

}

Output:

                    

abs(10.57) = 10.57

abs(-25.36) = 25.63

FAQs on Mathematical Functions

Question 1: With which do the trigonometric functions comes into use with angles in c++?

Answer: The trigonometric functions are applicable to the angles in the radians rather than the degrees.

Question 2: Deliver the output of the following C++ code?

Program:

                    

#include 

#include 

int main ()

{

int param, result;

int n;

param = 8.0;

result = frexp (param , &n);

printf ("%d \n", param);

return 0;

}

Answer: In the above program, it is breaking out the number with the use of the frexp() function.

Output:

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.