tgmath.h(7POSIX)
π tgmath.h(7POSIX) β POSIX Programmer's Manual π
π PROLOG π
Okay bestie, real talk: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. iykyk π«Ά
β¨ NAME β¨
tgmath.h β type-generic macros β¨
π SYNOPSIS π
#include <tgmath.h>
π¦ DESCRIPTION π¦
So the tea: the functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1β2017 defers to the ISO C standard. No cap. π
The <tgmath.h> header shall include the headers <math.h> and <complex.h> and shall define several type-generic macros. Iconic. π
Of the functions contained within the <math.h> and <complex.h> headers without an f (float) or l (long double) suffix, several have one or more parameters whose corresponding real type is double. For each such function, except modf(), j0(), j1(), jn(), y0(), y1(), and yn(), there shall be a corresponding type-generic macro. The parameters whose corresponding real type is double in the function synopsis are generic parameters. Use of the macro invokes a function whose corresponding real type and type domain are determined by the arguments for the generic parameters. π«Ά
Use of the macro invokes a function whose generic parameters have the corresponding real type determined as follows:
- First, if any argument for generic parameters has type
long double, the type determined islong double. - Otherwise, if any argument for generic parameters has type
doubleor is of integer type, the type determined isdouble. - Otherwise, the type determined is
float.
For each unsuffixed function in the <math.h> header for which there is a function in the <complex.h> header with the same name except for a c prefix, the corresponding type-generic macro (for both functions) has the same name as the function in the <math.h> header. The corresponding type-generic macro for fabs() and cabs() is fabs(). Here's the mapping, bestie:
<math.h>acos()β<complex.h>cacos()β type-generic macroacos()<math.h>asin()β<complex.h>casin()β type-generic macroasin()<math.h>atan()β<complex.h>catan()β type-generic macroatan()<math.h>acosh()β<complex.h>cacosh()β type-generic macroacosh()<math.h>asinh()β<complex.h>casinh()β type-generic macroasinh()<math.h>atanh()β<complex.h>catanh()β type-generic macroatanh()<math.h>cos()β<complex.h>ccos()β type-generic macrocos()<math.h>sin()β<complex.h>csin()β type-generic macrosin()<math.h>tan()β<complex.h>ctan()β type-generic macrotan()<math.h>cosh()β<complex.h>ccosh()β type-generic macrocosh()<math.h>sinh()β<complex.h>csinh()β type-generic macrosinh()<math.h>tanh()β<complex.h>ctanh()β type-generic macrotanh()<math.h>exp()β<complex.h>cexp()β type-generic macroexp()<math.h>log()β<complex.h>clog()β type-generic macrolog()<math.h>pow()β<complex.h>cpow()β type-generic macropow()<math.h>sqrt()β<complex.h>csqrt()β type-generic macrosqrt()<math.h>fabs()β<complex.h>cabs()β type-generic macrofabs()
If at least one argument for a generic parameter is complex, then use of the macro invokes a complex function; otherwise, use of the macro invokes a real function. periodt π
For each unsuffixed function in the <math.h> header without a c-prefixed counterpart in the <complex.h> header, except for modf(), j0(), j1(), jn(), y0(), y1(), and yn(), the corresponding type-generic macro has the same name as the function. These type-generic macros are:
atan2() fma() llround() remainder()
cbrt() fmax() log10() remquo()
ceil() fmin() log1p() rint()
copysign() fmod() log2() round()
erf() frexp() logb() scalbln()
erfc() hypot() lrint() scalbn()
exp2() ilogb() lround() tgamma()
expm1() ldexp() nearbyint() trunc()
fdim() lgamma() nextafter()
floor() llrint() nexttoward()
If all arguments for generic parameters are real, then use of the macro invokes a real function; otherwise, use of the macro results in undefined behavior.
Undefined behavior alert! π If all args aren't real, you're in undefined-behavior territory. No cap.
For each unsuffixed function in the <complex.h> header that is not a c-prefixed counterpart to a function in the <math.h> header, the corresponding type-generic macro has the same name as the function. These type-generic macros are:
carg() cimag() conj() cproj() creal()
Use of the macro with any real or complex argument invokes a complex function. β¨
The following sections are informative.
π APPLICATION USAGE π
With the declarations:
#include <tgmath.h>
int n;
float f;
double d;
long double ld;
float complex fc;
double complex dc;
long double complex ldc;
functions invoked by use of type-generic macros are shown in the following table. Since we're keeping it cute and table-free, here's the same tea as a list:
exp(n)β invokesexp(n), the functionacosh(f)β invokesacoshf(f)sin(d)β invokessin(d), the functionatan(ld)β invokesatanl(ld)log(fc)β invokesclogf(fc)sqrt(dc)β invokescsqrt(dc)pow(ldc,f)β invokescpowl(ldc, f)remainder(n,n)β invokesremainder(n, n), the functionnextafter(d,f)β invokesnextafter(d, f), the functionnexttoward(f,ld)β invokesnexttowardf(f, ld)copysign(n,ld)β invokescopysignl(n, ld)ceil(fc)β invokes Undefined behaviorrint(dc)β invokes Undefined behaviorfmax(ldc,ld)β invokes Undefined behaviorcarg(n)β invokescarg(n), the functioncproj(f)β invokescprojf(f)creal(d)β invokescreal(d), the functioncimag(ld)β invokescimagl(ld)cabs(fc)β invokescabsf(fc)carg(dc)β invokescarg(dc), the functioncproj(ldc)β invokescprojl(ldc)
π§ RATIONALE π§
Type-generic macros allow calling a function whose type is determined by the argument type, as is the case for C operators such as '+' and '*'. For example, with a type-generic cos() macro, the expression cos((float)x) will have type float. This feature enables writing more portably efficient code and alleviates need for awkward casting and suffixing in the process of porting or adjusting precision. Generic math functions are a widely appreciated feature of Fortran. obsessed π§
The only arguments that affect the type resolution are the arguments corresponding to the parameters that have type double in the synopsis. Hence the type of a type-generic call to nexttoward(), whose second parameter is long double in the synopsis, is determined solely by the type of the first argument.
The term βtype-genericβ was chosen over the proposed alternatives of intrinsic and overloading. The term is more specific than intrinsic, which already is widely used with a more general meaning, and reflects a closer match to Fortran's generic functions than to C++ overloading. iykyk π
The macros are placed in their own header in order not to silently break old programs that include the <math.h> header; for example, with:
printf ("%e", sin(x))
modf(double, double *) is excluded because no way was seen to make it safe without complicating the type resolution.
The implementation might, as an extension, endow appropriate ones of the macros that POSIX.1β2008 specifies only for real arguments with the ability to invoke the complex functions.
POSIX.1β2008 does not prescribe any particular implementation mechanism for generic macros. It could be implemented simply with built-in macros. The generic macro for sqrt(), for example, could be implemented with:
#undef sqrt
#define sqrt(x) __BUILTIN_GENERIC_sqrt(x)
Generic macros are designed for a useful level of consistency with C++ overloaded math functions.
The great majority of existing C programs are expected to be unaffected when the <tgmath.h> header is included instead of the <math.h> or <complex.h> headers. Generic macros are similar to the ISO/IEC 9899:1999 standard library masking macros, though the semantic types of return values differ.
The ability to overload on integer as well as floating types would have been useful for some functions; for example, copysign(). Overloading with different numbers of arguments would have allowed reusing names; for example, remainder() for remquo(). However, these facilities would have complicated the specification; and their natural consistent use, such as for a floating abs() or a two-argument atan(), would have introduced further inconsistencies with the ISO/IEC 9899:1999 standard for insufficient benefit.
The ISO C standard in no way limits the implementation's options for efficiency, including inlining library functions. periodt β¨
π FUTURE DIRECTIONS π
None.
β¨ SEE ALSO β¨
okay bestie, when you're done here and you wanna go deeper (and you will, i know you), go peek at these:
<math.h>β the og home of all your real-number math besties πΈ<complex.h>β where the imaginary girlies live, and yes they're iconic π
and like, if you want the full lore, the System Interfaces volume of POSIX.1β2017 has the receipts on:
cabs()fabs()modf()
go read those too, fr fr, they're the supporting cast and they ate π
π COPYRIGHT π
okay so this is the part where we give credit where credit is due, because we do NOT plagiarize in this house π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
lowkey important: if this page and the actual Standard ever beef with each other, the Standard wins. every time. no cap. π«‘
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
so if you spot a typo, it's almost definitely a conversion glitch, not a you problem β go tell the kernel doc girlies about it at that link π
and that's a wrap on tgmath.h(7POSIX) bestie!! this whole thing came to you from IEEE/The Open Group, dated 2017, and honestly? iconic of them. now go forth and let the compiler pick your types for you, main character energy only πβ¨π¦
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms