TExcelTAD
Home › C › C Math Functions

C Math Functions

C's arithmetic operators cover addition, subtraction, multiplication, division and remainder. For everything else — square roots, powers, rounding, trigonometry, logarithms — the standard library provides the header <math.h>. Most of its functions take and return double.

c
#include <math.h>

Note: On Linux, the math functions live in a separate library that you link with -lm: gcc prog.c -o prog -lm. The playgrounds on this site already add -lm for you, so feel free to change the examples to use values computed at run time. When you compile on your own machine, remember the flag.

Powers and roots

Function Returns Example Result
sqrt(x) square root sqrt(81.0) 9.0
cbrt(x) cube root cbrt(27.0) 3.0
pow(x, y) x raised to y pow(2.0, 8.0) 256.0
hypot(x, y) √(x² + y²) hypot(3.0, 4.0) 5.0
exp(x) eˣ exp(1.0) 2.718…
log(x) natural logarithm log(exp(2.0)) 2.0
log10(x) base-10 logarithm log10(1000.0) 3.0
log2(x) base-2 logarithm log2(1024.0) 10.0
c · try it yourself
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("sqrt(81)    = %.1f\n", sqrt(81.0));
    printf("cbrt(27)    = %.1f\n", cbrt(27.0));
    printf("pow(2, 10)  = %.0f\n", pow(2.0, 10.0));
    printf("pow(9, 0.5) = %.1f\n", pow(9.0, 0.5));
    printf("hypot(5,12) = %.1f\n", hypot(5.0, 12.0));
    printf("e           = %.6f\n", exp(1.0));
    printf("log10(1e6)  = %.1f\n", log10(1e6));
    printf("log2(4096)  = %.1f\n", log2(4096.0));
    return 0;
}
sqrt(81)    = 9.0
cbrt(27)    = 3.0
pow(2, 10)  = 1024
pow(9, 0.5) = 3.0
hypot(5,12) = 13.0
e           = 2.718282
log10(1e6)  = 6.0
log2(4096)  = 12.0

Tip: For small integer powers, plain multiplication (x * x, x * x * x) is simpler and exact. pow is meant for floating-point exponents.

Rounding

There are several ways to turn a decimal number into a whole one, and they differ for negative values:

Function Rule 2.5 -2.5 2.7
floor(x) round down (towards −∞) 2 −3 2
ceil(x) round up (towards +∞) 3 −2 3
round(x) nearest, halves away from zero 3 −3 3
trunc(x) drop the fraction (towards 0) 2 −2 2
(int) x same as trunc, but gives an int 2 −2 2
c · try it yourself
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%-8s %6s %6s %6s %6s\n", "x", "floor", "ceil", "round", "trunc");
    printf("%-8.2f %6.1f %6.1f %6.1f %6.1f\n", 2.5, floor(2.5), ceil(2.5), round(2.5), trunc(2.5));
    printf("%-8.2f %6.1f %6.1f %6.1f %6.1f\n", -2.5, floor(-2.5), ceil(-2.5), round(-2.5), trunc(-2.5));
    printf("%-8.2f %6.1f %6.1f %6.1f %6.1f\n", 7.49, floor(7.49), ceil(7.49), round(7.49), trunc(7.49));
    return 0;
}
x         floor   ceil  round  trunc
2.50        2.0    3.0    3.0    2.0
-2.50      -3.0   -2.0   -3.0   -2.0
7.49        7.0    8.0    7.0    7.0

A common use of ceil: how many pages do you need for 47 items at 10 per page? ceil(47 / 10.0) is 5. (With integers only, (47 + 10 - 1) / 10 gives the same answer.)

Absolute values, remainders, min and max

Function Header Purpose
abs(n) <stdlib.h> absolute value of an int
labs(n), llabs(n) <stdlib.h> for long, long long
fabs(x) <math.h> absolute value of a double
fmod(x, y) <math.h> floating-point remainder
fmin(x, y), fmax(x, y) <math.h> smaller / larger of two doubles
c · try it yourself
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {
    int balance = -350;
    printf("abs(%d) = %d\n", balance, abs(balance));
    printf("fabs(-7.25) = %.2f\n", fabs(-7.25));
    printf("fmod(10.5, 3) = %.1f\n", fmod(10.5, 3.0));
    printf("fmin(2.5, 1.5) = %.1f, fmax(2.5, 1.5) = %.1f\n", fmin(2.5, 1.5), fmax(2.5, 1.5));
    return 0;
}
abs(-350) = 350
fabs(-7.25) = 7.25
fmod(10.5, 3) = 1.5
fmin(2.5, 1.5) = 1.5, fmax(2.5, 1.5) = 2.5

abs is for integers only. Passing a double to abs silently truncates it — use fabs for floating-point values.

Trigonometry

The trigonometric functions work in radians, not degrees. To convert, multiply degrees by π/180. <math.h> on most systems defines M_PI, but it is not part of standard C, so portable code defines its own constant:

c · try it yourself
#include <stdio.h>
#include <math.h>

#define PI 3.14159265358979323846

int main(void) {
    printf("sin(30 deg) = %.4f\n", sin(30 * PI / 180));
    printf("cos(60 deg) = %.4f\n", cos(60 * PI / 180));
    printf("tan(45 deg) = %.4f\n", tan(45 * PI / 180));
    printf("atan2(1, 1) = %.4f rad = %.1f deg\n", atan2(1.0, 1.0), atan2(1.0, 1.0) * 180 / PI);
    return 0;
}
sin(30 deg) = 0.5000
cos(60 deg) = 0.5000
tan(45 deg) = 1.0000
atan2(1, 1) = 0.7854 rad = 45.0 deg

Also available: asin, acos, atan, and the hyperbolic sinh, cosh, tanh.

Special results

Invalid operations do not crash — they return special floating-point values. sqrt(-1.0) gives NaN ("not a number"), and 1.0 / 0.0 gives infinity. Use isnan(x) and isinf(x) to test for them.

Note: Each function has float and long double versions with an f or l suffix, such as sqrtf and sqrtl.

Test yourself

Which header declares sqrt and pow?

  1. <stdlib.h>
  2. <math.h>
  3. <stdio.h>
  4. <numbers.h>

What is floor(-3.2)?

  1. -3.0
  2. -4.0
  3. 3.0
  4. -3.2

What unit do sin, cos and tan expect?

  1. Degrees
  2. Radians
  3. Gradians
  4. Percent