Reference: math.h
Roots, powers, rounding, logs, and trigonometry.
5 minute read
Big idea
<math.h> holds the floating-point maths: roots, powers, rounding, logs, and trigonometry. Almost everything takes and returns double; add f to a name for the float version (sqrtf) and l for long double.
On Linux, link with -lm when you compile by hand: gcc main.c -lm.
Roots and powers
sqrt / cbrt
double sqrt(double x);
double cbrt(double x);
Square root and cube root.
- Gotcha:
sqrtof a negative number returns NaN ("not a number"), which then spreads through every calculation it touches. - Example:
sqrt(2)→ 1.414214
pow
double pow(double base, double exp);
base to the power exp.
- Gotcha: it's a double —
(int) pow(10, 2)is fine for small values, but for integer powers in a loop, multiplying is exact and faster.^in C is XOR, not power. - Example:
pow(2, 0.5)→ 1.414214
hypot
double hypot(double x, double y);
sqrt(x*x + y*y) without overflowing on big values — distance from the origin.
- Example:
hypot(3, 4)→ 5
exp / log / log10 / log2
double exp(double x);
double log(double x); /* natural log, base e */
double log10(double x);
double log2(double x);
- Gotcha:
logis the natural log, not base 10.log(0)is minus infinity;logof a negative is NaN. - Example:
log10(1000)→ 3
Rounding and absolute value
floor / ceil / round / trunc
double floor(double x); /* down, towards -infinity */
double ceil(double x); /* up, towards +infinity */
double round(double x); /* nearest; halves away from zero */
double trunc(double x); /* towards zero: drops the fraction */
| x | floor | ceil | round | trunc | (int) x |
|---|---|---|---|---|---|
| 2.5 | 2 | 3 | 3 | 2 | 2 |
| -2.5 | -3 | -2 | -3 | -2 | -2 |
| 2.4 | 2 | 3 | 2 | 2 | 2 |
- Gotcha: they still return a
double; cast if you need an int:(int) round(x). A plain(int)cast truncates.
fabs
double fabs(double x);
Absolute value of a double. (abs for ints lives in <stdlib.h>; using it on a double drops the fraction.)
fmod
double fmod(double x, double y);
The remainder of x / y for decimals — % only works on integers.
- Example:
fmod(7.5, 2)→ 1.5
fmin / fmax
double fmin(double a, double b);
double fmax(double a, double b);
The smaller / larger of two doubles.
Trigonometry
double sin(double x); double cos(double x); double tan(double x);
double asin(double x); double acos(double x); double atan(double x);
double atan2(double y, double x);
- Gotcha: angles are in radians, not degrees:
radians = degrees * M_PI / 180.M_PIisn't strictly standard; define your own#define PI 3.14159265358979if it's missing. atan2(y, x)gives the angle of the point(x, y)in the right quadrant — prefer it toatan(y / x).- Example:
sin(PI / 6)→ 0.5
Special values
INFINITY,NAN— infinity and not-a-number.isnan(x),isinf(x),isfinite(x)— test for them. (x == NANis always false, even whenxis NaN.)HUGE_VAL— returned on overflow.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979
int main(void) {
printf("%.3f %.1f %.1f\n", sqrt(2), pow(2, 10), cbrt(27));
printf("%.1f %.1f %.1f %.1f\n", floor(-2.5), ceil(-2.5), round(-2.5), trunc(-2.5));
printf("%.1f %.1f %.1f\n", fabs(-4.5), fmod(7.5, 2), hypot(5, 12));
printf("%.1f %.1f\n", log10(1000), log2(8));
printf("%.2f %.2f\n", sin(PI / 6), cos(0));
printf("%.1f degrees\n", atan2(1, 1) * 180 / PI);
printf("%d\n", isnan(sqrt(-1)) != 0);
return 0;
}output
1.414 1024.0 3.0
-3.0 -2.0 -3.0 -2.0
4.5 1.5 13.0
3.0 3.0
0.50 1.00
45.0 degrees
1Run and edit it in the app.
In C, how do you compute 2 to the power 8?
Check yourself
What does log(x) compute in C?
sin and cos take their angle in…