Today I will try to explore math.h (library) header file provided to the programmers by turbo C++ compiler. Mostly people try to implement their own implementation of these functions; this is due to the lack of knowledge about Math library provided by turbo C++.

All functions in this library takes double data type as argument and returns double data type as well. There is another variant available which takes long double as argument and returns the value as long double data type. Such functions can be used by putting an extra l next to the function name i.e. sin() function could be written as sinl() i.e.

double sin(double x)

long double sinl(long double x)

The functions in math.h library can be divided into three main categories i.e.

Table of Contents

Trigonometric Functions

double sin(double x)

Computes and returns the sine value of a radian angle x.

double asin(double x)

Computes and returns the value of inverse sine or arcsine of x in radians.

double sinh(double x)

Computes and returns the hyperbolic sine of x.

double cos(double x)

Computes and returns the cosine of x where x is an angle expressed in radians.

double acos(double x)

Computes and returns the value of inverse cosine or arccosine of x.

double cosh(double x)

Computes and returns the hyperbolic cosine value of x.

double tanh(double x)

Computes and returns the hyperbolic tangent value of x.

double atan(double x)

Computes and returns the inverse tangent or arctangent of x expressed in radians.

double atan2(double y, double x)

Computes and returns the arc tangent in radians of y/x based on the signs of both values to determine the correct quadrant.

The advantage of atan2() over atan() is that, because it is given separate access to the two original (rectangular) co-ordinates, it is able to distinguish between angles in the left and right half planes (which is not possible if only y/x is passed in, as is the case with atan()).

Exponential and Logarithmic Functions

double exp(double x)

Computes and returns the value of e raised to the xth power i.e. exponential function of x or ex

double frexp(double x, int *exponent)

The returned value is the mantissa and the integer pointed to by exponent is the exponent. The resultant value is x = mantissa * 2 ^ exponent.

double ldexp(double x, int exponent)

Computes and returns x multiplied by 2 raised to the power of exponent.

double log(double x)

Computes and returns the natural logarithm (base-e logarithm) of x i.e. ln(x) where x must be greater than 0.

double log10(double x)

Computes and returns the common logarithm (base-10 logarithm) of x i.e. log10(x) where x must be greater than 0.

double pow(double x, double y)

Computes and returns x raised to the power of y i.e. xy. The functions return an error if x equal to zero and y less than or equal to zero; or with x less than zero and y not an integer.

Mathematical Functions

double modf(double x, double *integer)

The returned value is the fraction component (part after the decimal), and sets integer to the integer component.

double sqrt(double x)

Computes and returns the square root value of x i.e. √x where x must be greater than or equal to zero.

double ceil(double x)

Computes and returns the nearest integer value which is greater than or equal to the value of x.

double fabs(double x)

Computes and returns the absolute value of x.

double floor(double x)

Computes and returns the largest integer value less than or equal to x.

double fmod(double x, double y)

Computes and returns the remainder of x divided by y. This is similar to using Modulus % operator.

C Programs to demonstrate math functions

Find the Square root of a number

Function: double sqrt(double);
It calculates the positive square root of the input value which is given as double data type.

Find the Power of a number

Function: double pow(double x, double y)
It calculates the value of xy . If both the values are 0 then the value returned by the function is 0. If the result is a more bigger number; which a variable of type double can not contain; then the function return an error (HUGE_VAL).

Find Ceil and Floor values

Functions:
double ceil(double x);
double florr(double x);

The ceil() function finds the smallest integer value of the value passed as the parameter, but the returned values is not greater than the values passed to the function. Where as floor() finds the largest integer value not bigger than the value passed as the parameter.

Math.h Header File

Here is the full source code from Turbo C++ 3.2 compiler. Note that math.h header file uses #ifndef directive to define the header file.