<math.h> provides standard math functions: trigonometric, exponential, logarithmic, rounding, and IEEE 754 utilities (isnan, isinf). Append f for float or l for long double versions.
Compile with -lm to link the math library.
This example demonstrates basic math functions and special-value checking.
// compile: gcc -o mathexample mathexample.c -lm // run: ./mathexample // description: math functions and special value detection #include <math.h> #include <stdio.h> int main() { printf("sqrt(2) = %.6f\n", sqrt(2.0)); printf("sin(pi/4) = %.6f\n", sin(M_PI / 4)); printf("exp(1) = %.6f\n", exp(1.0)); printf("log(10) = %.6f\n", log(10.0)); double nan_val = 0.0 / 0.0; double inf_val = 1.0 / 0.0; printf("isnan(nan): %d, isinf(inf): %d\n", isnan(nan_val), isinf(inf_val)); return 0; }
Root and power:
sqrt(x): square rootcbrt(x): cube rootpow(x, y): x to power yhypot(x, y): sqrt(x² + y²) without overflowExponential and logarithm:
exp(x), exp2(x), exp10(x): e^x, 2^x, 10^xexpm1(x): e^x - 1 (accurate for small x)log(x), log2(x), log10(x): natural, base-2, base-10 logarithmlog1p(x): log(1 + x) (accurate for small x)Trigonometric (argument in radians):
sin(x), cos(x), tan(x)asin(x), acos(x), atan(x): inverse (range: -π/2 to π/2)atan2(y, x): inverse with quadrant correctionHyperbolic:
sinh(x), cosh(x), tanh(x)asinh(x), acosh(x), atanh(x): inverseRounding:
ceil(x): round up (smallest integer ≥ x)floor(x): round down (largest integer ≤ x)round(x): round to nearest integer (0.5 rounds away from zero)trunc(x): truncate toward zeronearbyint(x): round to nearest using current rounding moderint(x): similar to nearbyint but may raise exceptionAbsolute value and remainder:
fabs(x): absolute value (floating-point)fmod(x, y): floating-point remainder (sign of x)remainder(x, y): IEEE remainder (nearest multiple of y)drem(x, y): alias for remainderMinimum and maximum:
fmin(x, y): minimum (NaN-safe: returns non-NaN if possible)fmax(x, y): maximumSpecial value checks:
isnan(x): check if NaNisinf(x): check if infiniteisfinite(x): check if finite (not NaN or inf)isnormal(x): check if normal (not zero, subnormal, inf, or NaN)signbit(x): check sign bit
Constants (non-standard; require -D_USE_MATH_DEFINES or -D_GNU_SOURCE):
M_PI: πM_E: eM_LN2, M_LN10: ln(2), ln(10)M_LOG2E, M_LOG10E: log₂(e), log₁₀(e)M_SQRT2, M_SQRT1_2: √2, 1/√2