Share via


泛型數學

針對 ISO C Standard 11 (C11) 和更新版本,標頭 <tgmath.h> 除了包含 <math.h><complex.h> 之外,還提供根據參數類型叫用對應數學函式的宏。

C 執行時間程式庫數學函式會以真實且複雜的變體提供。 每個變體都有三種變體,視引數的類型而定: floatdoublelong double 。 因為 C 不支援像 C++ 一樣多載,因此每個變體都有不同的名稱。 例如,若要取得實際浮點值的絕對值,您可以分別呼叫 fabsffabs 或 ,或 fabsl 取決於您要分別傳遞 floatdoublelong double 值。 若要取得複雜絕對值,您可以分別呼叫 、 cabsfcabs 或 的其中一個 float , 或 cabsl ,視您是否分別傳遞 、 doublelong double 複雜值而定。 如果引數不符合上述任何類型,則會選擇函式,就像引數是雙精度浮點數一樣。

<tgmath.h> 包含宏,可簡化要呼叫的正確數學函式選取範圍。 宏會檢查其傳遞的類型,然後呼叫正確的函式。 例如, sqrt 宏會 sqrt(9.9f) 系結至 sqrtf() ,但它會 sqrt(9.9) 系結至 sqrt() 。 如果泛型參數至少有一個宏引數很複雜,則宏會系結至複雜函式;否則,它會叫用實際函式。

中的 <tgmath.h> 類型泛型宏可讓您撰寫更多可攜式程式碼,因為您不需要根據引數類型來管理轉換或選取不同的函式名稱。

這些宏位於自己的標頭中,因此使用 <math.h> 標頭撰寫的程式不會中斷。 因此 double x = sin(42); ,當您包含 < math.h > 時,其行為一如既往。 即便如此,當標頭包含 而不是 <math.h><complex.h><tgmath.h> ,大部分現有的 C 程式應該不會受到影響。

下表列出 中 <tgmath.h> 可用的宏,以及其展開範圍。 modf 未包含在此資料表中,因為它沒有對應的類型泛型宏,因為它不清楚如何使其安全,而不會使類型解析複雜化。

Macro 真正
float
真正
double
真正
long double
複雜
float
複雜
double
複雜
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。 如需安裝和使用適用于 C11 和 C17 開發的 SDK 的指示,請參閱 在 Visual Studio 中安裝 C11 和 C17 支援。

另請參閱

C 執行時間程式庫參考