1tgmath.h(0P) POSIX Programmer's Manual tgmath.h(0P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 tgmath.h — type-generic macros
13
15 #include <tgmath.h>
16
18 The functionality described on this reference page is aligned with the
19 ISO C standard. Any conflict between the requirements described here
20 and the ISO C standard is unintentional. This volume of POSIX.1‐2017
21 defers to the ISO C standard.
22
23 The <tgmath.h> header shall include the headers <math.h> and <com‐
24 plex.h> and shall define several type-generic macros.
25
26 Of the functions contained within the <math.h> and <complex.h> headers
27 without an f (float) or l (long double) suffix, several have one or
28 more parameters whose corresponding real type is double. For each such
29 function, except modf(), j0(), j1(), jn(), y0(), y1(), and yn(), there
30 shall be a corresponding type-generic macro. The parameters whose cor‐
31 responding real type is double in the function synopsis are generic
32 parameters. Use of the macro invokes a function whose corresponding
33 real type and type domain are determined by the arguments for the
34 generic parameters.
35
36 Use of the macro invokes a function whose generic parameters have the
37 corresponding real type determined as follows:
38
39 * First, if any argument for generic parameters has type long double,
40 the type determined is long double.
41
42 * Otherwise, if any argument for generic parameters has type double
43 or is of integer type, the type determined is double.
44
45 * Otherwise, the type determined is float.
46
47 For each unsuffixed function in the <math.h> header for which there is
48 a function in the <complex.h> header with the same name except for a c
49 prefix, the corresponding type-generic macro (for both functions) has
50 the same name as the function in the <math.h> header. The corresponding
51 type-generic macro for fabs() and cabs() is fabs().
52
53 ┌──────────────────┬──────────────────────┬────────────────────┐
54 │<math.h> Function │ <complex.h> Function │ Type-Generic Macro │
55 ├──────────────────┼──────────────────────┼────────────────────┤
56 │acos() │ cacos() │ acos() │
57 │asin() │ casin() │ asin() │
58 │atan() │ catan() │ atan() │
59 │acosh() │ cacosh() │ acosh() │
60 │asinh() │ casinh() │ asinh() │
61 │atanh() │ catanh() │ atanh() │
62 │cos() │ ccos() │ cos() │
63 │sin() │ csin() │ sin() │
64 │tan() │ ctan() │ tan() │
65 │cosh() │ ccosh() │ cosh() │
66 │sinh() │ csinh() │ sinh() │
67 │tanh() │ ctanh() │ tanh() │
68 │exp() │ cexp() │ exp() │
69 │log() │ clog() │ log() │
70 │pow() │ cpow() │ pow() │
71 │sqrt() │ csqrt() │ sqrt() │
72 │fabs() │ cabs() │ fabs() │
73 └──────────────────┴──────────────────────┴────────────────────┘
74 If at least one argument for a generic parameter is complex, then use
75 of the macro invokes a complex function; otherwise, use of the macro
76 invokes a real function.
77
78 For each unsuffixed function in the <math.h> header without a c-pre‐
79 fixed counterpart in the <complex.h> header, except for modf(), j0(),
80 j1(), jn(), y0(), y1(), and yn(), the corresponding type-generic macro
81 has the same name as the function. These type-generic macros are:
82
83
84 atan2() fma() llround() remainder()
85 cbrt() fmax() log10() remquo()
86 ceil() fmin() log1p() rint()
87 copysign() fmod() log2() round()
88 erf() frexp() logb() scalbln()
89 erfc() hypot() lrint() scalbn()
90 exp2() ilogb() lround() tgamma()
91 expm1() ldexp() nearbyint() trunc()
92 fdim() lgamma() nextafter()
93 floor() llrint() nexttoward()
94
95 If all arguments for generic parameters are real, then use of the macro
96 invokes a real function; otherwise, use of the macro results in unde‐
97 fined behavior.
98
99 For each unsuffixed function in the <complex.h> header that is not a c-
100 prefixed counterpart to a function in the <math.h> header, the corre‐
101 sponding type-generic macro has the same name as the function. These
102 type-generic macros are:
103
104 carg() cimag() conj() cproj() creal()
105
106 Use of the macro with any real or complex argument invokes a complex
107 function.
108
109 The following sections are informative.
110
112 With the declarations:
113
114
115 #include <tgmath.h>
116 int n;
117 float f;
118 double d;
119 long double ld;
120 float complex fc;
121 double complex dc;
122 long double complex ldc;
123
124 functions invoked by use of type-generic macros are shown in the fol‐
125 lowing table:
126
127 ┌─────────────────┬───────────────────────────────┐
128 │ Macro │ Use Invokes │
129 ├─────────────────┼───────────────────────────────┤
130 │exp(n) │ exp(n), the function │
131 │acosh(f) │ acoshf(f) │
132 │sin(d) │ sin(d), the function │
133 │atan(ld) │ atanl(ld) │
134 │log(fc) │ clogf(fc) │
135 │sqrt(dc) │ csqrt(dc) │
136 │pow(ldc,f) │ cpowl(ldc, f) │
137 │remainder(n,n) │ remainder(n, n), the function │
138 │nextafter(d,f) │ nextafter(d, f), the function │
139 │nexttoward(f,ld) │ nexttowardf(f, ld) │
140 │copysign(n,ld) │ copysignl(n, ld) │
141 │ceil(fc) │ Undefined behavior │
142 │rint(dc) │ Undefined behavior │
143 │fmax(ldc,ld) │ Undefined behavior │
144 │carg(n) │ carg(n), the function │
145 │cproj(f) │ cprojf(f) │
146 │creal(d) │ creal(d), the function │
147 │cimag(ld) │ cimagl(ld) │
148 │cabs(fc) │ cabsf(fc) │
149 │carg(dc) │ carg(dc), the function │
150 │cproj(ldc) │ cprojl(ldc) │
151 └─────────────────┴───────────────────────────────┘
153 Type-generic macros allow calling a function whose type is determined
154 by the argument type, as is the case for C operators such as '+' and
155 '*'. For example, with a type-generic cos() macro, the expression
156 cos((float)x) will have type float. This feature enables writing more
157 portably efficient code and alleviates need for awkward casting and
158 suffixing in the process of porting or adjusting precision. Generic
159 math functions are a widely appreciated feature of Fortran.
160
161 The only arguments that affect the type resolution are the arguments
162 corresponding to the parameters that have type double in the synopsis.
163 Hence the type of a type-generic call to nexttoward(), whose second
164 parameter is long double in the synopsis, is determined solely by the
165 type of the first argument.
166
167 The term ``type-generic'' was chosen over the proposed alternatives of
168 intrinsic and overloading. The term is more specific than intrinsic,
169 which already is widely used with a more general meaning, and reflects
170 a closer match to Fortran's generic functions than to C++ overloading.
171
172 The macros are placed in their own header in order not to silently
173 break old programs that include the <math.h> header; for example, with:
174
175
176 printf ("%e", sin(x))
177
178 modf(double, double *) is excluded because no way was seen to make it
179 safe without complicating the type resolution.
180
181 The implementation might, as an extension, endow appropriate ones of
182 the macros that POSIX.1‐2008 specifies only for real arguments with the
183 ability to invoke the complex functions.
184
185 POSIX.1‐2008 does not prescribe any particular implementation mechanism
186 for generic macros. It could be implemented simply with built-in
187 macros. The generic macro for sqrt(), for example, could be implemented
188 with:
189
190
191 #undef sqrt
192 #define sqrt(x) __BUILTIN_GENERIC_sqrt(x)
193
194 Generic macros are designed for a useful level of consistency with C++
195 overloaded math functions.
196
197 The great majority of existing C programs are expected to be unaffected
198 when the <tgmath.h> header is included instead of the <math.h> or <com‐
199 plex.h> headers. Generic macros are similar to the ISO/IEC 9899:1999
200 standard library masking macros, though the semantic types of return
201 values differ.
202
203 The ability to overload on integer as well as floating types would have
204 been useful for some functions; for example, copysign(). Overloading
205 with different numbers of arguments would have allowed reusing names;
206 for example, remainder() for remquo(). However, these facilities would
207 have complicated the specification; and their natural consistent use,
208 such as for a floating abs() or a two-argument atan(), would have
209 introduced further inconsistencies with the ISO/IEC 9899:1999 standard
210 for insufficient benefit.
211
212 The ISO C standard in no way limits the implementation's options for
213 efficiency, including inlining library functions.
214
216 None.
217
219 <math.h>, <complex.h>
220
221 The System Interfaces volume of POSIX.1‐2017, cabs(), fabs(), modf()
222
224 Portions of this text are reprinted and reproduced in electronic form
225 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
226 table Operating System Interface (POSIX), The Open Group Base Specifi‐
227 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
228 Electrical and Electronics Engineers, Inc and The Open Group. In the
229 event of any discrepancy between this version and the original IEEE and
230 The Open Group Standard, the original IEEE and The Open Group Standard
231 is the referee document. The original Standard can be obtained online
232 at http://www.opengroup.org/unix/online.html .
233
234 Any typographical or formatting errors that appear in this page are
235 most likely to have been introduced during the conversion of the source
236 files to man page format. To report such errors, see https://www.ker‐
237 nel.org/doc/man-pages/reporting_bugs.html .
238
239
240
241IEEE/The Open Group 2017 tgmath.h(0P)