When using the math library, we need to include the math header file as illustrated in Part 1-1.
#include <math.h>This header file give us the prototypes for the functions in the math library. (We'll see how this is done in Part 2-7.) These prototypes tell us that most of the functions in the math library take arguments of type
doubleand also return double precision floating point numbers. Among the prototypes in the file
math.hare the following math functions:
sin( 1.57 )will return a number about 1.0.
Even though the math functions are defined to use double precision numbers, C's type handling will do what you expect if you use single precision floating point numbers as arguments or assign the return values to single precision numbers. For example,
float x, y ; y = sqrt( x ) ;will work perfectly well. However, for reasons of performance and computational accuracy it is usually advisable to go ahead and declare such variables as double precision.
As long as we're looking at using double precision numbers so extensively, we should learn how to read them in and write them out. The easy part is printing doubles. We do the same thing using
printf()as we do for single precision floats. Specifically,
double x ; ... printf( "%f", x ) ;is the proper way to print out double precision floating point numbers. However, the same is not true of
scanf(). Here, we have a different format specifier for the double precision numbers. To read in such a number, we'd use code like:
double x ; ... scanf( "%lf", &x ) ;Notice that the format specifier for reading doubles is
%lf. This specifier can also be used when printing doubles, but it is not necessary.
By way of review, let us note that if we have a right triangle, the cosine of the angle between the hypotenuse and a side is equal to the ratio of the length of the adjacent side to the length of the hypotenuse. So if we have this ratio, then we can find the angle by using the arc cosine (
acos()) function. We can also find the other angle by applying this procedure to the ratio of the other side to the hypotenuse.
Write a C expression (not the whole statement) to compute
the angle given the lengths of the two sides stored in