C Reference ยท ctype.h, math.h and others
C tgmath.h: type-generic math
Reference for tgmath.h: call sqrt, pow, fabs or exp once and let the compiler pick the float, double, long double or complex version for the argument.
<tgmath.h> includes both <math.h> and <complex.h> and then redefines the common functions as type-generic macros. When you write sqrt(x), the macro looks at the type of x and calls sqrtf for a float, sqrt for a double, sqrtl for a long double, or csqrt for a complex number. Integer arguments are treated as double.
The benefit is that code does not need editing when a variable changes from float to double, and that you cannot accidentally call the double version on a float and lose speed or precision without noticing.
#include <tgmath.h> float a = sqrt(2.0f); /* calls sqrtf */ double b = sqrt(2.0); /* calls sqrt */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
The header takes no arguments; it provides macros named after the <math.h> and <complex.h> functions.
| Group | Macros |
|---|---|
| Real and complex | acos, asin, atan, acosh, asinh, atanh, cos, sin, tan, cosh, sinh, tanh, exp, log, pow, sqrt, fabs |
| Real only | atan2, cbrt, ceil, copysign, erf, erfc, exp2, expm1, fdim, floor, fma, fmax, fmin, fmod, frexp, hypot, ilogb, ldexp, lgamma, llrint, llround, log10, log1p, log2, logb, lrint, lround, nearbyint, nextafter, nexttoward, remainder, remquo, rint, round, scalbn, scalbln, tgamma, trunc |
| Complex only | carg, cimag, conj, cproj, creal |
Choosing the function: if any argument is complex, the complex function is used; otherwise the real one. If the arguments have different types, the result type follows the usual arithmetic conversions: long double beats double, which beats float. An integer argument counts as double.
Special cases: fabs on a complex argument calls cabs, and fabs/sqrt/exp of a real argument return a real. modf, nan, remquo pointer arguments and functions with no floating argument (like isnan) are not wrapped here.
Usage notes
- Everything is a macro, so arguments may be evaluated more than once in some implementations. Do not pass expressions with side effects such as
sqrt(x++). - You cannot use the macro as a function pointer value the way you can with
<math.h>names. Because the macro is function-like, a baresqrt(without parentheses after it) still names the plaindoublefunction, as the example shows. - Complex versions are chosen at compile time from the static type, not from the value. A
double complexwhose imaginary part is zero still callscsqrt, sosqrt(CMPLX(-4.0, 0.0))returns2iwhilesqrt(-4.0)is NaN. - Integer arguments become
double, sosqrt(2)computes in double precision. For integer-heavy code that is usually what you want, butfloatdata is not silently widened. - Generic code in C99/C11 style can also use
_Genericto build your own type-generic macros, see_Generic. - Including
<tgmath.h>pulls in<complex.h>, which defines the macro namescomplexandI. A variable calledIorcomplexin your code will conflict. - Not available in C++, where the overloads in
<cmath>serve the same purpose. - Standard version: C99. In GCC the macros use
__builtin_tgmath, which is why the result types are exact.