CPP Tutorial

CPP examples programs

CPP Math Functions

Math functions and constants are already defined in the CPP library, in <math.h> header file, which can be used to perform mathematical calculations.

CPP provides various math functions including;

abs() function:

This function is used to get an absolute value of a number.

Syntax:

abs ( number )

ceil() function:

This function is used to get a round up value; greater than or equal to given number.

Syntax:

ceil ( number )

floor() function:

This function is used to get a round down value; less than or equal to given number.

Syntax:

floor ( number )

sqrt() function:

This function is used to get the square root of a number.

Syntax:

sqrt ( number )

pow() function:

This function is used to get the value of base raised to the power of exponent.

Syntax:

pow ( base, exponent )

There are numerous other functions defined by C which are used to perform mathematical operations.

Types of Math Functions:

Comparison macro functions:

METHOD DESCRIPTION
isgreater(x,y) To determine whether x is greater than y or not.
isgreaterequal(x,y) To determine whether x is greater than or equal to y or not.
less(x,y) To determine whether x is less than y or not.
islessequal(x,y) To determine whether x is less than or equal to y.
islessgreater(x,y)             To determine whether x is less or greater than y or not.
isunordered(x,y) To check whether x can be meaningfully compared or not

 

Error and gamma functions

METHOD DESCRIPTION
erf(x) To compute the error function value of x.
erfc(x) To compute the complementary error function value of x.
tgamma(x) To compute the gamma function value of x.
lgamma(x) To compute the logarithm of a gamma function of x.

 

Exponential functions

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

 

Floating point manipulation functions

METHOD DESCRIPTION
copysign(x,y) To return the magnitude of x with the sign of y.
nextafter(x,y) To represent the next representable value of x in the direction of y.
nexttoward(x,y) To represent the next representable value of x in the direction of y.

 

Hyperbolic functions

METHOD DESCRIPTION
cosh(x) To compute the hyperbolic cosine of x.
sinh(x) To compute the hyperbolic sine of x.
tanh(x) To compute the hyperbolic tangent of x.
acosh(x) To find the arc hyperbolic cosine of x.
asinh(x) To find the arc hyperbolic sine of x.
atanh(x) To find the arc hyperbolic tangent of x.

 

Macro functions

METHOD DESCRIPTION
fpclassify(x) To return the value of type that matches one of the macro constants.
isfinite(x) To check whether x is finite or not.
isinf() To check whether x is infinite or not.
isnan() To check whether x is nan or not.
isnormal(x) To check whether x is normal or not.
signbit(x) To check whether the sign of x is negative or not.

 

Maximum,Minimum and Difference functions

METHOD DESCRIPTION
fdim(x,y) To calculate the positive difference between x and y.
fmax(x,y) To return the larger number among two numbers x and y.
fmin() To return the smaller number among two numbers x and y.

 

Nearest integer operations 

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

 

Power functions

METHOD DESCRIPTION
pow(x,y) To compute x raised to the power y.
sqrt(x) To compute the square root of x.
cbrt(x) To compute the cube root of x.
hypot(x,y) To find the hypotenuse of a right angled triangle.

 

Example:

#include <iostream.h>  
#include <math.h>
using namespace std;  
int main()
{
cout << pow(6,2) <<endl;    
cout << abs(-897);
}

Output

36
897