Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Type-generic math(since C99)

      From cppreference.com
      <c‎ |numeric
       
       
       

      The header<tgmath.h> includes the headers<math.h> and<complex.h> and defines severaltype-generic macros that determine which real or, when applicable, complex function to call based on the types of the arguments.

      For each macro, the parameters whose corresponding real type in the unsuffixed<math.h> function isdouble are known asgeneric parameters (for example, both parameters ofpow are generic parameters, but only the first parameter ofscalbn is a generic parameter).

      When a<tgmath.h>'s macro is used the types of the arguments passed to the generic parameters determine which function is selected by the macro as described below. If the types of the arguments are notcompatible with the parameter types of the selected function, the behavior is undefined (e.g. if a complex argument is passed into a real-only<tgmath.h>'s macro:floatcomplex fc;ceil(fc); ordoublecomplex dc;double d;fmax(dc, d); are examples of undefined behavior).

      Note: type-generic macros were implemented in implementation-defined manner in C99, but C11 keyword_Generic makes it possible to implement these macros in portable manner.

      Contents

      [edit]Complex/real type-generic macros

      For all functions that have both real and complex counterparts, a type-generic macroXXX exists, which calls either of:

      • real function:
      • float variantXXXf
      • double variantXXX
      • longdouble variantXXXl
      • complex function:
      • float variantcXXXf
      • double variantcXXX
      • longdouble variantcXXXl

      An exception to the above rule is thefabs macro (see the table below).

      The function to call is determined as follows:

      • If any of the arguments for the generic parameters is imaginary, the behavior is specified on each function reference page individually (in particular,sin,cos,tan,cosh,sinh,tanh,asin,atan,asinh, andatanh callreal functions, the return types ofsin,tan,sinh,tanh,asin,atan,asinh, andatanh are imaginary, and the return types ofcos andcosh are real).
      • If any of the arguments for the generic parameters is complex, then the complex function is called, otherwise the real function is called.
      • If any of the arguments for the generic parameters islongdouble, then thelongdouble variant is called. Otherwise, if any of the parameters isdouble or integer, then thedouble variant is called. Otherwise,float variant is called.

      The type-generic macros are as follows:

      Type-generic
      macro
      Real function
      variants
      Complex function
      variants
       floatdoublelongdoublefloatdoublelongdouble
      fabsfabsffabsfabslcabsfcabscabsl
      expexpfexpexplcexpfcexpcexpl
      loglogflogloglclogfclogclogl
      powpowfpowpowlcpowfcpowcpowl
      sqrtsqrtfsqrtsqrtlcsqrtfcsqrtcsqrtl
      sinsinfsinsinlcsinfcsincsinl
      coscosfcoscoslccosfccosccosl
      tantanftantanlctanfctanctanl
      asinasinfasinasinlcasinfcasincasinl
      acosacosfacosacoslcacosfcacoscacosl
      atanatanfatanatanlcatanfcatancatanl
      sinhsinhfsinhsinhlcsinhfcsinhcsinhl
      coshcoshfcoshcoshlccoshfccoshccoshl
      tanhtanhftanhtanhlctanhfctanhctanhl
      asinhasinhfasinhasinhlcasinhfcasinhcasinhl
      acoshacoshfacoshacoshlcacoshfcacoshcacoshl
      atanhatanhfatanhatanhlcatanhfcatanhcatanhl

      [edit]Real-only functions

      For all functions that do not have complex counterparts, with the exception ofmodf, a type-generic macroXXX exists, which calls either of the variants of a real function:

      • float variantXXXf
      • double variantXXX
      • longdouble variantXXXl

      The function to call is determined as follows:

      • If any of the arguments for the generic parameters islongdouble, then thelongdouble variant is called. Otherwise, if any of the arguments for the generic parameters isdouble, then thedouble variant is called. Otherwise,float variant is called.
      Type-generic
      macro
      Real function
      variants
       floatdoublelongdouble
      atan2atan2fatan2atan2l
      cbrtcbrtfcbrtcbrtl
      ceilceilfceilceill
      copysigncopysignfcopysigncopysignl
      erferfferferfl
      erfcerfcferfcerfcl
      exp2exp2fexp2exp2l
      expm1expm1fexpm1expm1l
      fdimfdimffdimfdiml
      floorfloorffloorfloorl
      fmafmaffmafmal
      fmaxfmaxffmaxfmaxl
      fminfminffminfminl
      fmodfmodffmodfmodl
      frexpfrexpffrexpfrexpl
      hypothypotfhypothypotl
      ilogbilogbfilogbilogbl
      ldexpldexpfldexpldexpl
      lgammalgammaflgammalgammal
      llrintllrintfllrintllrintl
      llroundllroundfllroundllroundl
      log10log10flog10log10l
      log1plog1pflog1plog1pl
      log2log2flog2log2l
      logblogbflogblogbl
      lrintlrintflrintlrintl
      lroundlroundflroundlroundl
      nearbyintnearbyintfnearbyintnearbyintl
      nextafternextafterfnextafternextafterl
      nexttowardnexttowardfnexttowardnexttowardl
      remainderremainderfremainderremainderl
      remquoremquofremquoremquol
      rintrintfrintrintl
      roundroundfroundroundl
      scalblnscalblnfscalblnscalblnl
      scalbnscalbnfscalbnscalbnl
      tgammatgammaftgammatgammal
      trunctruncftrunctruncl

      [edit]Complex-only functions

      For all complex number functions that do not have real counterparts, a type-generic macrocXXX exists, which calls either of the variants of a complex function:

      The function to call is determined as follows:

      • If any of the arguments for the generic parameters is real, complex, or imaginary, then the appropriate complex function is called.
      Type-generic
      macro
      Complex function
      variants
       floatdoublelongdouble
      cargcargfcargcargl
      conjconjfconjconjl
      crealcrealfcrealcreall
      cimagcimagfcimagcimagl
      cprojcprojfcprojcprojl

      [edit]Example

      Run this code
      #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 floatcomplex dc=1+0.5*I;floatcomplex z=sqrt(dc);// argument type is float complex, calls csqrtfprintf("sqrt(1 + 0.5i) = %f+%fi\n",creal(z),// argument type is float complex, calls crealfcimag(z));// argument type is float complex, calls cimagf}

      Output:

      sqrt(2) = 1.414214sin(0.5f) = 0.479426sqrt(1 + 0.5i) = 1.029086+0.242934i

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.25 Type-generic math <tgmath.h> (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.25 Type-generic math <tgmath.h> (p: 272-273)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.25 Type-generic math <tgmath.h> (p: 373-375)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.22 Type-generic math <tgmath.h> (p: 335-337)
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/tgmath&oldid=180676"

      [8]ページ先頭

      ©2009-2025 Movatter.jp