# The Math Functions in C programming

#### C Math Library Functions

Math library functions allow you to perform certain common mathematical calculations. We use some of them here to introduce the concept of functions. Later in the tutorial, we’ll discuss many of the other functions in the C standard library.

Functions are normally used in a program by writing the name of the function followed by a left parenthesis followed by the argument (or a comma-separated list of arguments) of the function followed by a right parenthesis.

To calculate and print the square root of 900.0 you might write:

``````
printf( "%.2f", sqrt( 900.0 ) );
```
```

When this statement executes, the math library function sqrt is called to calculate the square root of the number contained in the parentheses (900.0). The number 900.0 is the argument of the sqrt function.

The C11 standard adds a wide range of floating point and complex-number capabilities.

The following table shows the functions of the math library:

 Function Description Example sqrt( x ) square root of x sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0 cbrt( x ) cube root of x (C99 and C11 only) cbrt( 27.0 ) is 3.0 cbrt( -8.0 ) is -2.0 exp( x ) exponential function ex exp( 1.0 ) is 2.718282 exp( 2.0 ) is 7.389056 log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0 log( 7.389056 ) is 2.0 log10( x ) logarithm of x (base 10) log10( 1.0 ) is 0.0 log10( 10.0 ) is 1.0 log10( 100.0 ) is 2.0 fabs( x ) absolute value of x as a floating-point number fabs( 13.5 ) is 13.5 fabs( 0.0 ) is 0.0 fabs( -13.5 ) is 13.5 ceil( x ) rounds x to the smallest integer not less than x ceil( 9.2 ) is 10.0 ceil( -9.8 ) is -9.0 floor( x ) rounds x to the largest integer not greater than x floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0 pow( x, y ) x raised to power y pow( 2, 7 ) is 128.0 pow( 9, .5 ) is 3.0 fmod( x, y ) remainder of x/y as a floating-point number fmod( 13.657, 2.333 ) is 1.992 sin( x ) trigonometric sine of x (x in radians) sin( 0.0 ) is 0.0 cos( x ) trigonometric cosine of x (x in radians) cos( 0.0 ) is 1.0 tan( x ) trigonometric tangent of x (x in radians) tan( 0.0 ) is 0.0

### Example of Math Library

The C program below tests the math library functions:

``````
// Using the goto statement
#include <stdio.h>
#include <math.h>
// function main begins program execution
int main( void )  {
// calculates and outputs the square root
printf( "sqrt(%.1f) = %.1f\n", 900.0, sqrt( 900.0 ) );
printf( "sqrt(%.1f) = %.1f\n", 9.0, sqrt( 9.0 ) );

// calculates and outputs the exponential function e to the x
printf( "exp(%.1f) = %f\n", 1.0, exp( 1.0 ) );
printf( "exp(%.1f) = %f\n", 2.0, exp( 2.0 ) );

// calculates and outputs the logarithm (base e)
printf( "log(%f) = %.1f\n", 2.718282, log( 2.718282 ) );
printf( "log(%f) = %.1f\n", 7.389056, log( 7.389056 ) );

// calculates and outputs the logarithm (base 10)
printf( "log10(%.1f) = %.1f\n", 1.0, log10( 1.0 ) );
printf( "log10(%.1f) = %.1f\n", 10.0, log10( 10.0 ) );
printf( "log10(%.1f) = %.1f\n", 100.0, log10( 100.0 ) );

// calculates and outputs the absolute value
printf( "fabs(%.1f) = %.1f\n", 13.5, fabs( 13.5 ) );
printf( "fabs(%.1f) = %.1f\n", 0.0, fabs( 0.0 ) );
printf( "fabs(%.1f) = %.1f\n", -13.5, fabs( -13.5 ) );

// calculates and outputs ceil( x )
printf( "ceil(%.1f) = %.1f\n", 9.2, ceil( 9.2 ) );
printf( "ceil(%.1f) = %.1f\n", -9.8, ceil( -9.8 ) );
// calculates and outputs floor( x )
printf( "floor(%.1f) = %.1f\n", 9.2, floor( 9.2 ) );
printf( "floor(%.1f) = %.1f\n", -9.8, floor( -9.8 ) );

// calculates and outputs pow( x, y )
printf( "pow(%.1f, %.1f) = %.1f\n", 2.0, 7.0, pow( 2.0, 7.0 ) );
printf( "pow(%.1f, %.1f) = %.1f\n", 9.0, 0.5, pow( 9.0, 0.5 ) );

// calculates and outputs fmod( x, y )
printf( "fmod(%.3f/%.3f) = %.3f\n", 13.657, 2.333,
fmod( 13.657, 2.333 ) );
// calculates and outputs sin( x )
printf( "sin(%.1f) = %.1f\n", 0.0, sin( 0.0 ) );

// calculates and outputs cos( x )
printf( "cos(%.1f) = %.1f\n", 0.0, cos( 0.0 ) );

// calculates and outputs tan( x )
printf( "tan(%.1f) = %.1f\n", 0.0, tan( 0.0 ) );
} // end main
``````
``````
Output:
sqrt(900.0) = 30.0
sqrt(9.0) = 3.0
exp(1.0) = 2.718282
exp(2.0) = 7.389056
log(2.718282) = 1.0
log(7.389056) = 2.0
log10(1.0) = 0.0
log10(10.0) = 1.0
log10(100.0) = 2.0
fabs(13.5) = 13.5
fabs(0.0) = 0.0
fabs(-13.5) = 13.5
ceil(9.2) = 10.0
ceil(-9.8) = -9.0
floor(9.2) = 9.0
floor(-9.8) = -10.0
pow(2.0, 7.0) = 128.0
pow(9.0, 0.5) = 3.0
fmod(13.657/2.333) = 1.992
sin(0.0) = 0.0
cos(0.0) = 1.0
tan(0.0) = 0.0
``````