跳至內容

tgmath.h

本頁使用了標題或全文手工轉換
維基百科,自由的百科全書

tgmath.hC標準函數庫中的頭文件,提供了數學函數的類型通用的宏定義。使用這些宏調用數學函數時,會根據參數自動對應到類型適合的數學函數,其效果類似於C++函數重載,使得編程者不必繁瑣地去調用數學庫函數的單精度、雙精度、長雙精度、單精度複數、雙精度複數、長雙精度複數等各個版本。

宏定義[編輯]

math.h與complex.h共有[編輯]

一些常見數學函數既在math.h有實數版本,也在complex.h有複數版本。tgmath.h提供了下述類型通用的宏定義:

  • acos
  • asin
  • atan
  • acosh
  • asinh
  • atanh
  • cos
  • sin
  • tan
  • cosh
  • sinh
  • tanh
  • exp
  • log
  • pow
  • sqrt
  • fabs

math.h專用[編輯]

對於包含在math.h中,但在complex.h中沒有對應的函數,提供了下述通用類型的宏:

  • atan2
  • cbrt
  • ceil
  • copysign
  • erf
  • erfc
  • exp2
  • expm1
  • fdim
  • floor
  • fma
  • fmax
  • fmin
  • fmod
  • frexp
  • hypot
  • ilogb
  • ldexp
  • lgamma
  • llrint
  • llround
  • log10
  • log1p
  • log2
  • logb
  • lrint
  • lround
  • nearbyint
  • nextafter
  • nexttoward
  • remainder
  • remquo
  • rint
  • round
  • scalbn
  • scalbln
  • tgamma
  • trunc

complex.h專用[編輯]

對於包含在complex.h中,但在math.h中沒有對應的函數,提供了下述通用類型的宏:

  • carg
  • cimag
  • conj
  • cproj
  • creal

例子[編輯]

#include <tgmath.h>
int n;
float f;
double d;
long double ld;
float complex fc;
double complex dc;
long double complex ldc;

              //实际调用了:
exp(n);   //exp(n)
acosh(f);   // acoshf(f)
sin(d);   // sin(d) 
atan(ld);   // atanl(ld)
log(fc);   // clogf(fc)
sqrt(dc);   // csqrt(dc)
pow(ldc, f);   // cpowl(ldc, f)
remainder(n, n);   // remainder(n, n) 
nextafter(d, f);   // nextafter(d, f) 
nexttoward(f, ld);   // nexttowardf(f, ld)
copysign(n, ld);   // copysignl(n, ld)
ceil(fc);   // 未定义
rint(dc);   // 未定义
fmax(ldc, ld);   // 未定义
carg(n);   // carg(n) 
cproj(f);   // cprojf(f)
creal(d);   // creal(d) 
cimag(ld);   // cimagl(ld)
cabs(fc);   // cabsf(fc)
carg(dc);   // carg(dc) 
cproj(ldc);   // cprojl(ldc)

參考文獻[編輯]