Monday, April 1, 2013

Math functions

A program showing the results of a few of the most popular functions of the math library

/* Functions from the math library */
#include <stdio.h> #include <math.h> #define PI 3.1416 int main (void) { double before, after, x, y, z; /* the ceil function */ before = -217.5; after = ceil (before); printf ("The ceiling of %3.1lf is %3.1lf\n", before, after); /* the floor function */ before = -217.5; after = floor (before); printf ("The floor of %3.1lf is %3.1lf\n", before, after); /* the log function */ before = 200.0; after = log (before); printf ("The ln of %3.1lf is %3.1lf\n", before, after); /* the log10 function*/ before = 200.0; after = log10 (before); printf ("The log of %3.1lf is %3.1lf\n", before, after); /* the sqrt function*/ before = 200.0; after = sqrt (before); printf ("The square root of %3.1lf is %3.1lf\n", before, after); /* the fabs function (for doubles)*/ before = -413.56; after = fabs (before); printf ("The absolute value of %3.1lf is %3.1lf\n", before, after); /* the sin function */ before = 45.0; after = sin (before * PI / 180); printf ("The sine of %3.1lf is %5.3lf\n", before, after); /* the cos function */ before = 45.0; after = cos (before * PI / 180); printf ("The cosine of %3.1lf is %5.3lf\n", before, after); /* the tan function */ before = 45.0; after = tan (before * PI / 180); printf ("The tangent of %3.1lf is %5.3lf\n", before, after); /* the exp function */ before = 10.0; after = exp (before); printf ("e to the power of %3.1lf is %5.3lf\n", before, after); /* the pow function */ x = 9.0; y = 3.0; z = pow (x, y); printf ("%3.1lf to the power of %3.1lf is %3.1lf\n", x, y, z); return (0); }
Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments
The ceiling of -217.5 is -217.0
The floor of -217.5 is -218.0
The ln of 200.0 is 5.3
The log of 200.0 is 2.3
The square root of 200.0 is 14.1
The absolute value of -413.6 is 413.6
The sine of 45.0 is 0.707
The cosine of 45.0 is 0.707
The tangent of 45.0 is 1.000
e to the power of 10.0 is 22026.466
9.0 to the power of 3.0 is 729.0

1 comment: