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
c.core.4— floating-point types.
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
| Function | Meaning |
|---|---|
sqrt | square root |
pow | power |
exp/log/log10 | exponential/logarithms |
sin/cos/tan and inverses | trigonometry |
fabs | absolute value |
floor/ceil/trunc/round | rounding |
fmod/remainder | remainder |
fmin/fmax | min/max |
isnan/isinf/isfinite/isnormal | classification (C99) |
copysign, nextafter | bit-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
- Forgetting to link with
-lm. - Comparing floating results with
==instead of an epsilon. - Ignoring domain/range errors (NaN/infinity propagation).
- Using
sqrtforfloatand paying a conversion penalty.
Undefined Behavior
- Passing invalid arguments (e.g.,
sqrt(-1.0)) is *not* UB; it produces a
domain error (NaN). Most <math.h> misuse is defined but error-reporting, not UB.
Portability
isnan/isinf/isfiniteare C99 and later.- The exact
errno/exception behavior is implementation-defined. -lmis required on many Unix-like systems.
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
- Use
isnan/isinf/isfiniteto validate inputs and outputs. - Use
fmafor accuracy in dot products and polynomial evaluation. - Prefer
fabsfor absolute value of floating-point (notabs, which is for
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.
Related Concepts
c.types.float— floating-point representation.c.core.4— NaN, infinity, rounding.
References
- ISO/IEC 9899:2018 §7.12 (math.h).
Verification
- Math functions and their f/l variants are standard.
VERIFIED - Domain errors produce NaN; range errors produce infinity.
VERIFIED -lmrequirement is platform-specific.VERIFIED- No example was executed during generation unless noted. `Execution not
verified.`
Progress
- [ ] Read
- [ ] Understand
- [ ] Complete examples
- [ ] Complete exercises
- [ ] Complete deep challenge
Concept checkboxes
- [ ] Core math functions
- [ ] float/long double variants
- [ ] Classification macros
- [ ] Domain/range errors
- [ ] fma
Mastery levels
| Concept | Current level (0–8) | Target level |
|---|---|---|
| c.lib.math | 0 | 5 |