NAME
tgmath.h - type-generic macros
SYNOPSIS
#include
DESCRIPTION
The
Of the functions contained whitin the
Use of the macro invokes function whose generic paramenters has type long double, the type determined is long double.
* First, if any argument for generic paramenters has type long double, the type determined is long double.
* Otherwise, if any argument for generic paramenters has type double or is of integer type, the type determined is double.
* Otherwise, the type determined is float.
For each unsuffixed function in the
Function Function Macro
acos() cacos() acos()
asin() casin() asin()
atan() catan() atan()
acosh() cacosh() acosh()
asinh() casinh() asinh()
atanh() catanh() atanh()
cos() ccos() cos()
sin() csin() sin()
tan() ctan() tan()
cosh() ccosh() cosh()
sinh() csinh() sinh()
If at leasr one argument for a generic paramenter is complex, then use if the macro invokes a complex function; otherwise, use of the macro invokes a real function.
For each unsuffixed function in the
If all arguments for gemeric paramenters are real, then use of the macro invokes a real function; otherwise, use of the macro results in undefined behavior.
For each unsuffixed function in the
Use of the macro with any real or complex argument invokes a complex function.
The following sections are informative.
APPLICATION USAGE
With the declaration:
#include
int n;
float f;
double d;
long double ld;
float complex fc;
double complex dc;
long doublee complex ldc;
function invoked by use of type-generic macros are shown in the following table:
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 proting or adjusting precision. Generic math function are a widely appreciated feature of Fortran.
The only arguments that affect the type resolution are the arguments corrsponding to the parameters that have double in the synopsis. Hence the type of a type-generic call to nexttoward(), whose second paramenter is long double in the synopsis, is determined solely by the type of the first argument.
The term "type-generic" was chosen over 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.
The macros are placed in their own header in order not to silently break old programs that include the
printf("%e", sin(x))
modf(double, double *) is excluded because no way was seen to make is safe without complicating the type resolution.
The implementation might, as an extension, endow appropriate ones of the macros that IEEE Std 10003.1-2001 specifies only only for real atguments with the abliity to invoke the complex function.
IEEE Std 1003.1-2001 does not prescribe any particular implementation mechanism for generic macros. It could implemented simply with built-in macros. The generic macros. It could be implemented simply with build-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
The ability to overload on integer as well as flowing 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 eith the ISO/IEC 9899:1999 standard fir insufficient benefit.
The ISO C standard in no way limits the implementstion's options for efficiency, including inlining library functions.
FUTURE DIRECTIONS
None.
SEE ALSO