liveBook · Manning

Tgmath.h

As explained in Modern C

tgmath.h is the C standard header that provides type-generic macros for mathematical functions.

A type-generic absolute value

#include <tgmath.h> double const a = -3.5; double magnitude = fabs(a); section 3.2

The <tgmath.h> header supplies type-generic macros for mathematical operations. An invocation such as fabs(a), sin(x), or pow(x, n) dispatches according to an argument's type and produces a result of the corresponding type. TAKEAWAY: include <tgmath.h> when one mathematical interface should work across supported numeric types. section 8.2

Absolute error in an iterative calculation

#include <tgmath.h> double const eps = 1E-9; double const a = 34.0; double x = 0.5; while (fabs(1.0 - a*x) >= eps) { x *= (2.0 - a*x); } Here fabs measures how far the product a*x is from 1.0. The loop applies Heron's approximation until the inverse estimate is sufficiently close. section 3.2

The header offers many related operations, including trigonometric, exponential, rounding, floating-point classification, and complex-number functions. Prefer these library facilities to handcrafted replacements: implementations may use processor-specific instructions and provide controlled numerical precision. section 8.2

How Modern C covers it

How deep it goes
Explains type-based dispatch and surveys function families, but defers detailed descriptions of the complete macro set.
Examples
Uses fabs in an iterative Heron approximation that computes multiplicative inverses and tests absolute error against a tolerance.
What you can do after
Apply it via <tgmath.h> macros and select established numerical operations instead of reimplementing them by hand.
How it connects
Connects type-generic macros with math.h, floating-point arithmetic, 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. The section links point to the parts of the book it draws on.