Table of Contents

<tgmath.h>

<tgmath.h> provides type-generic math wrappers (C99) so you can write sqrt instead of sqrtf/sqrt/sqrtl. The dispatcher selects the right function based on argument type at compile time.

Precision-critical code often prefers explicit suffixes for clarity.

Example

This example shows type-generic dispatch to different precision functions.

// compile: gcc -o tgmathexample tgmathexample.c -lm
// run: ./tgmathexample
// description: type-generic dispatch to float/double/long-double variants
 
#include <tgmath.h>
#include <stdio.h>
 
int main() {
    float f = 2.0f;
    double d = 2.0;
    long double ld = 2.0L;
 
    printf("sqrt(float)       = %.10f\n", (double)sqrt(f));
    printf("sqrt(double)      = %.10f\n", sqrt(d));
    printf("sqrt(long double) = %.10Lf\n", sqrt(ld));
 
    return 0;
}