当前位置: C语言 -- 标准库 -- <tgmath.h>

<tgmath.h>头文件(二)

对于<math.h>头文件中无后缀的函数(<math.h>头文件中将结果舍入到更窄类型的函数、modf函数、canonicalize函数除外。),如果<complex.h>头文件中没有同名函数(存在字母前缀c。),对应的泛型宏与<math.h>头文件中无后缀函数同名。

上述函数与泛型宏的对应关系如下表所示:

泛型宏 <math.h>头文件中函数
float double long double _Decimal32 _Decimal64 _Decimal128
acospi acospif acospi acospil acospid32 acospid64 acospid128
asinpi asinpif asinpi asinpil asinpid32 asinpid64 asinpid128
atan2pi atan2pif atan2pi atan2pil atan2pid32 atan2pid64 atan2pid128
atan2 atan2f atan2 atan2l atan2d32 atan2d64 atan2d128
atanpi atanpif atanpi atanpil atanpid32 atanpid64 atanpid128
cbrt cbrtf cbrt cbrtl cbrtd32 cbrtd64 cbrtd128
ceil ceilf ceil ceill ceild32 ceild64 ceild128
compoundn compoundnf compoundn compoundnl compoundnd32 compoundnd64 compoundnd128
copysign copysignf copysign copysignl copysignd32 copysignd64 copysignd128
cospi cospif cospi cospil cospid32 cospid64 cospid128
erf erff erf erfl erfd32 erfd64 erfd128
erfc erfcf erfc erfcl erfcd32 erfcd64 erfcd128
exp10m1 exp10m1f exp10m1 exp10m1l exp10m1d32 exp10m1d64 exp10m1d128
exp10 exp10f exp10 exp10l exp10d32 exp10d64 exp10d128
exp2m1 exp2m1f exp2m1 exp2m1l exp2m1d32 exp2m1d64 exp2m1d128
exp2 exp2f exp2 exp2l exp2d32 exp2d64 exp2d128
expm1 expm1f expm1 expm1l expm1d32 expm1d64 expm1d128
fdim fdimf fdim fdiml fdimd32 fdimd64 fdimd128
floor floorf floor floorl floord32 floord64 floord128
fma fmaf fma fmal fmad32 fmad64 fmad128
fmax fmaxf fmax fmaxl fmaxd32 fmaxd64 fmaxd128
fmaximum fmaximumf fmaximum fmaximuml fmaximumd32 fmaximumd64 fmaximumd128
fmaximum_mag fmaximum_magf fmaximum_mag fmaximum_magl fmaximum_magd32 fmaximum_magd64 fmaximum_magd128
fmaximum_num fmaximum_numf fmaximum_num fmaximum_numl fmaximum_numd32 fmaximum_numd64 fmaximum_numd128
fmaximum_mag_num fmaximum_mag_numf fmaximum_mag_num fmaximum_mag_numl fmaximum_mag_numd32 fmaximum_mag_numd64 fmaximum_mag_numd128
fmin fminf fmin fminl fmind32 fmind64 fmind128
fminimum fminimumf fminimum fminimuml fminimumd32 fminimumd64 fminimumd128
fminimum_mag fminimum_magf fminimum_mag fminimum_magl fminimum_magd32 fminimum_magd64 fminimum_magd128
fminimum_num fminimum_numf fminimum_num fminimum_numl fminimum_numd32 fminimum_numd64 fminimum_numd128
fminimum_mag_num fminimum_mag_numf fminimum_mag_num fminimum_mag_numl fminimum_mag_numd32 fminimum_mag_numd64 fminimum_mag_numd128
fmod fmodf fmod fmodl fmodd32 fmodd64 fmodd128
frexp frexpf frexp frexpl frexpd32 frexpd64 frexpd128
fromfpx fromfpxf fromfpx fromfpxl fromfpxd32 fromfpxd64 fromfpxd128
fromfp fromfpf fromfp fromfpl fromfpd32 fromfpd64 fromfpd128
hypot hypotf hypot hypotl hypotd32 hypotd64 hypotd128
ilogb ilogbf ilogb ilogbl ilogbd32 ilogbd64 ilogbd128
ldexp ldexpf ldexp ldexpl ldexpd32 ldexpd64 ldexpd128
lgamma lgammaf lgamma lgammal lgammad32 lgammad64 lgammad128
llogb llogbf llogb llogbl llogbd32 llogbd64 llogbd128
llrint llrintf llrint llrintl llrintd32 llrintd64 llrintd128
llround llroundf llround llroundl llroundd32 llroundd64 llroundd128
log10p1 log10p1f log10p1 log10p1l log10p1d32 log10p1d64 log10p1d128
log10 log10f log10 log10l log10d32 log10d64 log10d128
log1p log1pf log1p log1pl log1pd32 log1pd64 log1pd128
log2p1 log2p1f log2p1 log2p1l log2p1d32 log2p1d64 log2p1d128
log2 log2f log2 log2l log2d32 log2d64 log2d128
logb logbf logb logbl logbd32 logbd64 logbd128
logp1 logp1f logp1 logp1l logp1d32 logp1d64 logp1d128
lrint lrintf lrint lrintl lrintd32 lrintd64 lrintd128
lround lroundf lround lroundl lroundd32 lroundd64 lroundd128
nearbyint nearbyintf nearbyint nearbyintl nearbyintd32 nearbyintd64 nearbyintd128
nextafter nextafterf nextafter nextafterl nextafterd32 nextafterd64 nextafterd128
nextdown nextdownf nextdown nextdownl nextdownd32 nextdownd64 nextdownd128
nexttoward nexttowardf nexttoward nexttowardl nexttowardd32 nexttowardd64 nexttowardd128
nextup nextupf nextup nextupl nextupd32 nextupd64 nextupd128
pown pownf pown pownl pownd32 pownd64 pownd128
powr powrf powr powrl powrd32 powrd64 powrd128
remainder remainderf remainder remainderl remainderd32 remainderd64 remainderd128
remquo remquof remquo remquol
rint rintf rint rintl rintd32 rintd64 rintd128
rootn rootnf rootn rootnl rootnd32 rootnd64 rootnd128
roundeven roundevenf roundeven roundevenl roundevend32 roundevend64 roundevend128
round roundf round roundl roundd32 roundd64 roundd128
rsqrt rsqrtf rsqrt rsqrtl rsqrtd32 rsqrtd64 rsqrtd128
scalbln scalblnf scalbln scalblnl scalblnd32 scalblnd64 scalblnd128
scalbn scalbnf scalbn scalbnl scalbnd32 scalbnd64 scalbnd128
sinpi sinpif sinpi sinpil sinpid32 sinpid64 sinpid128
tanpi tanpif tanpi tanpil tanpid32 tanpid64 tanpid128
tgamma tgammaf tgamma tgammal tgammad32 tgammad64 tgammad128
trunc truncf trunc truncl truncd32 truncd64 truncd128
ufromfpx ufromfpxf ufromfpx ufromfpxl fromfpxd32 fromfpxd64 fromfpxd128
ufromfp ufromfpf ufromfp ufromfpl ufromfpd32 ufromfpd64 ufromfpd128

