Chapter 29Beginner
C math.h Library Reference
Complete reference for math.h - power, roots, trigonometry, logarithms, and rounding functions.
15 min readUpdated 2024-12-16
math.hpowsqrtsincoslogceilfloor
What You Will Learn
- âUse power and root functions
- âApply trigonometric functions
- âWork with logarithms
- âRound numbers correctly
01Introduction to math.h
đ What is math.h?
<math.h> provides mathematical functions for trigonometry, logarithms, powers, rounding, and more.
â ď¸ Link Math Library
On Linux/GCC, you must link with -lm:gcc program.c -o program -lm
| Category | Functions |
|---|---|
| Power/Root | pow, sqrt, cbrt, hypot |
| Trigonometry | sin, cos, tan, asin, acos, atan |
| Logarithm | log, log10, log2, exp |
| Rounding | ceil, floor, round, trunc |
| Other | fabs, fmod, fmax, fmin |
02Power and Root Functions
| Function | Prototype | Description |
|---|---|---|
| pow() | double pow(double base, double exp) | base raised to exp (x^y) |
| sqrt() | double sqrt(double x) | Square root (âx) |
| cbrt() | double cbrt(double x) | Cube root (âx) |
| hypot() | double hypot(double x, double y) | Hypotenuse â(x²+y²) |
power_root.c
C
1#include <stdio.h>2#include <math.h>34int main() {5 // Power6 printf("2^10 = %.0f\n", pow(2, 10)); // 10247 printf("5^3 = %.0f\n", pow(5, 3)); // 1258 9 // Square root10 printf("â16 = %.0f\n", sqrt(16)); // 411 printf("â2 = %.4f\n", sqrt(2)); // 1.414212 13 // Cube root14 printf("â27 = %.0f\n", cbrt(27)); // 315 16 // Hypotenuse (Pythagorean theorem)17 printf("hypot(3,4) = %.0f\n", hypot(3, 4)); // 518 19 return 0;20}03Trigonometric Functions
đĄ Radians, Not Degrees!
All trig functions use radians. Convert: radians = degrees Ă (Ď / 180)
| Function | Prototype | Description |
|---|---|---|
| sin() | double sin(double x) | Sine (x in radians) |
| cos() | double cos(double x) | Cosine |
| tan() | double tan(double x) | Tangent |
| asin() | double asin(double x) | Arc sine (inverse) |
| acos() | double acos(double x) | Arc cosine |
| atan() | double atan(double x) | Arc tangent |
| atan2() | double atan2(double y, double x) | Arc tangent of y/x |
trig_functions.c
C
1#include <stdio.h>2#include <math.h>34#define PI 3.1415926535897932384656int main() {7 // Convert degrees to radians8 double deg = 45.0;9 double rad = deg * (PI / 180.0);10 11 printf("sin(45°) = %.4f\n", sin(rad)); // 0.707112 printf("cos(45°) = %.4f\n", cos(rad)); // 0.707113 printf("tan(45°) = %.4f\n", tan(rad)); // 1.000014 15 // Inverse trig (returns radians)16 double angle = asin(0.5); // 30° in radians17 printf("asin(0.5) = %.4f rad = %.1f°\n", 18 angle, angle * (180.0 / PI));19 20 // Use M_PI constant (if available)21 printf("sin(Ď/2) = %.4f\n", sin(M_PI / 2)); // 1.000022 23 return 0;24}04Exponential and Logarithmic
| Function | Prototype | Description |
|---|---|---|
| exp() | double exp(double x) | e^x (e = 2.718...) |
| log() | double log(double x) | Natural log (ln x) |
| log10() | double log10(double x) | Base-10 log |
| log2() | double log2(double x) | Base-2 log |
log_functions.c
C
1#include <stdio.h>2#include <math.h>34int main() {5 // Exponential6 printf("e^1 = %.4f\n", exp(1)); // 2.71837 printf("e^2 = %.4f\n", exp(2)); // 7.38918 9 // Natural logarithm (base e)10 printf("ln(e) = %.4f\n", log(M_E)); // 1.000011 printf("ln(10) = %.4f\n", log(10)); // 2.302612 13 // Base-10 logarithm14 printf("log10(100) = %.4f\n", log10(100)); // 2.000015 printf("log10(1000) = %.4f\n", log10(1000)); // 3.000016 17 // Base-2 logarithm18 printf("log2(8) = %.4f\n", log2(8)); // 3.000019 printf("log2(1024) = %.4f\n", log2(1024)); // 10.000020 21 return 0;22}05Rounding Functions
| Function | Prototype | Description |
|---|---|---|
| ceil() | double ceil(double x) | Round up âxâ |
| floor() | double floor(double x) | Round down âxâ |
| round() | double round(double x) | Round to nearest |
| trunc() | double trunc(double x) | Truncate (toward 0) |
rounding.c
C
1#include <stdio.h>2#include <math.h>34int main() {5 double x = 3.7, y = -3.7;6 7 printf("Value ceil floor round trunc\n");8 printf("%.1f %.0f %.0f %.0f %.0f\n", 9 x, ceil(x), floor(x), round(x), trunc(x));10 printf("%.1f %.0f %.0f %.0f %.0f\n", 11 y, ceil(y), floor(y), round(y), trunc(y));12 13 // Output:14 // Value ceil floor round trunc15 // 3.7 4 3 4 316 // -3.7 -3 -4 -4 -317 18 return 0;19}Output
Value ceil floor round trunc
3.7 4 3 4 3
-3.7 -3 -4 -4 -3
06Other Useful Functions
| Function | Prototype | Description |
|---|---|---|
| fabs() | double fabs(double x) | Absolute value |x| |
| fmod() | double fmod(double x, double y) | Floating-point modulo |
| fmax() | double fmax(double x, double y) | Maximum of x, y |
| fmin() | double fmin(double x, double y) | Minimum of x, y |
other_math.c
C
1#include <stdio.h>2#include <math.h>34int main() {5 // Absolute value6 printf("fabs(-5.5) = %.1f\n", fabs(-5.5)); // 5.57 8 // Floating-point modulo9 printf("fmod(5.3, 2) = %.1f\n", fmod(5.3, 2)); // 1.310 11 // Max and Min12 printf("fmax(3.5, 2.1) = %.1f\n", fmax(3.5, 2.1)); // 3.513 printf("fmin(3.5, 2.1) = %.1f\n", fmin(3.5, 2.1)); // 2.114 15 return 0;16}07Math Constants
| Constant | Value | Description |
|---|---|---|
| M_PI | 3.14159... | Ď (pi) |
| M_E | 2.71828... | e (Euler's number) |
| M_SQRT2 | 1.41421... | â2 |
| M_LN2 | 0.69314... | ln(2) |
đĄ Note
These constants may require #define _USE_MATH_DEFINESbefore including math.h on some compilers (MSVC).
08Summary
đŻ Key Functions
Power/Root:
pow, sqrt, cbrt, hypot
Trig:
sin, cos, tan, asin, acos, atan
Log/Exp:
log, log10, log2, exp
Rounding:
ceil, floor, round, trunc
09Next Steps
Learn about character classification functions: