C - Math Functions
C provides a set of standard math functions in the math.h
library for performing common mathematical operations like square root, power, trigonometry, and more.
Common Math Functions
abs(x)
- Absolute value (int)fabs(x)
- Absolute value (float/double)ceil(x)
- Smallest integer not less than xfloor(x)
- Largest integer not greater than xsqrt(x)
- Square rootcbrt(x)
- Cube rootpow(x, y)
- x raised to the power yexp(x)
- Exponential function e^xlog(x)
- Natural logarithm (base e)log10(x)
- Logarithm base 10sin(x)
- Sine (x in radians)cos(x)
- Cosine (x in radians)tan(x)
- Tangent (x in radians)asin(x)
- Arc sine (result in radians)acos(x)
- Arc cosine (result in radians)atan(x)
- Arc tangent (result in radians)sinh(x)
- Hyperbolic sinecosh(x)
- Hyperbolic cosinetanh(x)
- Hyperbolic tangentfmod(x, y)
- Floating-point remainder of x/ymodf(x, &intpart)
- Splits x into integer and fractional partsfrexp(x, &exp)
- Breaks x into normalized fraction and exponentldexp(x, exp)
- Multiplies x by 2 raised to exphypot(x, y)
- Square root of (x*x + y*y)round(x)
- Rounds x to nearest integertrunc(x)
- Truncates x to integer
Example
#include <stdio.h>
#include <math.h>
int main() {
printf("abs(-5) = %d\\n", abs(-5));
printf("fabs(-5.5) = %.2f\\n", fabs(-5.5));
printf("ceil(2.3) = %.2f\\n", ceil(2.3));
printf("floor(2.7) = %.2f\\n", floor(2.7));
printf("sqrt(16) = %.2f\\n", sqrt(16));
printf("cbrt(27) = %.2f\\n", cbrt(27));
printf("pow(2, 3) = %.2f\\n", pow(2, 3));
printf("exp(1) = %.2f\\n", exp(1));
printf("log(2.71828) = %.2f\\n", log(2.71828));
printf("log10(100) = %.2f\\n", log10(100));
printf("sin(0) = %.2f\\n", sin(0));
printf("cos(0) = %.2f\\n", cos(0));
printf("tan(0) = %.2f\\n", tan(0));
printf("asin(1) = %.2f\\n", asin(1));
printf("acos(1) = %.2f\\n", acos(1));
printf("atan(1) = %.2f\\n", atan(1));
printf("sinh(0) = %.2f\\n", sinh(0));
printf("cosh(0) = %.2f\\n", cosh(0));
printf("tanh(0) = %.2f\\n", tanh(0));
printf("fmod(5.3, 2) = %.2f\\n", fmod(5.3, 2));
double intpart, fracpart;
fracpart = modf(3.14, &intpart);
printf("modf(3.14): intpart = %.2f, fracpart = %.2f\\n", intpart, fracpart);
int exp;
double frac = frexp(8.0, &exp);
printf("frexp(8.0): frac = %.2f, exp = %d\\n", frac, exp);
printf("ldexp(0.5, 4) = %.2f\\n", ldexp(0.5, 4));
printf("hypot(3, 4) = %.2f\\n", hypot(3, 4));
printf("round(2.5) = %.2f\\n", round(2.5));
printf("trunc(2.9) = %.2f\\n", trunc(2.9));
return 0;
}
Output (partial):
abs(-5) = 5
fabs(-5.5) = 5.50
ceil(2.3) = 3.00
floor(2.7) = 2.00
sqrt(16) = 4.00
cbrt(27) = 3.00
pow(2, 3) = 8.00
exp(1) = 2.72
log(2.71828) = 1.00
log10(100) = 2.00
sin(0) = 0.00
cos(0) = 1.00
tan(0) = 0.00
asin(1) = 1.57
acos(1) = 0.00
atan(1) = 0.79
sinh(0) = 0.00
cosh(0) = 1.00
tanh(0) = 0.00
fmod(5.3, 2) = 1.30
modf(3.14): intpart = 3.00, fracpart = 0.14
frexp(8.0): frac = 0.50, exp = 4
ldexp(0.5, 4) = 8.00
hypot(3, 4) = 5.00
round(2.5) = 3.00
trunc(2.9) = 2.00
Compile tip
On many systems you must link with the math library: gcc prog.c -o prog -lm
.
Summary
Use math.h
for advanced mathematical operations in C. Prefer double
for precision, and be mindful of floating-point rounding.