C Standard Library Tgmath H
`` is a header file in the C standard library that provides **Type-Generic Math Functions**.
`` was introduced in the C99 standard, allowing developers to use unified function names to call different types of mathematical functions (such as `float`, `double`, and `long double`) without explicitly specifying the exact type of the function.
The main purposes of `` are:
* Simplify the use of mathematical functions, avoiding the need to explicitly call different functions for different types (such as `sinf`, `sin`, `sinl`).
* Improve code readability and maintainability.
* Support mathematical functions for complex number types.
* * *
### 1γ**Type-Generic Macros**
`` 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)`: If `x` is `float`, it calls `sinf(x)`; if `x` is `double`, it calls `sin(x)`; if `x` is `long double`, it calls `sinl(x)`.
* `sqrt(x)`: If `x` is `float`, it calls `sqrtf(x)`; if `x` is `double`, it calls `sqrt(x)`; if `x` is `long double`, it calls `sqrtl(x)`.
These macros support the following types of arguments:
* Real number types: `float`, `double`, `long double`.
* Complex number types: `float complex`, `double complex`, `long double complex`.
* * *
### 2γ**Supported Functions**
`` supports the following type-generic mathematical functions:
#### 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 ``, demonstrating how to use type-generic mathematical functions:
## Example
#include
#include
int main(){
// Real number 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));// calls sinf
printf("sin(d) = %f\n",sin(d));// calls sin
printf("sin(ld) = %Lf\n",sin(ld));// calls sinl
// Complex number 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**:
sin(f) = 0.997495 sin(d) = 0.598472 sin(ld) = 0.350783 sqrt(z) = 1.272020 + 0.786151i
### 4γ**Notes**
* `` 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 explicit.
* If the argument type is ambiguous (for example, integer constants), the compiler may choose the default `double` version.
* Functions for complex number types require the `` header file.
* * *
### 5γ**Implementation Principle**
The implementation of `` relies on the generic selection mechanism in C (`_Generic` keyword). For example, the definition of the `sin` macro might be as follows:
#define sin(x) _Generic((x), \ float: sinf, \ double: sin, \ long double: sinl \ )(x)
The `_Generic` keyword selects the correct function based on the type of `x`.
`` provides type-generic mathematical functions, simplifying the calling of mathematical functions for different numeric types (such as `float`, `double`, `long double`, and complex types). It is an important feature in the C99 standard, particularly suitable for scientific computing and engineering applications that need to handle multiple numeric types. By using ``, developers can write cleaner and more generic code.
YouTip