Math Functions

1-minute read

There are many math functions available to us via C header files:

  • sin(angle) for finding the sine of an angle in radians
  • cos(angle) for finding the cosine of an angle in radians
  • tan(angle) for finding the tangent of an angle in radians
  • abs(number) to get the absolute value (modulus) of a number
  • floor(number) for rounding a number downwards
  • ceil(number) for rounding a number upwards
  • pow(base,exponent) for raising a base to the power of exponent

Most of these are available via math.h header file whereas abs() is in the stdlib.h header file.

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

int main(){
    double x = 1.57;
    int y = -3;

    printf("The sine of %g is %g\n",x,sin(x));
    printf("The cosine of %g is %g\n",x,cos(x));
    printf("The tangent of %g is %g\n",x,tan(x));
    printf("The absolute value of %d is %d\n",y,abs(y));
    printf("The floor/gaussian of %g is %g\n",x,floor(x));
    printf("The ceil of %g is %g\n",x,ceil(x));
    printf("%g raised to the power of %d is %g\n",x,y,pow(x,y));
}
math-functions.c
Copy

Support us via BuyMeACoffee

Resources

Here are some resources we recommend for you to use along with this lesson: