19. Math functions

By | September 23, 2021

C++ has many functions that allow you to perform mathematical tasks on numbers.

C++ offers some basic math functions and the required header file to use these functions is <math.h.

Mathematical calculations can be done in C++ programming language using the mathematical functions which are included in math or cmath library. These mathematical functions are defined to do complex mathematical calculations.

Trignometric functions

MethodDescription
cos(x)It computes the cosine of x.
sin(x)It computes the sine of x.
tan(x)It computes the tangent of x.
acos(x)It finds the inverse cosine of x.
asin(x)It finds the inverse sine of x.
atan(x)It finds the inverse tangent of x.
atan2(x,y)It finds the inverse tangent of a coordinate x and y.

Hyperbolic functions

MethodDescription
cosh(x)It computes the hyperbolic cosine of x.
sinh(x)It computes the hyperbolic sine of x.
tanh(x)It computes the hyperbolic tangent of x.
acosh(x)It finds the arc hyperbolic cosine of x.
asinh(x)It finds the arc hyperbolic sine of x.
atanh(x)It finds the arc hyperbolic tangent of x.

Exponential functions

MethodDescription
exp(x)It computes the exponential e raised to the power x.
frexp(value_type x,int* exp)It breaks a number into significand and 2 raised to the power exponent.
Idexp(float x, int e)It computes the product of x and 2 raised to the power e.
log(x)It computes the natural logarithm of x.
log10(x)It computes the common logarithm of x.
modf()It breaks a number into an integer and fractional part.
exp2(x)It computes the base 2 exponential of x.
expm1(x)It computes the exponential raised to the power x minus one.
log1p(x)It computes the natural logarithm of x plus one.
log2(x)It computes the base 2 logarithm of x.
logb(x)It computes the logarithm of x.
scalbn( x, n)It computes the product of x and FLT_RADX raised to the power n.
scalbln( x, n)It computes the product of x and FLT_RADX raised to the power n.
ilogb(x)It returns the exponent part of x.

Floating point manipulation functions

MethodDescription
copysign(x,y)It returns the magnitude of x with the sign of y.
nextafter(x,y)It represents the next representable value of x in the direction of y.
nexttoward(x,y)It represents the next representable value of x in the direction of y.

Maximum,Minimum and Difference functions

MethodDescription
fdim(x,y)It calculates the positive difference between x and y.
fmax(x,y)It returns the larger number among two numbers x and y.
fmin()It returns the smaller number among two numbers x and y .

Power functions

MethodDescription
pow(x,y)It computes x raised to the power y.
sqrt(x)It computes the square root of x.
cbrt(x)It computes the cube root of x.
hypot(x,y)It finds the hypotenuse of a right angled triangle.

Nearest integer operations

MethodDescription
ceil(x)It rounds up the value of x.
floor(x)It rounds down the value of x.
round(x)It rounds off the value of x.
lround(x)It rounds off the value of x and cast to long integer.
llround(x)It rounds off the value of x and cast to long long integer.
fmod(n,d)It computes the remainder of division n/d.
trunc(x)It rounds off the value x towards zero.
rint(x)It rounds off the value of x using rounding mode.
lrint(x)It rounds off the value of x using rounding mode and cast to long integer.
llrint(x)It rounds off the value x and cast to long long integer.
nearbyint(x)It rounds off the value x to a nearby integral value.
remainder(n,d)It computes the remainder of n/d.
remquo()It computes remainder and quotient both.

Other functions

MethodDescription
fabs(x)It computes the absolute value of x.
abs(x)It computes the absolute value of x.
fma(x,y,z)It computes the expression x*y+z.

Macro functions

MethodDescription
fpclassify(x)It returns the value of type that matches one of the macro constants.
isfinite(x)It checks whether x is finite or not.
isinf()It checks whether x is infinite or not.
isnan()It checks whether x is nan or not.
isnormal(x)It checks whether x is normal or not.
signbit(x)It checks whether the sign of x is negative or not.

Comparison macro functions

MethodDescription
isgreater(x,y)It determines whether x is greater than y or not.
isgreaterequal(x,y)It determines whether x is greater than or equal to y or not.
less(x,y)It determines whether x is less than y or not.
islessequal(x,y)It determines whether x is less than or equal to y.
islessgreater(x,y)It determines whether x is less or greater than y or not.
isunordered(x,y)It checks whether x can be meaningfully compared or not.

Error and gamma functions

MethodDescription
erf(x)It computes the error function value of x.
erfc(x)It computes the complementary error function value of x.
tgamma(x)It computes the gamma function value of x.
lgamma(x)It computes the logarithm of a gamma function of x.

Power

The pow function is used to calculate the power of the base raised to the power of exponent. It accepts two double values as arguments which are the base and exponents numbers and it returns a single double integer which is base to the power of exponent.

double pow( double, double)

Calling syntax

double x = pow(2, 4)

Example

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double base = 2 , power = 4;
   cout << "pow( "<<base<<" , "<<power<<" ) = " << pow(base, power) << endl;
}

Output

pow( 2 , 4 ) = 16

Sqrt ( square root)

sqrt function in C++ returns the square root of the double integer inside the parameter list. The method accept a double integer value as input find square root and returns a double integer as output.

double sqrt( double)

Calling syntax

double x = sqrt(25.00)

Example

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double a =25 ;
   cout << "sqrt( "<<a<<" ) = " << sqrt(a) << endl;
}

Output

sqrt( 25 ) = 5.00

sine

The sin method is used to calculate the sin of the angle given as an argument in degrees. This function accepts one double integer as an argument and returns a double integer which is the value of sin(x°).

double sin(double)

Calling syntax

double x = sin(23.4);

Example

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 45.3;
   cout << "sin ( "<<x<<" ) = " << sin(x) << endl;
}

Output

sin( 45.3 ) = 0.968142

Leave a Reply

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