?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
頭文件<tgmath.h>包含頭文件<math.h>和<complex.h>,并定義了幾個(gè)類型泛型宏,它們根據(jù)參數(shù)的類型確定哪些實(shí)際的或適用的復(fù)雜函數(shù)。
對(duì)于每個(gè)宏,參數(shù)的相應(yīng)的實(shí)數(shù)類型在未被混合的math.h函數(shù)中是double的,被稱為泛型參數(shù)(例如pow的兩個(gè)參數(shù)都是泛型參數(shù),但只有scalbn的第一個(gè)參數(shù)是泛型參數(shù))。
當(dāng)使用<tgmath.h>宏時(shí),傳遞給泛型參數(shù)的參數(shù)類型決定了如下所述由宏選擇哪個(gè)函數(shù)。 如果參數(shù)的類型與所選函數(shù)的參數(shù)類型不兼容,則行為是未定義的(例如,如果將一個(gè)復(fù)雜參數(shù)傳遞給實(shí)數(shù)tgmath宏:float complex fc; ceil(fc)或double complex dc ; double d; fmax(dc,d)是未定義行為的示例)。
注意:類型通用宏在C99中以實(shí)現(xiàn)定義的方式實(shí)現(xiàn),但C11關(guān)鍵字_Generic使得可以以便攜方式實(shí)現(xiàn)這些宏。
對(duì)于既有真實(shí)對(duì)象又有復(fù)雜對(duì)象的所有函數(shù),都存在一個(gè)類型通用宏XXX,它調(diào)用以下任一項(xiàng):
實(shí)際功能:
float
變型 XXXf
double
變型 XXX
long double
變型 XXXl
復(fù)雜功能:
float
變型 cXXXf
double
變型 cXXX
long double
變型 cXXXl
上述規(guī)則的一個(gè)例外是晶圓廠宏(參見下表)。
要調(diào)用的函數(shù)如下確定:
如果通用參數(shù)的任何參數(shù)都是虛構(gòu)的,則在每個(gè)函數(shù)參考頁(yè)面上分別指定行為(特別是,sin,cos,tag,cosh,sinh,tanh,asin,atan,asinh和atanh稱為實(shí)函數(shù), sin,tan,sinh,tanh,asin,atan,asinh和atanh的返回類型是虛構(gòu)的,cos和cosh的返回類型是實(shí)數(shù))
如果通用參數(shù)的任何參數(shù)都很復(fù)雜,則調(diào)用復(fù)雜函數(shù),否則調(diào)用實(shí)函數(shù)。
如果通用參數(shù)的任何參數(shù)是long double
,則long double
調(diào)用該變體。否則,如果任何參數(shù)是double
或整數(shù),則double
調(diào)用該變體。否則,float
調(diào)用變體。
類型通用宏如下所示:
Type-generic macro | Real function variants | Complex function variants | ||||
---|---|---|---|---|---|---|
float | double | long double | float | double | long double | |
fabs | fabsf | fabs | fabsl | cabsf | cabs | cabsl |
exp | expf | exp | expl | cexpf | cexp | cexpl |
log | logf | log | logl | clogf | clog | clogl |
pow | powf | pow | powl | cpowf | cpow | cpowl |
sqrt | sqrtf | sqrt | sqrtl | csqrtf | csqrt | csqrtl |
sin | sinf | sin | sinl | csinf | csin | csinl |
cos | cosf | cos | cosl | ccosf | ccos | ccosl |
tan | tanf | tan | tanl | ctanf | ctan | ctanl |
asin | asinf | asin | asinl | casinf | casin | casinl |
acos | acosf | acos | acosl | cacosf | cacos | cacosl |
atan | atanf | atan | atanl | catanf | catan | catanl |
sinh | sinhf | sinh | sinhl | csinhf | csinh | csinhl |
cosh | coshf | cosh | coshl | ccoshf | ccosh | ccoshl |
tanh | tanhf | tanh | tanhl | ctanhf | ctanh | ctanhl |
asinh | asinhf | asinh | asinhl | casinhf | casinh | casinhl |
acosh | acoshf | acosh | acoshl | cacoshf | cacosh | cacoshl |
atanh | atanhf | atanh | atanhl | catanhf | catanh | catanhl |
對(duì)于沒有復(fù)雜對(duì)象的所有函數(shù),除了modf之外,還有一個(gè)類型通用宏XXX存在,它調(diào)用實(shí)函數(shù)的變體之一:
float
變型 XXXf
double
變型 XXX
long double
變型 XXXl
要調(diào)用的函數(shù)如下確定:
如果通用參數(shù)的任何參數(shù)是long double,則調(diào)用long double變體。 否則,如果泛型參數(shù)的任何參數(shù)是雙精度型,則調(diào)用雙精度型。 否則,調(diào)用float變量。
Type-generic macro | Real function variants | ||
---|---|---|---|
float | double | long double | |
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 |
對(duì)于所有沒有實(shí)際對(duì)應(yīng)項(xiàng)的復(fù)數(shù)函數(shù),都存在一個(gè)類型通用宏cXXX,它調(diào)用復(fù)雜函數(shù)的任何一種變體:
float
complex
變型 cXXXf
double
complex
變型 cXXX
long
double
complex
變型 cXXXl
要調(diào)用的函數(shù)如下確定:
如果通用參數(shù)的任何參數(shù)是實(shí)數(shù),復(fù)數(shù)或虛數(shù),則調(diào)用相應(yīng)的復(fù)數(shù)函數(shù)。
Type-generic macro | Complex function variants | ||
---|---|---|---|
float | double | long double | |
carg | cargf | carg | cargl |
conj | conjf | conj | conjl |
creal | crealf | creal | creall |
cimag | cimagf | cimag | cimagl |
cproj | cprojf | cproj | cprojl |
#include <stdio.h>#include <tgmath.h> int main(void){ int i = 2; printf("sqrt(2) = %f\n", sqrt(i)); // argument type is int, calls sqrt float f = 0.5; printf("sin(0.5f) = %f\n", sin(f)); // argument type is float, calls sinf float complex dc = 1 + 0.5*I; float complex z = sqrt(dc); // argument type is float complex, calls csqrtf printf("sqrt(1 + 0.5i) = %f+%fi\n", creal(z), // argument type is float complex, calls crealf cimag(z)); // argument type is float complex, calls cimagf}
輸出:
sqrt(2) = 1.414214sin(0.5f) = 0.479426sqrt(1 + 0.5i) = 1.029086+0.242934i
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.25類型通用數(shù)學(xué)<tgmath.h>(p:373-375)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.22類型通用數(shù)學(xué)<tgmath.h>(p:335-337)