What are some key math functions in C?

C programming has numerous functions. These functions are used to operate on floats.

Some functions in math.h

  1. tan(), cos(), sin()- these functions are standard trigonometric functions that all work in radius.

  2. sqrt()- this function is used to determine the square root of the given value.

  3. pow(x,y)- this function returns the value xyx^{y}.

  4. fabs()- this function returns an absolute value.

  5. log(), exp()- these functions provides a natural logarithm and a natural exponent.

  6. log10() - this functions denotes the base 10 logarithm.

Some functions in stdlib.h

  1. abs(), labs() -these functions return the value of an int and a long.

  2. div(x,y)- it returns the structure with the division and remainder of x/y for int and a long.

Code

Let’s see a example program of how the sqrt(), pow(), abs() functions work:

#include<stdio.h>
#include <math.h>
#include<stdlib.h>
int main()
{
printf("%f",sqrt(25));
printf("%f",sqrt(7));
printf("%f",pow(2,5));
printf("%f",pow(3,2));
printf("%d",abs(-12));
return 0;
}

Let’s see a program that illustrates the fabs() function:

#include <stdio.h>
#include <math.h>
int main ()
{
int h, s;
h = 4321;
s = -443;
printf("The absolute value of %d is %lf\n", h, fabs(h));
printf("The absolute value of %d is %lf\n", s, fabs(s));
return(0);
}

Free Resources