tgmath.h是C標準函数庫中的头文件,提供了数学函数的类型通用的宏定义。使用这些宏调用数学函数时,会根据参数自动对应到类型适合的数学函数,其效果类似于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
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)
参考文献
评论 (0)