| Contents |

27 <tgmath.h> Hàm Toán Type-Generic

Đây là các macro type-generic wrap quanh các hàm toán trong <math.h><complex.h>. Header này include cả hai cái đó.

Nhưng nhìn bề mặt, bạn có thể nghĩ về chúng như khả năng dùng, ví dụ, hàm sqrt() với bất kỳ kiểu nào mà không cần nghĩ xem nó là double hay long double hay thậm chí complex.

Đây là các macro được định nghĩa—vài cái không có đối ứng trong không gian thực hoặc phức. Type suffix được bỏ qua trong bảng ở các cột Real và Complex. Không macro generic nào có type suffix.

Hàm Thực Hàm Phức Macro Generic
acos cacos acos
asin casin asin
atan catan atan
acosh cacosh acosh
asinh casinh asinh
atanh catanh atanh
cos ccos cos
sin csin sin
tan ctan tan
cosh ccosh cosh
sinh csinh sinh
tanh ctanh tanh
exp cexp exp
log clog log
pow cpow pow
sqrt csqrt sqrt
fabs cabs fabs
atan2 atan2
fdim fdim
cbrt cbrt
floor floor
ceil ceil
fma fma
copysign copysign
fmax fmax
erf erf
fmin fmin
erfc erfc
fmod fmod
exp2 exp2
frexp frexp
expm1 expm1
hypot hypot
ilogb ilogb
ldexp ldexp
lgamma lgamma
llrint llrint
llround llround
log10 log10
log1p log1p
log2 log2
logb logb
lrint lrint
lround lround
nearbyint nearbyint
nextafter nextafter
nexttoward nexttoward
remainder remainder
remquo remquo
rint rint
round round
scalbn scalbn
scalbln scalbln
tgamma tgamma
trunc trunc
carg carg
cimag cimag
conj conj
cproj cproj
creal creal

27.1 Ví dụ

Đây là ví dụ ta gọi hàm sqrt() type-generic trên nhiều kiểu khác nhau.

#include <stdio.h>
#include <tgmath.h>

int main(void)
{
    double x = 12.8;
    long double y = 34.9;
    double complex z = 1 + 2 * I;

    double x_result;
    long double y_result;
    double complex z_result;

    // Ta gọi cùng hàm sqrt()--nó là type-generic!
    x_result = sqrt(x);
    y_result = sqrt(y);
    z_result = sqrt(z);

    printf("x_result: %f\n", x_result);
    printf("y_result: %Lf\n", y_result);
    printf("z_result: %f + %fi\n", creal(z_result), cimag(z_result));
}

Output:

x_result: 3.577709
y_result: 5.907622
z_result: 1.272020 + 0.786151i

| Contents |