C Library math.h Functions
The math.h header defines various C mathematical functions and one macro. All the functions available in this library take double as an argument and return double as the result. Let us discuss some important C math functions one by one.
C Math Functions
1. double ceil (double x)
The C library function double ceil (double x) returns the smallest integer value greater than or equal to x.
Syntax
double ceil(double x);
Example
// C code to illustrate
// the use of ceil function.
#include <math.h>
#include <stdio.h>
int main()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = 1.2;
val3 = -2.8;
val4 = -2.3;
printf("value1 = %.1lf\n", ceil(val1));
printf("value2 = %.1lf\n", ceil(val2));
printf("value3 = %.1lf\n", ceil(val3));
printf("value4 = %.1lf\n", ceil(val4));
return (0);
}
Output
value1 = 2.0 value2 = 2.0 value3 = -2.0 value4 = -2.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);
Example
// C code to illustrate
// the use of floor function
#include <math.h>
#include <stdio.h>
int main()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = 1.2;
val3 = -2.8;
val4 = -2.3;
printf("Value1 = %.1lf\n", floor(val1));
printf("Value2 = %.1lf\n", floor(val2));
printf("Value3 = %.1lf\n", floor(val3));
printf("Value4 = %.1lf\n", floor(val4));
return (0);
}
Output
Value1 = 1.0 Value2 = 1.0 Value3 = -3.0 Value4 = -3.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)
Example
// C code to illustrate
// the use of fabs function
#include <math.h>
#include <stdio.h>
int main()
{
int a, b;
a = 1234;
b = -344;
printf("The absolute value of %d is %lf\n", a, fabs(a));
printf("The absolute value of %d is %lf\n", b, fabs(b));
return (0);
}
Output
The absolute value of 1234 is 1234.000000 The absolute value of -344 is 344.000000
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)
Example
// C code to illustrate
// the use of log function
#include <math.h>
#include <stdio.h>
int main()
{
double x, ret;
x = 2.7;
/* finding log(2.7) */
ret = log(x);
printf("log(%lf) = %lf", x, ret);
return (0);
}
Output
log(2.700000) = 0.993252
5. double log10(double x)
The C library function double log10(double x) returns the common logarithm (base-10 logarithm) of x.
Syntax
double log10(double x);
Example
// C code to illustrate
// the use of log10 function
#include <math.h>
#include <stdio.h>
int main()
{
double x, ret;
x = 10000;
/* finding value of log1010000 */
ret = log10(x);
printf("log10(%lf) = %lf\n", x, ret);
return (0);
}
Output
log10(10000.000000) = 4.000000
6. double fmod(double x, double y)
The C library function double fmod(double x, double y) returns the remainder of x divided by y.
Syntax
double fmod(double x, double y)
Example
// C code to illustrate
// the use of fmod function
#include <math.h>
#include <stdio.h>
int main()
{
float a, b;
int c;
a = 8.2;
b = 5.7;
c = 3;
printf("Remainder of %f / %d is %lf\n", a, c,
fmod(a, c));
printf("Remainder of %f / %f is %lf\n", a, b,
fmod(a, b));
return (0);
}
Output
Remainder of 8.200000 / 3 is 2.200000 Remainder of 8.200000 / 5.700000 is 2.500000
7. double sqrt(double x)
The C library function double sqrt(double x) returns the square root of x.
Syntax
double sqrt(double x);
Example
// C code to illustrate
// the use of sqrt function
#include <math.h>
#include <stdio.h>
int main()
{
printf("Square root of %lf is %lf\n", 225.0,
sqrt(225.0));
printf("Square root of %lf is %lf\n", 300.0,
sqrt(300.0));
return (0);
}
Output
Square root of 225.000000 is 15.000000 Square root of 300.000000 is 17.320508
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
double pow(double x, double y);
Example
// C code to illustrate
// the use of pow function
#include <math.h>
#include <stdio.h>
int main()
{
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);
}
Output
Value 8.0 ^ 3 = 512.000000 Value 3.05 ^ 1.98 = 9.097324
9. double modf(double x, double *integer)
The C library function double modf(double x, double *integer) returns the fraction component (part after the decimal), and sets integer to the integer component.
Syntax
double modf(double x, double *integer)
Example
// C code to illustrate
// the use of modf function
#include <math.h>
#include <stdio.h>
int main()
{
double x, fractpart, intpart;
x = 8.123456;
fractpart = modf(x, &intpart);
printf("Integral part = %lf\n", intpart);
printf("Fraction Part = %lf \n", fractpart);
return (0);
}
Output
Integral part = 8.000000 Fraction Part = 0.123456
10. double exp(double x)
The C library function double exp(double x) returns the value of e raised to the xth power.
Syntax
double exp(double x);
Example
// C code to illustrate
// the use of exp function
#include <math.h>
#include <stdio.h>
int main()
{
double x = 0;
printf("The exponential value of %lf is %lf\n", x,
exp(x));
printf("The exponential value of %lf is %lf\n", x + 1,
exp(x + 1));
printf("The exponential value of %lf is %lf\n", x + 2,
exp(x + 2));
return (0);
}
Output
The exponential value of 0.000000 is 1.000000 The exponential value of 1.000000 is 2.718282 The exponential value of 2.000000 is 7.389056
11. double cos(double x)
The C library function double cos(double x) returns the cosine of a radian angle x.
Syntax
double cos(double x);
The same syntax can be used for other trigonometric functions like sin, tan, etc.
Example
// C code to illustrate
// the use of cos function
#include <math.h>
#include <stdio.h>
#define PI 3.14159265
int main()
{
double x, ret, val;
x = 60.0;
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);
}
Output
The cosine of 60.000000 is 0.500000 degrees The cosine of 90.000000 is 0.000000 degrees
12. double acos(double x)
The C library function double acos(double x) returns the arc cosine of x in radians.
Syntax
double acos(double x);
The same syntax can be used for other arc trigonometric functions like asin, atan etc.
Example
// C code to illustrate
// the use of acos function
#include <math.h>
#include <stdio.h>
#define PI 3.14159265
int main()
{
double x, ret, val;
x = 0.9;
val = 180.0 / PI;
ret = acos(x) * val;
printf("The arc cosine of %lf is %lf degrees", x, ret);
return (0);
}
Output
The arc cosine of 0.900000 is 25.841933 degrees
13. double tanh(double x)
The C library function double tanh(double x) returns the hyperbolic tangent of x.
Syntax
double tanh(double x);
The same syntax can be used for other hyperbolic trigonometric functions like sinh, cosh etc.
Example
// C code to illustrate
// the use of tanh function
#include <math.h>
#include <stdio.h>
int main()
{
double x, ret;
x = 0.5;
ret = tanh(x);
printf("The hyperbolic tangent of %lf is %lf degrees",
x, ret);
return (0);
}
Output
The hyperbolic tangent of 0.500000 is 0.462117 degrees