liveBook · Manning

Tgmath.h

As explained in Modern C, Third Edition

tgmath is the C type-generic mathematics facility that provides interfaces selecting numerical functions according to argument types.

The <tgmath.h> header supplies type-generic mathematics macros. An invocation such as fabs(x), sin(x), or pow(x, n) dispatches to a numerical function selected from the argument type, with a result of the corresponding type. section 8.3

Using fabs in an iterative calculation

#include <tgmath.h> constexpr double epsilon = 1E-9; double const a = 34.0; double x = 0.5; while (fabs(1.0 - a*x) >= epsilon) { x *= (2.0 - a*x); } Here fabs supports the termination test without spelling out a separate floating-point variant. The surrounding Heron iteration repeatedly improves an estimate of 1/a until a*x is sufficiently close to 1. section 3.2

The facility covers a broad range of floating-point operations, including trigonometric, logarithmic, rounding, classification, scaling, and complex-number-related functions. Prefer these established interfaces over handcrafted replacements: implementations may use processor-specific instructions and controlled numerical precision. section 8.3

Table 8.6 Numerical functions that work with floating-point types. Names followed by [f|l] describe three functions for double, float, and long double arguments. The others are type-generic macros that adapt to the concrete type of their arguments.
acosh Hyperbolic arc cosine
acos, acospi Arc cosine (divided by \(\pi\)) C23
asinh Hyperbolic arc sine
asin, asinpi Arc sine (divided by \(\pi\)) C23
atan2, atan2pi Arc tangent, two arguments (divided by \(\pi\)) C23
atanh Hyperbolic arc tangent
atan, atanpi Arc tangent (divided by \(\pi\)) C23
canonicalize[f|l] Canonicalizes a floating-point value
cbrt \(\sqrt[3]{x}\)
ceil \(\lceil x \rceil\)
compoundn \((1+x)^n\) C23
copysign Copies the sign from \(y\) to \(x\)
cosh Hyperbolic cosine
cos, cospi Cosine function, \(\cos x\) (\(\cos\pi x\)) C23
erfc Complementary error function, \(1 - \frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^2} dt\)
erf Error function, \(\frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^2} dt\)
exp2 \(2^x\)
expm1 \(e^x-1\)
exp \(e^x\)
fabs \(|x|\) for floating point
fadd, dadd Rounded addition to float or double C23
fdim Positive difference
fdiv, ddiv Rounded division to float or double C23
floor \(\lfloor x \rfloor\)
fmaximum_mag[_num] Floating-point value of maximal magnitude C23
fmax, fmaximum[_num] Floating-point maximum C23
fma \(x \cdot y + z\)
fminimum_mag[_num] Floating-point value of minimal magnitude C23
fmin, fminimum[_num] Floating-point minimum C23
fmod Remainder of floating-point division
fmul, dmul Rounded multiplication to float or double C23
fpclassify Classifies a floating-point value
frexp Significand and exponent
fromfp[f|l] Round to signed integer value with specific width C23
fsub, dsub Rounded subtraction to float or double C23
hypot \(\sqrt[2]{x^2+y^2}\)

Browse the numerical-function table to distinguish type-generic macros from families with explicit double, float, and long-double forms, and to survey available operations. section 8.3

How Modern C, Third Edition covers it

How deep it goes
Explains argument-based dispatch and implementation rationale, but does not enumerate every macro or specify dispatch mechanics in detail.
Examples
Uses fabs in a Heron-approximation loop that computes multiplicative inverses from command-line numbers.
What it assumes
Assumes familiarity with C headers, macros, numeric arguments, and floating-point calculations while introducing type-generic mathematical selection.
What you can do after
Apply it via <tgmath.h> macros and choose established numerical interfaces instead of reimplementing floating-point operations.
How it connects
Connects type-generic macros with math.h, floating-point types, numerical precision, and processor-specific instructions.

Also explained in

Compare how 2 books explain tgmath.h →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.