如果上述泛型宏中所有实参都是实数,使用泛型宏将调用实数函数;否则使用上述泛型宏将导致未定义行为。


以泛型宏ceil为例:

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
/*泛型宏ceil范例*/

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

int main(void)
{
    /*调用<math.h>头文件中的ceil函数。*/
    printf("ceil(1.3) = %.2f\n", ceil(1.3));

    /*调用<math.h>头文件中的ceilf函数。*/
    printf("ceil(1.3f) = %.2f\n", ceil(1.3f));

    /*调用<math.h>头文件中的ceill函数。*/
    printf("ceil(1.3L) = %.2Lf\n", ceil(1.3L));
    
    #if __STDC_VERSION_MATH_H__ >= 202311L && defined(__STDC_IEC_60559_DFP__)
        /*调用<math.h>头文件中的ceild32函数。*/
        printf("ceil(1.3df) = %.2Hf\n", ceil(1.3df));
        
        /*调用<math.h>头文件中的ceild64函数。*/
        printf("ceil(1.3dd) = %.2Df\n", ceil(1.3dd));
        
        /*调用<math.h>头文件中的ceild128函数。*/
        printf("ceil(1.3dl) = %.2DDf\n", ceil(1.3dl));
    #else
        puts("The implementation does not support decimal floating-point types.");
    #endif
 
    return 0;
}


输出:

ceil(1.3) = 2.00

ceil(1.3f) = 2.00

ceil(1.3L) = 2.00

The implementation does not support decimal floating-point types.


对于<complex.h>头文件中无后缀的函数,如果<math.h>头文件中没有同名函数(仅在字母前缀c上存在差异。),对应的泛型宏与<complex.h>头文件中无后缀的函数同名。

上述函数与泛型宏的对应关系如下表所示:

泛型宏 <complex.h>头文件中函数
float double long double
carg cargf carg cargl
cimag cimagf cimag cimagl
conj conjf conj conjl
cproj cprojf cproj cprojl
creal crealf creal creall

上述泛型宏中的实参无论是标准浮点类型还是复数类型,都将调用复数函数;如果实参是十进制浮点类型,其行为是未定义的。


以泛型宏conj为例:

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
/*泛型宏conj范例*/

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

/*判断数据类型的宏。*/
#define TYPE(x) _Generic((x),  \
                         float complex:"float complex type",  \
                         double complex:"double complex type",  \
                         long double complex:"long double complex type",  \
                         default:"unknown type"  \
                         )

int main(void)
{
    /*调用<complex.h>头文件的conj函数。*/
    printf("conj(1.0): %s\n", TYPE(conj(1.0)));

    /*调用<complex.h>头文件的conj函数。*/
    printf("conj(1.0+1.0I): %s\n", TYPE(conj(1.0+1.0I)));
 
    return 0;
}


输出:

conj(1.0): double complex type

conj(1.0+1.0I): double complex type