7.4 Example

Example 7-1 shows how the tan, sin, and cos functions operate.

Example 7-1 Calculating and Verifying a Tangent Value

/*  This example uses two functions --- mytan and main ---     *
 *  to calculate the tangent value of a number, and to check   *
 *  the calculation using the sin and cos functions.           */

#include <math.h>
#include <stdio.h>

/*  This function is used to calculate the tangent using the   *
 *  sin and cos functions.                                     */

double mytan(x)
double x;
{
   double  y, y1, y2;

   y1 = sin (x);
   y2 = cos (x);

   if (y2 == 0)
      y = 0;
   else
      y = y1 / y2;

   return y;
}
main()
{
   double  x;
                                   /*  Print values: compare   */
   for (x=0.0; x<1.5; x += 0.1)
      printf("tan of %4.1f = %6.2f\t%6.2f\n", x, mytan(x), tan(x));
}

Example 7-1 produces the following output:

$ RUN  EXAMPLE
tan of  0.0 =   0.00      0.00
tan of  0.1 =   0.10      0.10
tan of  0.2 =   0.20      0.20
tan of  0.3 =   0.31      0.31
tan of  0.4 =   0.42      0.42
tan of  0.5 =   0.55      0.55
tan of  0.6 =   0.68      0.68
tan of  0.7 =   0.84      0.84
tan of  0.8 =   1.03      1.03
tan of  0.9 =   1.26      1.26
tan of  1.0 =   1.56      1.56
tan of  1.1 =   1.96      1.96
tan of  1.2 =   2.57      2.57
tan of  1.3 =   3.60      3.60
tan of  1.4 =   5.80      5.80
$


Previous Page | Next Page | Table of Contents | Index