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.

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.

#include <stdio.h>
#include <math.h>

int main(void)
{
  double dSqrtNumber = 36.0
  double dSqrtResult;   
  dSqrtResult = sqrt(dSqrtNumber);
  printf("The square root of the number ");
  printf("%lf is: %lf\n",  dSqrtNumber, dSqrtResult);
  return 0;
} 

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).

#include <stdio.h>
#include <math.h>

int main(void)
{
  double dValuex = 5.0, dValuey = 5.0;
  double dResult;
  result = pow(dValuex, dValuey);
  printf("%lf power %lf is: %lf\n", dValuex, dValuey, dResult);
  return 0;
}

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.

#include <stdio.h>
#include <math.h>

int main(void)
{
  double dNumber = 50.54;
  double dFloorValue, dCeilValue;
  dFloorValue = floor(dNumber);
  dCeilValue = ceil(dNumber);€
  printf("Original number is:     %5.2lf\n", dNumber);
  printf("Number rounded down %5.2lf\n", dFloorValue);
  printf("Number rounded up   %5.2lf\n", dCeilValue);
  return 0;
}

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.

/*  math.h
    Definitions for the math floating point package.
    Copyright (c) 1987, 1991 by Borland International
    All Rights Reserved.
*/

#ifndef  __MATH_H
#define  __MATH_H

#if !defined( __DEFS_H )
#include <_defs.h>
#endif

#define HUGE_VAL    _huge_dble
extern double _Cdecl _huge_dble;
#define _LHUGE_VAL   _huge_ldble
extern long double _Cdecl _huge_ldble;

#ifdef __cplusplus
extern "C" {
#endif
double  _Cdecl acos  (double __x);
double  _Cdecl asin  (double __x);
double  _Cdecl atan  (double __x);
double  _Cdecl atan2 (double __y, double __x);
double  _Cdecl ceil  (double __x);
double  _Cdecl cos   (double __x);
double  _Cdecl cosh  (double __x);
double  _Cdecl exp   (double __x);
double  _Cdecl fabs  (double __x);
double  _Cdecl __fabs__  (double __x);          /* Intrinsic */
double  _Cdecl floor (double __x);
double  _Cdecl fmod  (double __x, double __y);
double  _Cdecl frexp (double __x, int *__exponent);
double  _Cdecl ldexp (double __x, int __exponent);
double  _Cdecl log   (double __x);
double  _Cdecl log10 (double __x);
double  _Cdecl modf  (double __x, double *__ipart);
double  _Cdecl pow   (double __x, double __y);
double  _Cdecl sin   (double __x);
double  _Cdecl sinh  (double __x);
double  _Cdecl sqrt  (double __x);
double  _Cdecl tan   (double __x);
double  _Cdecl tanh  (double __x);

long double _Cdecl acosl  (long double __x);
long double _Cdecl asinl  (long double __x);
long double _Cdecl atan2l (long double __x, long double __y);
long double _Cdecl atanl  (long double __x);
long double _Cdecl ceill  (long double __x);
long double _Cdecl coshl  (long double __x);
long double _Cdecl cosl   (long double __x);
long double _Cdecl expl   (long double __x);
long double _Cdecl fabsl  (long double __x);
long double _Cdecl floorl (long double __x);
long double _Cdecl fmodl  (long double __x, long double __y);
long double _Cdecl frexpl (long double __x, int *__exponent);
long double _Cdecl ldexpl (long double __x, int __exponent);
long double _Cdecl log10l (long double __x);
long double _Cdecl logl   (long double __x);
long double _Cdecl modfl  (long double __x, long double *__ipart);
long double _Cdecl powl   (long double __x, long double __y);
long double _Cdecl sinhl  (long double __x);
long double _Cdecl sinl   (long double __x);
long double _Cdecl sqrtl  (long double __x);
long double _Cdecl tanhl  (long double __x);
long double _Cdecl tanl   (long double __x);

typedef enum
{
    DOMAIN = 1,    /* argument domain error -- log (-1)        */
    SING,          /* argument singularity  -- pow (0,-2))     */
    OVERFLOW,      /* overflow range error  -- exp (1000)      */
    UNDERFLOW,     /* underflow range error -- exp (-1000)     */
    TLOSS,         /* total loss of significance -- sin(10e70) */
    PLOSS,         /* partial loss of signif. -- not used      */
    STACKFAULT     /* floating point unit stack overflow       */
}   _mexcep;

#ifdef __cplusplus
}
#endif


#if !__STDC__

struct  exception 
{
    int type;
    char   *name;
    double  arg1, arg2, retval;
};

struct  _exceptionl
{
    int type;
    char   *name;
    long double  arg1, arg2, retval;
};

#ifdef __cplusplus
extern "C" {
#endif
int     _Cdecl abs   (int __x);
double  _Cdecl atof  (const char *__s);
double  _Cdecl hypot (double __x, double __y);
long    _Cdecl labs  (long __x);
int     _Cdecl matherr (struct exception *__e);
double  _Cdecl poly  (double __x, int __degree, double *__coeffs);
double  _Cdecl pow10 (int __p);
int     _Cdecl _matherrl (struct _exceptionl *__e);

long double _Cdecl _atold (const char *__s);
long double _Cdecl hypotl (long double __x, long double __y);
long double _Cdecl polyl  (long double __x, int __degree, long double *__coeffs);
long double _Cdecl pow10l (int __p);

#ifdef __cplusplus
    /* use class complex instead of cabs in C++ */
#else
struct complex      /* as used by "cabs" function */
{
    double  x, y;
};

struct _complexl    /* as used by "cabsl" function */
{
    long double  x, y;
};

#define cabs(z)     (hypot  ((z).x, (z).y))
#define cabsl(z)    (hypotl ((z).x, (z).y))
#endif

#ifdef __cplusplus
}
#endif

/* Constants rounded for 21 decimals. */
#define M_E         2.71828182845904523536
#define M_LOG2E     1.44269504088896340736
#define M_LOG10E    0.434294481903251827651
#define M_LN2       0.693147180559945309417
#define M_LN10      2.30258509299404568402
#define M_PI        3.14159265358979323846
#define M_PI_2      1.57079632679489661923
#define M_PI_4      0.785398163397448309616
#define M_1_PI      0.318309886183790671538
#define M_2_PI      0.636619772367581343076
#define M_1_SQRTPI  0.564189583547756286948
#define M_2_SQRTPI  1.12837916709551257390
#define M_SQRT2     1.41421356237309504880
#define M_SQRT_2    0.707106781186547524401

#define EDOM    33      /* Math argument */
#define ERANGE  34      /* Result too large */

#endif  /* !__STDC__ */

#endif