Previous: 8.1.1 Character Processing Routines
Up: 8.1 The C Standard Library
Previous Page: 8.1.1 Character Processing Routines
Next Page: 8.2 Formatted Input/Output

8.1.2 Math Routines

There are many mathematical routines in the standard library, such as abs(), pow(), sqrt(), rand(), sin(), cos(), tan(), and so forth. The prototypes for these are defined in the header file, < math.h>, which must be included whenever these functions are used in a program. In addition, on Unix systems, the math library is maintained separately from the standard library, thus requiring that it be linked when the code is compiled. This can be done with the compiler command:

cc  filename.c -lm
The option -l specifies that a library must be linked with the code and the m specifies the math library. Note that this option MUST appear as the last item on the command line.

Most of the functions listed above are self explanatory (and are described in detail in Appendix ). As an example, let us look at the function, rand() which generates pseudo-random integers in the range of numbers from 0 to the largest positive integer value. The numbers cannot be completely random because the range is limited. However, for the most part, the numbers generated by rand() appear to be quite random. The prototype for the function is:

int rand(void);
Each time the function is called, it returns a random integer number. Figure 8.2 shows an example which generates and prints some random numbers.

Sample Session:

The random number generator will always start with the same number unless it is ``seeded'' first by calling the function, srand(). The prototype for it is:
void srand(unsigned x);
In the example in Figure 8.3, we seed the random number generator with a user supplied number. The program then finds random throws for a single dice. After the random generator is seeded, every random number generated, n, is evaluated modulo 6, i.e. n % 6 is evaluated. This results in numbers from 0 to 5. We add one to obtain the dice throws from 1 to 6.

Sample Session:

Similarly, we can write a program that draws a card from a full deck of 52 cards as shown in Figure 8.4. It starts by seeding the random number generator before its use. Next, a random number is generated and evaluated modulo 52, resulting in a random number between 0 and 51, representing a card. For a number, n, the value n / 13 is in the range 0 through 3, each corresponding to a suit: say 0 is clubs, 1 is diamonds, 2 is hearts, and 3 is spades. In addition, n % 13 + 1 evaluates to a number in the range 1 through 13, corresponding to a card in a suit: say 1 is ace, 2 is deuce, ..., 11 is jack, 12 is queen, and 13 is king.

Sample Session:

The next program uses the library function, sqrt(), to obtain square roots of randomly generated numbers. The function, sqrt(), requires its argument to be of type double, and it returns type double. In the program shown in Figure 8.5, the randomly generated whole number is assigned to a double variable before finding its square root.

Sample Session:

  • ***Square Root Program - Random Numbers***
  • Sq.Rt. of 346.000000 is 18.601075
  • Sq.Rt. of 130.000000 is 11.401754
  • Sq.Rt. of 10982.000000 is 104.795038
  • Sq.Rt. of 1090.000000 is 33.015148
  • Sq.Rt. of 11656.000000 is 107.962957

These have been just a few examples of using routines available in the math library. A complete listing of math routines is provided in Appendix . Rather than writing our own functions all the time, we will make use of library functions in our code wherever we can in the future.



Previous: 8.1.1 Character Processing Routines
Up: 8.1 The C Standard Library
Previous Page: 8.1.1 Character Processing Routines
Next Page: 8.2 Formatted Input/Output

tep@wiliki.eng.hawaii.edu
Wed Aug 17 09:15:23 HST 1994