c/c++ The C Mathematical Functions

c/c++ The C Mathematical Functions 

Overview

C and C++ define a rich and varied set of mathematical functions. Originally, both C and C++ supported the same set of 22 math functions. However, as C++ matured, it added to these original functions. Then, C99 greatly increased the size of the C math library. The net result of these changes is that today, the C and C++ math libraries have diverged. For this reason, the C math functions (including those added by C99) are described here. Chapter 9 describes the C++ math functions. Keep in mind that the original, core set of C math functions is still fully supported by all versions of C and C++.
All the math functions require the header <math.h>. In addition to declaring the math functions, this header defines one or more macros. For C89, the only macro defined by <math.h> is HUGE_VAL, which is a double value that indicates that an overflow has occurred. C99 defines several more, including the following:
HUGE_VALF
A float version of HUGE_VAL
HUGE_VALL
A long double version of HUGE_VAL
INFINITY
A value representing infinity
math_errhandling
Contains either MATH_ERRNO and/or MATH_ERREXCEPT
MATH_ERRNO
errno used to report errors
MATH_ERREXCEPT
Floating-point exception raised to report errors
Not a Number
C99 defines several function-like macros that classify a value. They are shown here:
int fpclassify(fpval)
Returns FP_INFINITE, FP_NAN, FP_NORMAL, FP_SUBNORMAL, or FP_ZERO, depending upon the value in fpval. These macros are defined by <math.h>.
int isfinite(fpval)
Returns nonzero if fpval is finite.
int isinf(fpval)
Returns nonzero if fpval is infinite.
int isnan(fpval)
Returns nonzero if fpval is not a number.
int isnormal(fpval)
Returns nonzero if fpval is a normal value.
int signbit(fpval)
Returns nonzero if fpval is negative (i.e., its sign bit is set).
C99 defines the following comparison macros. For each, a and b must be floating-point types.
int isgreater(a, b)
Returns nonzero if a is greater than b.
int isgreaterequal(a, b)
Returns nonzero if a is greater than or equal to b.
int isless(a, b)
Returns nonzero if a is less than b.
int islessequal(a, b)
Returns nonzero if a is less than or equal to b.
int islessgreater(a, b)
Returns nonzero if a is greater than or less than b.
int isunordered(a, b)
Returns 1 if a and b are unordered relative to each other; zero is returned if a and b are ordered.

The reason for these macros is that they gracefully handle values that are not numbers, without causing a floating-point exception.
The macros EDOM and ERANGE are also used by the math functions. These macros are defined in the header <errno.h>.
Errors are handled somewhat differently between C89 and C99. For C89, if an argument to a math function is not in the domain for which it is defined, an implementation-defined value is returned, and the built-in global integer variable errno is set equal to EDOM. For C99, a domain error also causes an implementation-defined value to be returned. However, the value of math_errhandling determines what other actions take place. If math_errhandling contains MATH_ERRNO, then the built-in global integer variable errno is set equal to EDOM. If math_errhandling contains MATH_ERREXCEPT, a floating-point exception is raised.
For C89, if a function produces a result that is too large to be represented, an overflow occurs. This causes the function to return HUGE_VAL, and errno is set to ERANGE, indicating a range error. If an underflow happens, the function returns zero and sets errno to ERANGE. For C99, an overflow error also causes the function to return HUGE_VAL and an underflow also causes the function to return zero. Then, if math_errhandling contains MATH_ERRNO, errno is set to ERANGE, indicating a range error. If math_errhandling contains MATH_ERREXCEPT, a floating-point exception is raised.
In C89, the mathematical functions were specified as operating on values of type double, and returning double values. C99 added float and long double versions of these functions, which use the f and l suffixes, respectively. For example, C89 defined sin( ) as shown here:
double sin(double arg);
C99 keeps sin( ) and adds sinf( ) and sinl( ), shown next:
float sinf(float arg);
long double sinl(long double arg);
The operations of all three functions are the same, except for the data upon which they operate. The addition of the f and l math functions allows you to use the version that precisely fits the data upon which you are operating.
Since C99 has added so many new functions, it is helpful to list those functions that are supported by C89. They are shown here. These are also the math functions that C has in common with C++.
acos
asin
atan
atan2
ceil
cos
cosh
exp
fabs
floor
fmod
frexp
ldexp
log
log10
modf
pow
sin
sinh
sqrt
tan
tanh

