C Mastery / math.h: Floating-Point Functions
Part 3 — The Standard Library

math.h: Floating-Point Functions

This chapter covers <math.h>: the standard floating-point functions, constants, and error-reporting conventions.

Why This Matters

<math.h> provides the mathematical operations (sqrt, pow, trig, log, floor, etc.) that C does not provide as language operators. Its domain/error conventions are essential for numerical correctness.

Prerequisites

Core Concept

<math.h> declares functions that take and return double (with float and long double variants suffixed f and l). For example:

double sqrt(double x);
float  sqrtf(float x);
long double sqrtl(long double x);

Common functions

FunctionMeaning
sqrtsquare root
powpower
exp/log/log10exponential/logarithms
sin/cos/tan and inversestrigonometry
fabsabsolute value
floor/ceil/trunc/roundrounding
fmod/remainderremainder
fmin/fmaxmin/max
isnan/isinf/isfinite/isnormalclassification (C99)
copysign, nextafterbit-level helpers

Error handling

Math functions may set errno (EDOM for domain errors, ERANGE for range errors) and/or raise floating-point exceptions, depending on the implementation. Domain errors (e.g., sqrt(-1.0)) produce NaN; range errors (e.g., exp(1000.0)) produce infinity.

Syntax

#include <math.h>

double r = sqrt(9.0);      /* 3.0 */
int    bad = isnan(0.0/0.0);  /* 1 */

Note: on many systems you must link with -lm.

Examples

Basic math

#include <stdio.h>
#include <math.h>

int main(void)
{
    printf("%f\n", sqrt(16.0));
    printf("%f\n", pow(2.0, 10.0));
    printf("%f\n", floor(3.7));
    printf("%d\n", isnan(0.0/0.0));
    return 0;
}

Expected output: 4.000000, 1024.000000, 3.000000, 1.

How It Works

Math functions map to CPU floating-point instructions (e.g., sqrtsd on x86) or to software implementations for operations with no direct hardware support. Classification macros (isnan, etc.) inspect the IEEE 754 bit pattern.

Variations

float and long double variants

Use sqrtf/sqrtl when the argument is float/long double to avoid unnecessary conversions.

fma

fma(a, b, c) computes a*b + c with a single rounding (fused multiply-add). It is more accurate and often faster than separate operations.

Common Mistakes

Undefined Behavior

domain error (NaN). Most <math.h> misuse is defined but error-reporting, not UB.

Portability

Under the Hood

Many math functions are implemented with polynomial/rational approximations and careful range reduction, then a hardware instruction for the final step. The compiler may inline simple functions like fabs or sqrt to a single instruction.

Practical Usage

int).

Exercises

1. Write a program that computes and prints sqrt, pow, and log for a few values. 2. Demonstrate NaN and infinity and classify them with isnan/isinf. 3. Compare fma(a,b,c) with a*b + c for a case where rounding differs. 4. Use fabs and an epsilon to compare two computed values.

Deep Challenge

Implement a numerically stable quadratic-equation solver that handles all edge cases (no real roots, cancellation, very large/small coefficients) using <math.h>, and explain the numerical issues in each case.

References

Verification

verified.`

Progress

Concept checkboxes

Mastery levels

ConceptCurrent level (0–8)Target level
c.lib.math05