BlogsDope image BlogsDope

Important functions in math.h library of C

May 31, 2017 C EXAMPLE FUNCTION 7762
Download Our App.
BlogsDope App
Get it on Google Play

This post lists the important functions available in the “math.h” library of C with their examples. Before continuing, you must know that we need to include the “math.h” library while compiling our program by using -lm (cc -c filename.c cc -o filename filename.c -lm).

Function Description Example
sqrt(x) square root of x sqrt(4.0) is 2.0
sqrt(10.0) is 3.162278
exp(x) exponential (ex) exp(1.0) is 2.718282
exp(4.0) is 54.598150
log(x) natural logarithm of x (base e) log(2.0) is 0.693147
log(4.0) is 1.386294
log10(x) logarithm of x (base 10) log10(10.0) is 1.0
log10(100.0) is 2.0
fabs(x) absolute value of x fabs(2.0) is 2.0
fabs(-2.0) is 2.0
ceil(x) rounds x to smallest integer not less than x ceil(9.2) is 10.0
ceil(-9.2) is -9.0
floor(x) rounds x to largest integer not greater than x floor(9.2) is 9.0
floor(-9.2) is -10.0
pow(x,y) x raised to power y (xy) pow(2,2) is 4.0
fmod(x) remainder of x/y as floating-point number fmod(13.657, 2.333) is 1.992
sin(x) sine of x (x in radian) sin(0.0) is 0.0
cos(x) cosine of x (x in radian) cos(0.0) is 1.0
tan(x) tangent of x (x in radian) tan(0.0) is 0.0

Examples


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

int main()
{
    printf("%f\n",sqrt(10.0));
    printf("%f\n",exp(4.0));
    printf("%f\n",log(4.0));
    printf("%f\n",log10(100.0));
    printf("%f\n",fabs(-5.2));
    printf("%f\n",ceil(4.5));
    printf("%f\n",floor(-4.5));
    printf("%f\n",pow(4.0,.5));
    printf("%f\n",fmod(4.5,2.0));
    printf("%f\n",sin(0.0));
    printf("%f\n",cos(0.0));
    printf("%f\n",tan(0.0));
    return 0;
}

Output

3.162278
54.598150
1.386294
2.000000
5.200000
5.000000
-5.000000
2.000000
0.500000
0.000000
1.000000
0.000000

 


Liked the post?
Developer and founder of CodesDope.
Editor's Picks
1 COMMENT

Please login to view or add comment(s).