C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

Two additional mathematical functions might come in handy, even if you don’t do heavy scientific and
math programming. The pow() function raises a value to a power, and the sqrt() function returns
the square root of a value.


Tip

You can’t compute the square root of a negative number. The fabs() function can
help ensure that you don’t try to do so by converting the number to a positive value
before you compute the square root.

Perhaps a picture will bring back fond high school algebra memories. Figure 20.1 shows the familiar
math symbols used for pow() and sqrt().


FIGURE 20.1 Looking at the math symbols for pow() and sqrt().

The following code prints the value of 10 raised to the third power and the square root of 64:


Click here to view code image


printf("10 raised to the third power is %.0f.\n", pow(10.0, 3.0));
printf("The square root of 64 is %.0f.\n", sqrt(64));

Here is the output of these printf() functions:


Click here to view code image


10 raised to the 3rd power is 1000.
The square root of 64 is 8.

Getting into Trig and Other Really Hard Stuff


Only a handful of readers will need the trigonometric and logarithmic functions. If you know you
won’t, or if you hope you won’t, go ahead and skip to the next section. Those of you who need them
now won’t require much explanation, so not much is given.


Table 20.1 explains the primary trigonometric functions. They each require an argument expressed in
radians.

Free download pdf