C 标准库 <tgmath.h>
系统解析 C99/C11 类型泛型数学头文件 <tgmath.h> 的自动分派规则、实数与复数参数推导、整数默认转 double 规则以及底层 _Generic 实现机理,并在离线 GCC C11 容器结合 _Generic 实测返回类型分派。
学完这一章,你能够
- 系统解析 C99/C11 类型泛型数学头文件 <tgmath.h> 的自动分派规则、实数与复数参数推导、整数默认转 double 规则以及底层 _Generic 实现机理,并在离线 GCC C11 容器结合 _Generic 实测返回类型分派。
类型泛型数学宏的设计初衷与分派规则
在传统 `<math.h>` 与 `<complex.h>` 中,同一个数学运算会因精度与实复数差异分裂出多达 6 个函数名(例如 `sinf`/`sin`/`sinl`/`csinf`/`csin`/`csinl`)。C99 引入的 `<tgmath.h>`(Type-Generic Math)自动包含 `<math.h>` 与 `<complex.h>`,并定义了一套同名类型泛型宏,允许开发者统一书写 `sin(x)`、`sqrt(x)`、`exp(x)` 等简洁调用,由编译器在编译期依据实参类型自动分派至最匹配的底层数学函数,显著提升数值算法的可读性与可维护性。
以 `sin(x)` 与 `sqrt(x)` 为例:当 `x` 为 `float` 时调用 `sinf`/`sqrtf` 并返回 `float`;为 `double` 时调用 `sin`/`sqrt`;为 `long double` 时调用 `sinl`/`sqrtl`;若 `x` 为 `float complex`、`double complex` 或 `long double complex`,则自动路由至对应精度的 `csin*`/`csqrt*` 复变函数并返回对应复数类型。对于多参数宏(如 `pow(x, y)`),若任一参数为复数则选复数版本,且精度向最高实参类型提升(`long double` > `double` > `float`);若传入整型参数(如 `sqrt(4)`),标准明确规定按 `double` 处理。
支持函数清单、实测分派与 _Generic 底层机理
下方三张参考表分别汇总了 `<tgmath.h>` 覆盖的常用三角与双曲函数(13 项,注意 `atan2` 仅定义于实数类型)、指数对数与幂函数(`exp`/`log`/`log10`/`pow`,注意 `log10` 仅定义于实数类型)以及其他高频运算(`sqrt`/`fabs`/`ceil`/`floor`/`fmod`,其中对复数调用 `fabs(z)` 会自动分派至返回实数模长的 `cabs*`,而 `ceil`/`floor`/`fmod` 仅支持实数类型)。
包含 `<tgmath.h>` 后,同一份代码里可直接对 `float f = 1.5f`、`double d = 2.5`、`long double ld = 3.5L` 均写 `sin(...)`,同时对 `double complex z = 1.0 + 2.0 * I` 直接写 `sqrt(z)`(输出 `1.272020 + 0.786151i`)。本站离线 GCC C11 实验进一步结合 C11 `_Generic` 静态类型标签,实测证实 `sin(f)` 返回 `float`、`sin(d)` 返回 `double`、`sin(ld)` 返回 `long double`、`sqrt(4)` 返回 `double`、`sqrt(z)` 与 `exp(z)` 返回 `double complex`。
使用 `<tgmath.h>` 需关注四项要点:仅在 C99 及更高标准下可用;参数类型决定分派目标,编写字面量时应留意后缀(`1.5f` 与 `1.5` 分派不同);整数实参(如 `sqrt(4)`)按标准规则提升为 `double` 版本;按 ISO C 规范,包含 `<tgmath.h>` 本身即会自动包含 `<math.h>` 与 `<complex.h>`(若平台支持复数),因此直接可用 `complex` 与 `I`(独立文档或旧环境显式同时写出 `<complex.h>` 也无妨)。在实现原理上,C99 早期编译器曾依赖内置扩展实现类型分派,而自 C11 标准化 `_Generic` 泛型选择表达式后,标准库即可直接用类似 `#define sin(x) _Generic((x), float: sinf, double: sin, long double: sinl, ...)(x)` 的方式优雅完成零运行时开销的编译期分派。
<tgmath.h> 三角与双曲类型泛型函数参考
| 泛型宏名 | 实数/复数支持与说明 |
| sin | 正弦函数;支持实数(`sinf`/`sin`/`sinl`)与复数(`csinf`/`csin`/`csinl`)。 |
| cos | 余弦函数;支持实数与复数全精度分派。 |
| tan | 正切函数;支持实数与复数全精度分派。 |
| asin | 反正弦函数;支持实数与复数全精度分派。 |
| acos | 反余弦函数;支持实数与复数全精度分派。 |
| atan | 反正切函数;支持实数与复数全精度分派。 |
| atan2 | 双参数象限反正切函数;仅支持实数类型(`atan2f`/`atan2`/`atan2l`)。 |
| sinh | 双曲正弦函数;支持实数与复数全精度分派。 |
| cosh | 双曲余弦函数;支持实数与复数全精度分派。 |
| tanh | 双曲正切函数;支持实数与复数全精度分派。 |
| asinh | 反双曲正弦函数;支持实数与复数全精度分派。 |
| acosh | 反双曲余弦函数;支持实数与复数全精度分派。 |
| atanh | 反双曲正切函数;支持实数与复数全精度分派。 |
<tgmath.h> 指数、对数与幂类型泛型函数参考
| 泛型宏名 | 实数/复数支持与说明 |
| exp | 自然指数函数 `e^x`;支持实数(`exp*`)与复数(`cexp*`)。 |
| log | 自然对数函数 `ln(x)`;支持实数(`log*`)与复数(`clog*`)。 |
| log10 | 以 10 为底的常用对数;仅支持实数类型(`log10f`/`log10`/`log10l`)。 |
| pow | 幂函数 `x^y`;支持实数(`pow*`)与复数(`cpow*`),精度按双参最高类型推导。 |
<tgmath.h> 开方、绝对值与舍入取余类型泛型函数参考
| 泛型宏名 | 实数/复数支持与说明 |
| sqrt | 平方根函数;实数分派至 `sqrt*`,复数分派至 `csqrt*`,整数参数按 `double` 处理。 |
| fabs | 绝对值/模长函数;实数分派至 `fabs*`,复数分派至 `cabs*`(均返回对应实浮点类型)。 |
| ceil | 向上取整函数;仅支持实数类型(`ceilf`/`ceil`/`ceill`)。 |
| floor | 向下取整函数;仅支持实数类型(`floorf`/`floor`/`floorl`)。 |
| fmod | 浮点取余函数;仅支持实数类型(`fmodf`/`fmod`/`fmodl`)。 |
实测 <tgmath.h> 基于 _Generic 验证的实数/整数/复数自动分派与常用运算
展开查看收起
#include <stdio.h>
#include <tgmath.h>
#include <stdbool.h>
#define TYPE_TAG(expr) _Generic((expr), \
float: 1, \
double: 2, \
long double: 3, \
float complex: 4, \
double complex: 5, \
long double complex: 6, \
default: 0)
int main(void) {
float f = 1.5f;
double d = 2.5;
long double ld = 3.5L;
int tf = TYPE_TAG(sin(f));
int td = TYPE_TAG(sin(d));
int tld = TYPE_TAG(sin(ld));
int tint = TYPE_TAG(sqrt(4));
double s_int = sqrt(4);
bool c1_pass = (tf == 1 && td == 2 && tld == 3 && tint == 2 && fabs(s_int - 2.0) < 1e-12);
double complex z = 1.0 + 2.0 * I;
int tz_sqrt = TYPE_TAG(sqrt(z));
int tz_exp = TYPE_TAG(exp(z));
double complex sz = sqrt(z);
double complex sq_back = sz * sz;
double err = fabs(creal(sq_back) - 1.0) + fabs(cimag(sq_back) - 2.0);
bool c2_pass = (tz_sqrt == 5 && tz_exp == 5 && err < 1e-12);
float pf = pow(2.0f, 3.0f);
double lg = log10(1000.0);
double c_val = ceil(2.3);
double f_val = floor(2.7);
double m_val = fmod(5.5, 2.0);
double a2 = atan2(1.0, 1.0);
bool c3_pass = (
TYPE_TAG(pow(2.0f, 3.0f)) == 1 &&
fabs(pf - 8.0f) < 1e-6f &&
fabs(lg - 3.0) < 1e-12 &&
c_val == 3.0 && f_val == 2.0 && fabs(m_val - 1.5) < 1e-12 &&
fabs(a2 - acos(-1.0) / 4.0) < 1e-12
);
printf(
"{"
"\"runtime\":\"GCC C11 (mstorsjo/llvm-mingw:20260616)\","
"\"cases\":["
"{\"name\":\"tgmath_real_type_dispatch\",\"expected\":\"f=1,d=2,ld=3,int=2\",\"actual\":\"f=%d,d=%d,ld=%d,int=%d\",\"passed\":%s},"
"{\"name\":\"tgmath_complex_dispatch\",\"expected\":\"tsqrt=5,texp=5,ok=1\",\"actual\":\"tsqrt=%d,texp=%d,ok=%d\",\"passed\":%s},"
"{\"name\":\"tgmath_exp_log_rounding\",\"expected\":\"pow=8.0,log10=3.0,ceil=3.0,floor=2.0,fmod=1.5\",\"actual\":\"pow=%.1f,log10=%.1f,ceil=%.1f,floor=%.1f,fmod=%.1f\",\"passed\":%s}"
"]"
"}\n",
tf, td, tld, tint, c1_pass ? "true" : "false",
tz_sqrt, tz_exp, (err < 1e-12) ? 1 : 0, c2_pass ? "true" : "false",
(double)pf, lg, c_val, f_val, m_val, c3_pass ? "true" : "false"
);
return 0;
}
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
返回值类型是 `double`(值为 `5.0`)。因为 `<tgmath.h>` 规定对复数参数调用泛型宏 `fabs` 会自动分派至复数模长函数 `cabs`/`cabsf`/`cabsl`,而复数的模长是非负实数。