Mathematical Functions in C
Mathematical Functions in C
MODULE 1
C MATH
All the functions available in this library take double as an argument and return double as the result.
The <math.h> header file contains various methods for performing mathematical operations such as
2) floor(number) rounds down the given number. It returns the integer value
which is less than or equal to given number.
Syntax
double ceil(double x);
// C code to illustrate the use of ceil function.
#include <math.h>
#include <stdio.h>
return (0);
}
2. double floor(double x)
The C library function double floor(double x) returns the largest integer value less than or equal
to x.
Syntax
double floor(double x);
// C code to illustrate the use of floor function
#include <math.h>
#include <stdio.h>
int main()
{ Output
float val1, val2, val3, val4; Value1 = 1.0
return (0);
}
3. double fabs(double x)
The C library function double fabs(double x) returns the absolute value of x.
Syntax
syntax : double fabs(double x)
// C code to illustrate the use of fabs function
#include <math.h>
#include <stdio.h>
return (0);
}
4. double log(double x)
The C library function double log(double x) returns the natural logarithm (base-e logarithm) of x.
Syntax
double log(double x)
// C code to illustrate the use of log function
#include <math.h>
#include <stdio.h>
/* finding log(2.7) */
ret = log(x);
printf("log(%lf) = %lf", x, ret);
return (0);
}
5. double log10(double x)
The C library function double log10(double x) returns the common logarithm (base-10 logarithm) of x.
Syntax
The C library function double fmod(double x, double y) returns the remainder of x divided by y.
Syntax
int main()
{
float a, b; Output
int c;
a = 8.2;
b = 5.7; Remainder of 8.200000 / 3 is 2.200000
c = 3;
printf("Remainder of %f / %d is %lf\n", a, c, Remainder of 8.200000 / 5.700000 is 2.500000
fmod(a, c));
printf("Remainder of %f / %f is %lf\n", a, b,
fmod(a, b));
return (0);
}
7. double sqrt(double x)
Syntax
#include <math.h>
#include <stdio.h>
int main()
Output
{
Square root of 225.000000 is 15.000000
printf("Square root of %lf is %lf\n", 225.0, sqrt(225.0)); Square root of 300.000000 is 17.320508
return (0);
}
8. double pow(double x, double y)
The C library function double pow(double x, double y) returns x raised to the power of y i.e. xy.
Syntax
Output
int main()
Value 8.0 ^ 3 = 512.000000
{
Value 3.05 ^ 1.98 = 9.097324
printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));
printf("Value 3.05 ^ 1.98 = %lf", pow(3.05,
1.98));
return (0);
}
9. double modf(double x, double *integer)
The C library function double modf(double x, double *integer) returns the fraction component (part after the
Syntax
int main()
{ Output
double x, fractpart, intpart;
Integral part = 8.000000
x = 8.123456;
fractpart = modf(x, &intpart); Fraction Part = 0.123456
return (0);
}
10. double exp(double x)
The C library function double exp(double x) returns the value of e raised to the x power.
th
Syntax
The C library function double exp(double x) returns the value of e raised to the x power.
th
Syntax
The C library function double cos(double x) returns the cosine of a radian angle x.
Syntax
The same syntax can be used for other trigonometric functions like sin, tan, etc.
// C code to illustrate the use of cos function
#include <math.h>
#include <stdio.h>
#define PI 3.14159265
int main()
Output
{
double x, ret, val;
The cosine of 60.000000 is 0.500000 degrees
x = 60.0;
The cosine of 90.000000 is 0.000000 degrees
val = PI / 180.0;
ret = cos(x * val);
printf("The cosine of %lf is %lf degrees\n", x, ret);
x = 90.0;
val = PI / 180.0;
ret = cos(x * val);
printf("The cosine of %lf is %lf degrees\n", x, ret);
return (0);
}
12. double acos(double x)
The C library function double acos(double x) returns the arc cosine of x in radians.
Syntax
The same syntax can be used for other arc trigonometric functions like asin, atan etc.
// C code to illustrate
// the use of acos function
#include <math.h>
#include <stdio.h>
x = 0.9;
val = 180.0 / PI;
return (0);
}
13. double tanh(double x)
Syntax
The same syntax can be used for other hyperbolic trigonometric functions like sinh, cosh etc.
// C code to illustrate the use of tanh function
#include <math.h>
#include <stdio.h>
int main()
{ Output
double x, ret;
x = 0.5; The hyperbolic tangent of 0.500000 is 0.462117 degrees
ret = tanh(x);
printf("The hyperbolic tangent of %lf is %lf
degrees",
x, ret);
return (0);
}
C MATH EXAMPLE
Output:
#include<stdio.h> printf("\n%f",sqrt(16));
4.000000
#include <math.h> printf("\n%f",sqrt(7));
4.000000
int main(){ printf("\n%f",pow(2,4));
3.000000
printf("\n%f",ceil(3.6)); printf("\n%f",pow(3,3)); 3.000000
printf("\n%f",ceil(3.3));
printf("\n%d",abs(-12)); 4.000000
printf("\n%f",floor(3.6));
return 0; 2.645751
printf("\n%f",floor(3.2));
} 16.000000
27.000000
12