# Math Library Functions

As you know, a class can provide member functions that perform the services of the class. For example, in Chapters 35, you have called the member functions of various versions of a GradeBook object to display the GradeBook's welcome message, to set its course name, to obtain a set of grades and to calculate the average of those grades.

Sometimes functions are not members of a class. Such functions are called global functions. Like a class's member functions, the function prototypes for global functions are placed in header files, so that the global functions can be reused in any program that includes the header file and that can link to the function's object code. For example, recall that we used function pow of the header file to raise a value to a power in Figure 5.6. We introduce various functions from the header file here to present the concept of global functions that do not belong to a particular class. In this chapter and in subsequent chapters, we use a combination of global functions (such as main) and classes with member functions to implement our example programs.

The header file provides a collection of functions that enable you to perform common mathematical calculations. For example, you can calculate the square root of 900.0 with the function call

```sqrt( 900.0 )
```

The preceding expression evaluates to 30.0. Function sqrt takes an argument of type double and returns a double result. Note that there is no need to create any objects before calling function sqrt. Also note that all functions in the header file are global functionstherefore, each is called simply by specifying the name of the function followed by parentheses containing the function's arguments.

Function arguments may be constants, variables or more complex expressions. If c = 13.0, d = 3.0 and f = 4.0, then the statement

```cout << sqrt( c + d * f ) << endl;
```

calculates and prints the square root of 13.0 + 3.0 * 4.0 = 25.0namely, 5.0. Some math library functions are summarized in Fig. 6.2. In the figure, the variables x and y are of type double.

Figure 6.2. Math library functions.

Function

Description

Example

ceil( x )

rounds x to the smallest integer not less than x

ceil( 9.2 ) is 10.0

ceil( -9.8 ) is -9.0

cos( x )

trigonometric cosine of x (x in radians)

cos( 0.0 ) is 1.0

exp( x )

exponential function ex

exp( 1.0 ) is 2.71828

exp( 2.0 ) is 7.38906

fabs( x )

absolute value of x

fabs( 5.1 ) is 5.1

fabs( 0.0 ) is 0.0

fabs( -8.76 ) is 8.76

floor( x )

rounds x to the largest integer not greater than x

floor( 9.2 ) is 9.0

floor( -9.8 ) is -10.0

fmod( x, y )

remainder of x/y as a floating-point number

fmod( 2.6, 1.2 ) is 0.2

log( x )

natural logarithm of x (base e)

log( 2.718282 ) is 1.0

log( 7.389056 ) is 2.0

log10( x )

logarithm of x (base 10)

log10( 10.0 ) is 1.0

log10( 100.0 ) is 2.0

pow( x, y )

x raised to power y ( xy )

pow( 2, 7 ) is 128

pow( 9, .5 ) is 3

sin( x )

trigonometric sine of x (x in radians)

sin( 0.0 ) is 0

sqrt( x )

square root of x (where x is a nonnegative value)

sqrt( 9.0 ) is 3.0

tan( x )

trigonometric tangent of x (x in radians)

tan( 0.0 ) is 0 C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Similar book on Amazon 