المتغيرات
فضاءات التسمية
أفعال

Type-generic math

من cppreference.com
< c‏ | numeric

الملف ‎<tgmath.h>‎ يتضمن الملفات ‎<math.h>‎ و ‎<complex.h>‎ ويعرف العديد من الماكرو عامة النوع التي تستدعي الدالة المناسبة للمعطى على حسب نوعا سواء كان حقيقيا أم مركبا.

كل ماكرو يأخذ معطى أو أكثر عام النوع generic parameter. المعطى عام النوع هو الذي نوعه double بالنسبة للدول الحقيقية المجردة (أي بدون حرف يدل على نوع المعطى) المعرفة في math.h. فمثلا كلا معطيا pow‎ عام بينما فقط المعطى الأول من scalbn‎ معطى عام.


When a ‎<tgmath.h>‎ 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 not compatible 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 macro: float complex fc; ceil(fc) or double complex 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.

محتويات

[تعديل]ماكرو عامة للمعطيات المركبة والحقيقية

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

  • real function:
  • float يؤدي إلى استدعاء ‎XXXf‎
  • double يؤدي إلى استدعاء ‎XXX‎
  • longdouble يؤدي إلى استدعاء ‎XXXl‎
  • complex function:
  • float يؤدي إلى استدعاء ‎cXXXf‎
  • double يؤدي إلى استدعاء ‎cXXX‎
  • longdouble يؤدي إلى استدعاء ‎cXXXl‎

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

تحديد الدالة المطلوبة يتم كالآتي:

  • 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, tag, cosh, sinh, tanh, asin, atan, asinh, and atanh call real functions, the return types of sin, tan, sinh, tanh, asin, atan, asinh, and atanh are imaginary, and the return types of cos and cosh 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 is longdouble, then the longdouble variant is called. Otherwise, if any of the parameters is double or integer, then the double variant is called. Otherwise, float variant is called.

The type-generic macros are as follows:

ماكرو عام النوع الدوال الحقيقية
variants
الدوال المركبة
variants
 
float
double
longdouble
float
double
longdouble
fabs fabsffabsfabslcabsfcabscabsl
exp expfexpexplcexpfcexpcexpl
log logflogloglclogfclogclogl
pow powfpowpowlcpowfcpowcpowl
sqrt sqrtfsqrtsqrtlcsqrtfcsqrtcsqrtl
sin sinfsinsinlcsinfcsincsinl
cos cosfcoscoslccosfccosccosl
tan tanftantanlctanfctanctanl
asin asinfasinasinlcasinfcasincasinl
acos acosfacosacoslcacosfcacoscacosl
atan atanfatanatanlcatanfcatancatanl
sinh sinhfsinhsinhlcsinhfcsinhcsinhl
cosh coshfcoshcoshlccoshfccoshccoshl
tanh tanhftanhtanhlctanhfctanhctanhl
asinh asinhfasinhasinhlcasinhfcasinhcasinhl
acosh acoshfacoshacoshlcacoshfcacoshcacoshl
atanh atanhfatanhatanhlcatanhfcatanhcatanhl

[تعديل]دوال حقيقية فقط

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

  • float يؤدي إلى استدعاء ‎XXXf‎
  • double يؤدي إلى استدعاء ‎XXX‎
  • longdouble يؤدي إلى استدعاء ‎XXXl‎

تحديد الدالة المطلوبة يتم كالآتي:

  • If any of the arguments for the generic parameters is longdouble, then the longdouble variant is called. Otherwise, if any of the arguments for the generic parameters is double, then the double variant is called. Otherwise, float variant is called.
ماكرو عام النوع الدوال الحقيقية
variants
 
float
double
longdouble
atan2 atan2fatan2atan2l
cbrt cbrtfcbrtcbrtl
ceil ceilfceilceill
copysign copysignfcopysigncopysignl
erf erfferferfl
erfc erfcferfcerfcl
exp2 exp2fexp2exp2l
expm1 expm1fexpm1expm1l
fdim fdimffdimfdiml
floor floorffloorfloorl
fma fmaffmafmal
fmax fmaxffmaxfmaxl
fmin fminffminfminl
fmod fmodffmodfmodl
frexp frexpffrexpfrexpl
hypot hypotfhypothypotl
ilogb ilogbfilogbilogbl
ldexp ldexpfldexpldexpl
lgamma lgammaflgammalgammal
llrint llrintfllrintllrintl
llround llroundfllroundllroundl
log10 log10flog10log10l
log1p log1pflog1plog1pl
log2 log2flog2log2l
logb logbflogblogbl
lrint lrintflrintlrintl
lround lroundflroundlroundl
nearbyint nearbyintfnearbyintnearbyintl
nextafter nextafterfnextafternextafterl
nexttoward nexttowardfnexttowardnexttowardl
remainder remainderfremainderremainderl
remquo remquofremquoremquol
rint rintfrintrintl
round roundfroundroundl
scalbln scalblnfscalblnscalblnl
scalbn scalbnfscalbnscalbnl
tgamma tgammaftgammatgammal
trunc truncftrunctruncl

[تعديل]دوال مركبة فقط

لكل الدوال المركبة التي ليس لها نظير حقيقي, يوجد ماكرو عام النوع على الصورة ‎cXXX‎. يقوم هذا الماكرو بإستدعاء النظير المناسب على حسب المعطى:

  • float complex يؤدي إلى استدعاء ‎cXXXf‎
  • double complex يؤدي إلى استدعاء ‎cXXX‎
  • longdouble complex يؤدي إلى استدعاء ‎cXXXl‎

تحديد الدالة المطلوبة يتم كالآتي:

ماكرو عام النوع الدوال المركبة
variants
 
float
double
longdouble
carg cargfcargcargl
conj conjfconjconjl
creal crealfcrealcreall
cimag cimagfcimagcimagl
cproj cprojfcprojcprojl

[تعديل] مثال

#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   float complex dc =1+0.5*I;float complex z = sqrt(dc);// argument type is float complex, calls csqrtf printf("sqrt(1 + 0.5i) = %f+%fi\n", creal(z), // argument type is float complex, calls crealf cimag(z));// argument type is float complex, calls cimagf}

الخرج:

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