1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved
.TH "<tgmath.h>" P 2003 POSIX
.\" <tgmath.h>
.SH NAME
tgmath.h \- type-generic macros
.SH SYNOPSIS
.LP
\fB#include <tgmath.h>\fP
.SH DESCRIPTION
.LP
The \fI<tgmath.h>\fP header shall include the headers \fI<math.h>\fP
and \fI<complex.h>\fP and shall define several type-generic macros.
.LP
Of the functions contained within the \fI<math.h>\fP and \fI<complex.h>\fP
headers without an \fIf\fP ( \fBfloat\fP) or \fIl\fP ( \fBlong
double\fP) suffix, several have one or more parameters whose corresponding
real type is \fBdouble\fP. For each such function,
except \fImodf\fP(), there shall be a corresponding type-generic macro.
The parameters whose
corresponding real type is \fBdouble\fP 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.
.LP
Use of the macro invokes a function whose generic parameters have
the corresponding real type determined as follows:
.IP " *" 3
First, if any argument for generic parameters has type \fBlong double\fP,
the type determined is \fBlong double\fP.
.LP
.IP " *" 3
Otherwise, if any argument for generic parameters has type \fBdouble\fP
or is of integer type, the type determined is
\fBdouble\fP.
.LP
.IP " *" 3
Otherwise, the type determined is \fBfloat\fP.
.LP
.LP
For each unsuffixed function in the \fI<math.h>\fP header for which
there is a
function in the \fI<complex.h>\fP header with the same name except
for a \fIc\fP
prefix, the corresponding type-generic macro (for both functions)
has the same name as the function in the \fI<math.h>\fP header. The
corresponding type-generic macro for \fIfabs\fP() and \fIcabs\fP()
is \fIfabs\fP().
.TS C
center; l l l.
\fB<math.h>\fP \fB<complex.h>\fP \fBType-Generic\fP
\fBFunction\fP \fBFunction\fP \fBMacro\fP
\fIacos\fP() \fIcacos\fP() \fIacos\fP()
\fIasin\fP() \fIcasin\fP() \fIasin\fP()
\fIatan\fP() \fIcatan\fP() \fIatan\fP()
\fIacosh\fP() \fIcacosh\fP() \fIacosh\fP()
\fIasinh\fP() \fIcasinh\fP() \fIasinh\fP()
\fIatanh\fP() \fIcatanh\fP() \fIatanh\fP()
\fIcos\fP() \fIccos\fP() \fIcos\fP()
\fIsin\fP() \fIcsin\fP() \fIsin\fP()
\fItan\fP() \fIctan\fP() \fItan\fP()
\fIcosh\fP() \fIccosh\fP() \fIcosh\fP()
\fIsinh\fP() \fIcsinh\fP() \fIsinh\fP()
\fItanh\fP() \fIctanh\fP() \fItanh\fP()
\fIexp\fP() \fIcexp\fP() \fIexp\fP()
\fIlog\fP() \fIclog\fP() \fIlog\fP()
\fIpow\fP() \fIcpow\fP() \fIpow\fP()
\fIsqrt\fP() \fIcsqrt\fP() \fIsqrt\fP()
\fIfabs\fP() \fIcabs\fP() \fIfabs\fP()
.TE
.LP
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.
.LP
For each unsuffixed function in the \fI<math.h>\fP header without
a
\fIc\fP-prefixed counterpart in the \fI<complex.h>\fP header, the
corresponding
type-generic macro has the same name as the function. These type-generic
macros are:
.TS C
center; lw(19) lw(19) lw(19) lw(19).
T{
.br
\fIatan2\fP()
.br
\fIcbrt\fP()
.br
\fIceil\fP()
.br
\fIcopysign\fP()
.br
\fIerf\fP()
.br
\fIerfc\fP()
.br
\fIexp2\fP()
.br
\fIexpm1\fP()
.br
\fIfdim\fP()
.br
\fIfloor\fP()
.br
\
T} T{
.br
\fIfma\fP()
.br
\fIfmax\fP()
.br
\fIfmin\fP()
.br
\fIfmod\fP()
.br
\fIfrexp\fP()
.br
\fIhypot\fP()
.br
\fIilogb\fP()
.br
\fIldexp\fP()
.br
\fIlgamma\fP()
.br
\fIllrint\fP()
.br
\
T} T{
.br
\fIllround\fP()
.br
\fIlog10\fP()
.br
\fIlog1p\fP()
.br
\fIlog2\fP()
.br
\fIlogb\fP()
.br
\fIlrint\fP()
.br
\fIlround\fP()
.br
\fInearbyint\fP()
.br
\fInextafter\fP()
.br
\fInexttoward\fP()
.br
\
T} T{
.br
\fIremainder\fP()
.br
\fIremquo\fP()
.br
\fIrint\fP()
.br
\fIround\fP()
.br
\fIscalbn\fP()
.br
\fIscalbln\fP()
.br
\fItgamma\fP()
.br
\fItrunc\fP()
.br
\
T}
.TE
.LP
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.
.LP
For each unsuffixed function in the \fI<complex.h>\fP header that
is not a
\fIc\fP-prefixed counterpart to a function in the \fI<math.h>\fP header,
the
corresponding type-generic macro has the same name as the function.
These type-generic macros are:
.sp
.RS
.nf
\fIcarg\fP()
\fIcimag\fP()
\fIconj\fP()
\fIcproj\fP()
\fIcreal\fP()
.fi
.RE
.LP
Use of the macro with any real or complex argument invokes a complex
function.
.LP
\fIThe following sections are informative.\fP
.SH APPLICATION USAGE
.LP
With the declarations:
.sp
.RS
.nf
\fB#include <tgmath.h>
int n;
float f;
double d;
long double ld;
float complex fc;
double complex dc;
long double complex ldc;
\fP
.fi
.RE
.LP
functions invoked by use of type-generic macros are shown in the following
table:
.TS C
center; l2 l.
\fBMacro\fP \fBUse Invokes\fP
\fIexp\fP(\fIn\fP) \fIexp\fP(\fIn\fP), the function
\fIacosh\fP(\fIf\fP) \fIacoshf\fP(\fIf\fP)
\fIsin\fP(\fId\fP) \fIsin\fP(\fId\fP), the function
\fIatan\fP(\fIld\fP) \fIatanl\fP(\fIld\fP)
\fIlog\fP(\fIfc\fP) \fIclogf\fP(\fIfc\fP)
\fIsqrt\fP(\fIdc\fP) \fIcsqrt\fP(\fIdc\fP)
\fIpow\fP(\fIldc,f\fP) \fIcpowl\fP(\fIldc, f\fP)
\fIremainder\fP(\fIn,n\fP) \fIremainder\fP(\fIn, n\fP), the function
\fInextafter\fP(\fId,f\fP) \fInextafter\fP(\fId, f\fP), the function
\fInexttoward\fP(\fIf,ld\fP) \fInexttowardf\fP(\fIf, ld\fP)
\fIcopysign\fP(\fIn,ld\fP) \fIcopysignl\fP(\fIn, ld\fP)
\fIceil\fP(\fIfc\fP) Undefined behavior
\fIrint\fP(\fIdc\fP) Undefined behavior
\fIfmax\fP(\fIldc,ld\fP) Undefined behavior
\fIcarg\fP(\fIn\fP) \fIcarg\fP(\fIn\fP), the function
\fIcproj\fP(\fIf\fP) \fIcprojf\fP(\fIf\fP)
\fIcreal\fP(\fId\fP) \fIcreal\fP(\fId\fP), the function
\fIcimag\fP(\fIld\fP) \fIcimagl\fP(\fIld\fP)
\fIcabs\fP(\fIfc\fP) \fIcabsf\fP(\fIfc\fP)
\fIcarg\fP(\fIdc\fP) \fIcarg\fP(\fIdc\fP), the function
\fIcproj\fP(\fIldc\fP) \fIcprojl\fP(\fIldc\fP)
.TE
.SH RATIONALE
.LP
Type-generic macros allow calling a function whose type is determined
by the argument type, as is the case for C operators such
as \fB'+'\fP and \fB'*'\fP . For example, with a type-generic \fIcos\fP()
macro, the
expression \fIcos\fP(( \fBfloat\fP) \fIx\fP) will have type \fBfloat\fP.
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.
.LP
The only arguments that affect the type resolution are the arguments
corresponding to the parameters that have type
\fBdouble\fP in the synopsis. Hence the type of a type-generic call
to \fInexttoward\fP(), whose second parameter is \fBlong double\fP
in the synopsis, is determined
solely by the type of the first argument.
.LP
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.
.LP
The macros are placed in their own header in order not to silently
break old programs that include the \fI<math.h>\fP header; for example,
with:
.sp
.RS
.nf
\fBprintf ("%e", sin(x))
\fP
.fi
.RE
.LP
\fImodf\fP( \fBdouble\fP, \fBdouble *\fP) is excluded because no way
was seen to make it safe without complicating the type
resolution.
.LP
The implementation might, as an extension, endow appropriate ones
of the macros that IEEE\ Std\ 1003.1-2001 specifies
only for real arguments with the ability to invoke the complex functions.
.LP
IEEE\ Std\ 1003.1-2001 does not prescribe any particular implementation
mechanism for generic macros. It could be
implemented simply with built-in macros. The generic macro for \fIsqrt\fP(),
for example,
could be implemented with:
.sp
.RS
.nf
\fB#undef sqrt
#define sqrt(x) __BUILTIN_GENERIC_sqrt(x)
\fP
.fi
.RE
.LP
Generic macros are designed for a useful level of consistency with
C++ overloaded math functions.
.LP
The great majority of existing C programs are expected to be unaffected
when the \fI<tgmath.h>\fP header is included
instead of the \fI<math.h>\fP or \fI<complex.h>\fP headers. Generic
macros are similar to the ISO/IEC\ 9899:1999
standard library masking macros, though the semantic types of return
values differ.
.LP
The ability to overload on integer as well as floating types would
have been useful for some functions; for example, \fIcopysign\fP().
Overloading with different numbers of arguments would have allowed
reusing
names; for example, \fIremainder\fP() for \fIremquo\fP(). However,
these facilities would have complicated the specification; and their
natural consistent use, such as for a floating \fIabs\fP() or a two-argument
\fIatan\fP(), would have introduced further inconsistencies with the
ISO/IEC\ 9899:1999 standard
for insufficient benefit.
.LP
The ISO\ C standard in no way limits the implementation's options
for efficiency, including inlining library functions.
.SH FUTURE DIRECTIONS
.LP
None.
.SH SEE ALSO
.LP
\fI<math.h>\fP , \fI<complex.h>\fP , the System Interfaces
volume of IEEE\ Std\ 1003.1-2001, \fIcabs\fP(), \fIfabs\fP(), \fImodf\fP()
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 6, Copyright (C) 2001-2003 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 .
|