C 标准库 <tgmath.h>
<tgmath.h> 是 C 标准库中的一个头文件,提供了类型泛型数学函数(Type-Generic Math Functions)。
<tgmath.h> 库在 C99 标准中引入,允许开发者使用统一的函数名来调用不同类型的数学函数(如 float、double 和 long double),而无需显式指定函数的具体类型。
<tgmath.h> 的主要目的是:
- 
        
简化数学函数的使用,避免为不同类型(如
float、double、long double)显式调用不同的函数(如sinf、sin、sinl)。 - 
        
提高代码的可读性和可维护性。
 - 
        
支持复数类型的数学函数。
 
1、类型泛型宏
<tgmath.h> 定义了一组类型泛型宏,这些宏根据参数的类型自动选择正确的数学函数。例如:
- 
        
sin(x):如果x是float,则调用sinf(x);如果x是double,则调用sin(x);如果x是long double,则调用sinl(x)。 - 
        
sqrt(x):如果x是float,则调用sqrtf(x);如果x是double,则调用sqrt(x);如果x是long double,则调用sqrtl(x)。 
这些宏支持以下类型的参数:
- 
        
实数类型:
float、double、long double。 - 
        
复数类型:
float complex、double complex、long double complex。 
2、支持的函数
<tgmath.h> 支持以下类型泛型数学函数:
基本数学函数
| 函数 | 描述 | 
|---|---|
sin | 
            正弦函数 | 
cos | 
            余弦函数 | 
tan | 
            正切函数 | 
asin | 
            反正弦函数 | 
acos | 
            反余弦函数 | 
atan | 
            反正切函数 | 
atan2 | 
            两个参数的反正切函数 | 
sinh | 
            双曲正弦函数 | 
cosh | 
            双曲余弦函数 | 
tanh | 
            双曲正切函数 | 
asinh | 
            反双曲正弦函数 | 
acosh | 
            反双曲余弦函数 | 
atanh | 
            反双曲正切函数 | 
指数和对数函数
| 函数 | 描述 | 
|---|---|
exp | 
            指数函数 | 
log | 
            自然对数函数 | 
log10 | 
            常用对数函数 | 
pow | 
            幂函数 | 
其他函数
| 函数 | 描述 | 
|---|---|
sqrt | 
            平方根函数 | 
fabs | 
            绝对值函数 | 
ceil | 
            向上取整函数 | 
floor | 
            向下取整函数 | 
fmod | 
            浮点数取余函数 | 
3、实例
以下是一个使用 <tgmath.h> 的示例,展示了如何使用类型泛型数学函数:
实例
#include <tgmath.h>
int main() {
// 实数类型
float f = 1.5f;
double d = 2.5;
long double ld = 3.5L;
// 使用类型泛型函数
printf("sin(f) = %f\n", sin(f)); // 调用 sinf
printf("sin(d) = %f\n", sin(d)); // 调用 sin
printf("sin(ld) = %Lf\n", sin(ld)); // 调用 sinl
// 复数类型
double complex z = 1.0 + 2.0 * I;
// 使用类型泛型函数
double complex sqrt_z = sqrt(z);
printf("sqrt(z) = %f + %fi\n", creal(sqrt_z), cimag(sqrt_z));
return 0;
}
输出结果:
sin(f) = 0.997495 sin(d) = 0.598472 sin(ld) = 0.350783 sqrt(z) = 1.272020 + 0.786151i
4、注意事项
- 
        
<tgmath.h>仅在 C99 及更高版本中可用。 - 
        
类型泛型宏根据参数的类型自动选择正确的函数,因此参数的类型必须明确。
 - 
        
如果参数类型不明确(例如,整数常量),编译器可能会选择默认的
double版本。 - 
        
复数类型的函数需要包含
<complex.h>头文件。 
5、实现原理
<tgmath.h> 的实现依赖于 C 语言的泛型选择机制(_Generic 关键字)。例如,sin 宏的定义可能如下:
#define sin(x) _Generic((x), \
    float: sinf,             \
    double: sin,             \
    long double: sinl        \
)(x)
_Generic 关键字根据 x 的类型选择正确的函数。
<tgmath.h> 提供了类型泛型数学函数,简化了不同数值类型(如 float、double、long double 和复数类型)的数学函数调用。它是 C99 标准中的一个重要特性,特别适用于需要处理多种数值类型的科学计算和工程应用。通过使用 <tgmath.h>,开发者可以编写更简洁、更通用的代码。
 
       
点我分享笔记