Share via


泛型类型数学

对于 ISO C 标准 11 (C11) 及更高版本,标头 <tgmath.h> 除了包括 <math.h><complex.h> 之外,还提供了根据参数类型调用相应数学函数的宏。

C 运行时库数学函数有实变函数和复变函数两种变体。 每个变体有三种风格,具体取决于参数的类型:floatdoublelong double。 由于 C 不支持像 C++ 一样重载,因此每个变体都有不同的名称。 例如,若要获取实际浮点值的绝对值,需要分别调用 fabsffabsfabsl,具体取决于传递的是 floatdouble、还是 long double 值。 若要获取复杂绝对值,需要调用 cabsfcabscabsl 值之一,具体取决于传递的是 floatdouble 还是 long double 复杂值。 如果参数与上述任何类型都不匹配,则选择该函数时,将参数视为双精度数。

<tgmath.h> 包含的宏可用于在调用正确数学函数时简化选择。 宏检查所传递的类型,然后调用正确的函数。 例如,宏 sqrtsqrt(9.9f) 绑定到 sqrtf(),而将 sqrt(9.9) 绑定到 sqrt()。 如果泛型参数至少有一个复杂的宏参数,则宏会绑定到复杂函数;否则,会调用一个实际函数。

<tgmath.h> 中使用泛型宏可以编写更易于移植的代码,因为无需根据参数类型来管理强制转换或选择不同的函数名称。

这些宏位于其自己的标头中,以便使用 <math.h> 标头编写的程序不会中断。 因此,包含 <math.h> 时,double x = sin(42); 表现如常。 即便如此,在包含标头 <tgmath.h> 而非 <math.h><complex.h> 时,都不想大多数现有的 C 程序受到影响。

下表列出了 <tgmath.h> 中可用的宏及其扩展内容。 modf 不包含在此表中,因为它没有相应的泛型宏,不清楚如何在不复杂化类型解析的情况下保证安全。

Real
float
Real
double
Real
long double
Complex
float
Complex
double
Complex
long double
acos acosf acos acosl cacosf cacos cacosl
acosh acoshf acosh acoshl cacoshf cacosh cacoshl
asin asinf asin asinl casinf casin casinl
asinh asinhf asinh asinhl casinhf casinh casinhl
atan atanf atan atanl catanf catan catanl
atanh atanhf atanh atanhl catanhf catanh catanhl
cos cosf cos cosl ccosf ccos ccosl
cosh coshf cosh coshl ccoshf ccosh ccoshl
exp expf exp expl cexpf cexp cexpl
fabs fabsf fabs fabsl cabsf cabs cabsl
log logf log logl clogf clog clogl
pow powf pow powl cpowf cpow cpowl
sin sinf sin sinl csinf csin csinl
sinh sinhf sinh sinhl csinhf csinh csinhl
sqrt sqrtf sqrt sqrtl csqrtf csqrt csqrtl
tan tanf tan tanl ctanf ctan ctanl
tanh tanhf tanh tanhl ctanhf ctanh ctanhl
atan2 atan2f atan2 atan2l - - -
cbrt cbrtf cbrt cbrtl - - -
ceil ceilf ceil ceill - - -
copysign copysignf copysign copysignl - - -
erf erff erf erfl - - -
erfc erfcf erfc erfcl - - -
exp2 exp2f exp2 exp2l - - -
expm1 expm1f expm1 expm1l - - -
fdim fdimf fdim fdiml - - -
floor floorf floor floorl - - -
fma fmaf fma fmal - - -
fmax fmaxf fmax fmaxl - - -
fmin fminf fmin fminl - - -
fmod fmodf fmod fmodl - - -
frexp frexpf frexp frexpl - - -
hypot hypotf hypot hypotl - - -
ilogb ilogbf ilogb ilogbl - - -
ldexp ldexpf ldexp ldexpl - - -
lgamma lgammaf lgamma lgammal - - -
llrint llrintf llrint llrintl - - -
llround llroundf llround llroundl - - -
log10 log10f log10 log10l - - -
log1p log1pf log1p log1pl - - -
log2 log2f log2 log2l - - -
logb logbf logb logbl - - -
lrint lrintf lrint lrintl - - -
lround lroundf lround lroundl - - -
nearbyint nearbyintf nearbyint nearbyintl - - -
nextafter nextafterf nextafter nextafterl - - -
nexttoward nexttowardf nexttoward nexttowardl - - -
remainder remainderf remainder remainderl - - -
remquo remquof remquo remquol - - -
rint rintf rint rintl - - -
round roundf round roundl - - -
scalbln scalblnf scalbln scalblnl - - -
scalbn scalbnf scalbn scalbnl - - -
tgamma tgammaf tgamma tgammal - - -
trunc truncf trunc truncl - - -
carg - - - cargf carg cargl
conj - - - conjf conj conjl
creal - - - crealf creal creall
cimag - - - cimagf cimag cimagl
cproj - - - cprojf cproj cprojl

要求

使用 /std:c11 进行编译。

Windows SDK 10.0.20348.0(版本 2104)或更高版本。 请参阅 Windows SDK 以下载最新的 SDK。 有关安装和使用 SDK 进行 C11 和 C17 开发的说明,请参阅在 Visual Studio 中安装 C11 和 C17 支持

另请参阅

C 运行时库参考