C Numeric
Curated hub for C numerics: math, complex arithmetic, floating-point environment, random number generation, bit utilities, and newer checked integer helpers.
The C numerics area covers much more than “math functions”. It includes floating-point classification and exceptions, complex arithmetic, pseudo-random number generation, bit utilities, type-generic dispatch, and newer checked integer arithmetic helpers.
# Start Here
Real-valued math
Elementary functions, powers, rounding, classification, special values, and the core floating-point function families.
sqrt · pow · fpclassify · NAN
Complex arithmetic
Complex numbers, projections, magnitudes, trigonometric/exponential functions, and the C99 complex model.
Floating-point environment
Exception flags, rounding direction, saved environments, and the parts of C numerics that depend on the current FP execution state.
Pseudo-random number generation
The classic `rand`/`srand` surface and its limits, suitable mostly for simple or legacy code paths.
Bit utilities
C23 bit operations and endian-related helpers for low-level integer representation work.
Type-generic and checked arithmetic
Macros that dispatch across floating/complex families and the newer checked integer operations added for overflow-aware arithmetic.
# Quick Map
| If you need to... | Start with | Why |
|---|---|---|
| Compute elementary functions, powers, roots, or rounding behavior | Math | The main real-valued function families, classification macros, and floating constants live here. |
| Work with `double complex` and related types | Complex | Complex-specific functions and constants are separate from the real-valued math surface. |
| Inspect or control FP exceptions and rounding mode | Floating-point environment | These APIs deal with execution-state side effects, not just numeric values. |
| Generate simple pseudo-random integers | Random | The C library exposes the traditional `rand`/`srand` interface here. |
| Use type-generic numeric macros across float/double/complex variants | Type-generic math | <tgmath.h> chooses the right overload-like macro expansion for you. |
| Write overflow-aware integer arithmetic in modern C | ckd_add, ckd_sub, ckd_mul | The checked integer helpers are the direct route when arithmetic overflow must be detected rather than ignored. |
| Reason about bit widths, leading/trailing zeros, or endianness | Bit manipulation | The newer bit utilities cover low-level representation-oriented integer tasks. |
# Numeric Families
| Family | Representative entry points | Use it for |
|---|---|---|
| Core math | sin, cos, pow, sqrt, fma | Real-valued mathematical computation, rounding, powers, logs, and transcendental functions. |
| Classification and special values | fpclassify, isfinite, isnan, NAN, INFINITY | Detect value categories and handle NaN/infinity-aware code paths. |
| Complex arithmetic | cabs, creal, cimag, cpow | Complex-number computation and decomposition under the C complex model. |
| Floating-point environment | feclearexcept, fetestexcept, feraiseexcept, feholdexcept | Exception flags, saved environments, and rounding/FP-state-sensitive execution. |
| Pseudo-random numbers | rand, srand, RAND_MAX | Traditional C library PRNG entry points for simple or legacy use. |
| Type-generic dispatch | tgmath | Calling the right math/complex family through macros instead of choosing the suffixed name manually. |
| Checked and bit-oriented integer helpers | ckd_add, ckd_sub, ckd_mul, stdc_leading_zeros | Overflow-aware arithmetic and low-level integer representation utilities in newer C revisions. |
# Common Entry Points
| Task | Go here | Notes |
|---|---|---|
| Absolute value / magnitude | abs, fabs, cabs | Pick integer, real floating, or complex magnitude based on the numeric domain. |
| Exponentials, logs, and powers | exp, log, pow | These are central math-family entry points and often pair with classification/error handling. |
| Roots and distance-like helpers | sqrt, cbrt, hypot | Use these instead of ad hoc formulas when precision and special cases matter. |
| Classify a floating-point result | fpclassify, isfinite, isnan, signbit | These are often needed around exceptional or boundary numeric cases. |
| Check or clear FP exceptions | feclearexcept, fetestexcept | Start here when correctness depends on FP status flags rather than only returned values. |
| Seed and draw from the classic PRNG | srand, rand | Useful mainly for compatibility and simple code; not a modern high-quality generator design. |
# Standard Evolution
| Standard | Navigation impact |
|---|---|
| C99 | Major expansion: complex arithmetic, floating-point environment, type-generic math, classification macros, and many modern math facilities became part of the standard library. |
| C11 | The numeric area gained alignment with newer language features and continued to rely on the C99 math/complex/fenv core as the main navigation backbone. |
| C23 | Added newer bit helpers and checked integer arithmetic entry points, making `/c/numeric/` broader than just floating-point and complex APIs. |
# Boundary Lines
| This hub covers | Use a different hub for |
|---|---|
| Math, complex numbers, FP environment, random numbers, checked arithmetic, and bit-oriented numeric helpers. | String for text-to-number conversion and byte-string utilities, Chrono for time/date arithmetic, Language for constant-expression and conversion rules. |
| The C library view of numerics. | C++ numerics when you need the C++ library surface instead of the C one. |
# Practical Routes
I need a math function
Start from the math section when the main question is about a computation on real-valued arguments or a floating-point classification rule.
sin · pow · fpclassify
I need complex-number support
Start from the complex section when values are truly complex, not just paired real values passed through ordinary math functions.
I need FP state or exception control
Start from the floating-point environment when return values alone are not enough and FP flags or rounding mode affect correctness.