acos

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

NAN

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.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net