?
本文檔使用 php中文網手冊 發(fā)布
頭文件<tgmath.h>包含頭文件<math.h>和<complex.h>,并定義了幾個類型泛型宏,它們根據參數的類型確定哪些實際的或適用的復雜函數。
對于每個宏,參數的相應的實數類型在未被混合的math.h函數中是double的,被稱為泛型參數(例如pow的兩個參數都是泛型參數,但只有scalbn的第一個參數是泛型參數)。
當使用<tgmath.h>宏時,傳遞給泛型參數的參數類型決定了如下所述由宏選擇哪個函數。 如果參數的類型與所選函數的參數類型不兼容,則行為是未定義的(例如,如果將一個復雜參數傳遞給實數tgmath宏:float complex fc; ceil(fc)或double complex dc ; double d; fmax(dc,d)是未定義行為的示例)。
注意:類型通用宏在C99中以實現定義的方式實現,但C11關鍵字_Generic使得可以以便攜方式實現這些宏。
對于既有真實對象又有復雜對象的所有函數,都存在一個類型通用宏XXX,它調用以下任一項:
實際功能:
float
變型 XXXf
double
變型 XXX
long double
變型 XXXl
復雜功能:
float
變型 cXXXf
double
變型 cXXX
long double
變型 cXXXl
上述規(guī)則的一個例外是晶圓廠宏(參見下表)。
要調用的函數如下確定:
如果通用參數的任何參數都是虛構的,則在每個函數參考頁面上分別指定行為(特別是,sin,cos,tag,cosh,sinh,tanh,asin,atan,asinh和atanh稱為實函數, sin,tan,sinh,tanh,asin,atan,asinh和atanh的返回類型是虛構的,cos和cosh的返回類型是實數)
如果通用參數的任何參數都很復雜,則調用復雜函數,否則調用實函數。
如果通用參數的任何參數是long double
,則long double
調用該變體。否則,如果任何參數是double
或整數,則double
調用該變體。否則,float
調用變體。
類型通用宏如下所示:
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 |
對于沒有復雜對象的所有函數,除了modf之外,還有一個類型通用宏XXX存在,它調用實函數的變體之一:
float
變型 XXXf
double
變型 XXX
long double
變型 XXXl
要調用的函數如下確定:
如果通用參數的任何參數是long double,則調用long double變體。 否則,如果泛型參數的任何參數是雙精度型,則調用雙精度型。 否則,調用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 |
對于所有沒有實際對應項的復數函數,都存在一個類型通用宏cXXX,它調用復雜函數的任何一種變體:
float
complex
變型 cXXXf
double
complex
變型 cXXX
long
double
complex
變型 cXXXl
要調用的函數如下確定:
如果通用參數的任何參數是實數,復數或虛數,則調用相應的復數函數。
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標準(ISO / IEC 9899:2011):
7.25類型通用數學<tgmath.h>(p:373-375)
C99標準(ISO / IEC 9899:1999):
7.22類型通用數學<tgmath.h>(p:335-337)