C Standard Library<tgmath.h>
<tgmath.h>is a header file in the C standard library, providingType-generic math functions(Type-Generic Math Functions)。
<tgmath.h>The library was introduced in the C99 standard, allowing developers to use unified function names to call different types of mathematical functions (such asfloat、doubleandlong double), without needing to explicitly specify the specific type of the function.
<tgmath.h>The main purpose is:
-
It simplifies the use of mathematical functions, avoiding the need to define separate functions for different types (such as
float、double、long double) explicitly call different functions (e.g.sinf、sin、sinl)。 -
Improve code readability and maintainability.
-
Mathematical functions supporting complex types.
1、Type-generic macros
<tgmath.h>It defines a set of type-generic macros that automatically select the correct mathematical function based on the type of the argument. For example:
-
sin(x): ifxYesfloat, then callsinf(x); ifxYesdouble, then callsin(x); ifxYeslong double, then callsinl(x)。 -
sqrt(x): ifxYesfloat, then callsqrtf(x); ifxYesdouble, then callsqrt(x); ifxYeslong double, then callsqrtl(x)。
These macros support arguments of the following types:
-
Real types:
float、double、long double。 -
Complex types:
float complex、double complex、long double complex。
2、Supported functions
<tgmath.h>The following type-generic math functions are supported:
Basic mathematical functions
| Function | Description |
|---|---|
sin |
Sine function |
cos |
Cosine function |
tan |
Tangent function |
asin |
Arcsine function |
acos |
Arccosine function |
atan |
Arctangent function |
atan2 |
Two-argument arctangent function |
sinh |
Hyperbolic sine function |
cosh |
Hyperbolic cosine function |
tanh |
Hyperbolic tangent function |
asinh |
Inverse hyperbolic sine function |
acosh |
Inverse hyperbolic cosine function |
atanh |
Inverse hyperbolic tangent function |
Exponential and logarithmic functions
| Function | Description |
|---|---|
exp |
Exponential function |
log |
Natural logarithm function |
log10 |
Common logarithm function |
pow |
power function |
Other functions
| Function | Description |
|---|---|
sqrt |
square root function |
fabs |
Absolute value function |
ceil |
Ceiling function |
floor |
Floor function |
fmod |
Floating-point remainder function |
3、Example
The following is an example using<tgmath.h>is an example that demonstrates how to use type-generic mathematical functions:
Example
#include <tgmath.h>
int main() {
// real types
float f = 1.5f;
double d = 2.5;
long double ld = 3.5L;
// use type-generic functions
printf("sin(f) = %f\n", sin(f)); // call sinf
printf("sin(d) = %f\n", sin(d)); // call sin
printf("sin(ld) = %Lf\n", sin(ld)); // call sinl
// complex types
double complex z = 1.0 + 2.0 * I;
// use type-generic functions
double complex sqrt_z = sqrt(z);
printf("sqrt(z) = %f + %fi\n", creal(sqrt_z), cimag(sqrt_z));
return 0;
}
Output result:
sin(f) = 0.997495 sin(d) = 0.598472 sin(ld) = 0.350783 sqrt(z) = 1.272020 + 0.786151i
4、Notes
-
<tgmath.h>It is only available in C99 and later versions. -
Type-generic macros automatically select the correct function based on the type of the argument, so the type of the argument must be clear.
-
If the argument type is ambiguous (e.g., an integer constant), the compiler may choose the default
doubleversion. -
Functions for complex types need to include
<complex.h>Header file.
5、Implementation principle
<tgmath.h>The implementation relies on C's generic selection mechanism (_Generickeyword). For example,sinThe macro definition may be as follows:
#define sin(x) _Generic((x), \
float: sinf, \
double: sin, \
long double: sinl \
)(x)
_Generickeyword based onxof the type to select the correct function.
<tgmath.h>It provides type-generic mathematical functions, simplifying calls for different numeric types (such asfloat、double、long doubleand complex types). It is an important feature in the C99 standard, especially suitable for scientific computing and engineering applications that need to handle multiple numeric types. By using<tgmath.h>, developers can write more concise and more generic code.