One last point: Throughout, all angles are in radians.

acos

#include <math.h>float acosf(float arg);double acos(double arg);
long double acosl(long double arg);
 
acosf( ) and acosl( ) were added by C99.
The acos( ) family of functions returns the arc cosine of arg. The argument must be in the range –1 to 1; otherwise, a domain error will occur.
Related functions are asin( ), atan( ), atan2( ), sin( ), cos( ), tan( ), sinh( ), cosh( ), and tanh( ).

acosh

#include <math.h>float acoshf(float arg);double acosh(double arg);
long double acoshl(long double arg);
 
acosh( ), acoshf( ), and acoshl( ) were added by C99.
The acosh( ) family of functions returns the arc hyperbolic cosine of arg. The argument must be zero or greater; otherwise, a domain error will occur.
Related functions are asinh( ), atanh( ), sinh( ), cosh( ), and tanh( ).

asin

#include <math.h>float asinf(float arg);double asin(double arg); 
long double asinl(long double arg); 
 
asinf( ) and asinl( ) were added by C99.
The asin( ) family of functions returns the arc sine of arg. The argument must be in the range –1 to 1; otherwise, a domain error will occur.
Related functions are acos( ), atan( ), atan2( ), sin( ), cos( ), tan( ), sinh( ), cosh( ), and tanh( ).

asinh

#include <math.h>float asinhf(float arg);double asinh(double arg);
long double asinhl(long double arg);
 
asinh( ), asinhf( ), and asinl( ) were added by C99.
The asinh( ) family of functions returns the arc hyperbolic sine of arg.
Related functions are acosh( ), atanh( ), sinh( ), cosh( ), and tanh( ).

atan

#include <math.h>float atanf(float arg);double atan(double arg);
long double atanl(long double arg);
 
atanf( ) and atanl( ) were added by C99.
The atan( ) family of functions returns the arc tangent of arg.
Related functions are asin( ), acos( ), atan2( ), tan( ), cos( ), sin( ), sinh( ), cosh( ), and tanh( ).

atanh

#include <math.h>float atanhf(float arg);double atanh(double arg);
long double atanhl(long double arg); 
 
atanh( ), atanhf( ), and atanl( ) were added by C99.
The atanh( ) family of functions returns the arc hyperbolic tangent of arg. This argument must be in the range –1 to 1; otherwise a domain error will occur. If arg equals 1 or –1 a range error is possible.
Related functions are acosh( ), asinh( ), sinh( ), cosh( ), and tanh( ).

tan

#include <math.h>float tanf(float arg);double tan(double arg);
long double tanl(long double arg);
 
tanf( ) and tanl( ) were added by C99.
The tan( ) family of functions returns the tangent of arg. The value of arg must be in radians.
Related functions are acos( ), asin( ), atan( ), atan2( ), cos( ), sin( ), sinh( ), cosh( ), and tanh( 

tanh

#include <math.h>float tanhf(float arg);double tanh(double arg);
long double tanhl(long double arg); 
 
tanhf( ) and tanhl( ) were added by C99.
The tanh( ) family of functions returns the hyperbolic tangent of arg.
Related functions are acos( ), asin( ), atan( ), atan2( ), cos( ), sin( ), cosh( ), sinh( ), and tan( ).

tgamma

#include <math.h>float tgammaf(float arg);double tgamma(double arg);
long double tgammal(long double arg);
 
tgamma( ), tgammaf( ), and tgammal( ) were added by C99.
The tgamma( ) family of functions returns the gamma function of arg.
A related function is lgamma( ).

trunc

#include <math.h>float truncf(float arg);double trunc(double arg);
long double truncl(long double arg);
trunc( ), truncf( ), and truncl( ) were added by C99.
The trunc( ) family of functions returns the truncated value of arg.
A related function is nearbyint( ).

Followers