C言語/標準ライブラリ/tgmath.h

提供: testwiki
ナビゲーションに移動 検索に移動

テンプレート:スタブ テンプレート:Nav

ISO/IEC 9899:1999(C99)で標準に追加されたヘッダー<tgmath.h>では、引数の型に応じて対応する数学関数を呼び出す型総称マクロ[1]が定義されます[2]。 <tgmath.h>をインクルードすると、<math.h>と<complex.h>が(暗黙に)インクルードされます。

C言語の数学関数には実数型と複素数型があります。それぞれ引数の型に応じて、float、double、long doubleの3種類があるので 2×3=6通りの組み合わせを生じます。 C言語はC++のようなオーバーロードをサポートしていないため、引数の型が異なる関数のバリエーションには { sin, sinf, sin, sinl, csinf, csin, csinl } のような名前が付けられています。

<tgmath.h>には、呼び出すべき正しい数学関数の選択を簡単にするマクロが含まれています。マクロは渡された型に応じて正しい関数を呼び出します。 例えば、マクロsqrt()では、sqrt(2.0f) は sqrtf(2.0f)を、sqrt(2.0) は sqrt() を[3]、sqrt(2.0L) ならば sqrtl(2.0L) を呼び出します。

コード例
#include <stdio.h>
#include <tgmath.h>

int main(void)
{
  printf("%32.30f\n",  sqrt(2.0f));
  printf("%32.30f\n",  sqrt(2.0));
  printf("%32.30Lf\n", sqrt(2.0L));
  float complex f = 2 + I * 2;
  f = sqrt(f);
  printf("%32.30f+%32.30fi\n", creal(f), cimag(f));
  double complex d = 2 + I * 2;
  d = sqrt(d);
  printf("%32.30f+%32.30fi\n", creal(d), cimag(d));
  long double complex l = 2 + I * 2;
  l = sqrt(l);
  printf("%32.30Lf+%32.30Lfi\n", creal(l), cimag(l));
}
結果
1.414213538169860839843750000000
1.414213562373095145474621858739
1.414213562373095048763788073032
1.553773999214172363281250000000+0.643594264984130859375000000000i
1.553773974030037363647238635167+0.643594252905582586699040348321i
1.553773974030037307377145883169+0.643594252905582624700326493938i
<tgmath.h> で定義されている型総称マクロと展開先の数学関数
型総称マクロ 実数 複素数
float double long double float double long double
三角関数 sin sinfsinsinlcsinfcsincsinl
coscos cosfcoscoslccosfccosccosl
tan tanftantanlctanfctanctanl
asin asinfasinasinlcasinfcasincasinl
acos acosfacosacoslcacosfcacoscacosl
atan atanfatanatanlcatanfcatancatanl
atan2 atan2fatan2atan2l---
双曲線関数 sinh sinhfsinhsinhlcsinhfcsinhcsinhl
cosh coshfcoshcoshlccoshfccoshccoshl
tanh tanhftanhtanhlctanhfctanhctanhl
asinh asinhfasinhasinhlcasinhfcasinhcasinhl
acosh acoshfacoshacoshlcacoshfcacoshcacoshl
atanh atanhfatanhatanhlcatanhfcatanhcatanhl
指数関数及び対数関数 exp expfexpexplcexpfcexpcexpl
exp2 exp2fexp2exp2l---
expm1 expm1fexpm1expm1l---
frexp frexpffrexpfrexpl---
ilogb ilogbfilogbilogbl---
ldexp ldexpfldexpldexpl---
log logflogloglclogfclogclogl
log10 log10flog10log10l---
log1p log1pflog1plog1pl---
log2 log2flog2log2l---
logb logbflogblogbl---
scalbln scalblnfscalblnscalblnl---
scalbn scalbnfscalbnscalbnl---
べき乗関数及び絶対値関数 cbrt cbrtfcbrtcbrtl---
fabs fabsffabsfabslcabsfcabscabsl
hypot hypotfhypothypotl---
pow powfpowpowlcpowfcpowcpowl
sqrt sqrtfsqrtsqrtlcsqrtfcsqrtcsqrtl
誤差関数及びガンマ関数 erf erfferferfl---
erfc erfcferfcerfcl---
lgamma lgammaflgammalgammal---
tgamma tgammaftgammatgammal---
最近接整数関数 ceil ceilfceilceill---
floor floorffloorfloorl---
nearbyint nearbyintfnearbyintnearbyintl---
rint rintfrintrintl---
lrint lrintflrintlrintl---
llrint llrintfllrintllrintl---
round roundfroundroundl---
lround lroundflroundlroundl---
llround llroundfllroundllroundl---
trunc truncftrunctruncl---
剰余関数 fmod fmodffmodfmodl---
remainder remainderfremainderremainderl---
remquo remquofremquoremquol---
実数操作関数 copysign copysignfcopysigncopysignl---
nextafter nextafterfnextafternextafterl---
nexttoward nexttowardfnexttowardnexttowardl---
最大, 最小及び正の差関数 fdim fdimffdimfdiml---
fmax fmaxffmaxfmaxl---
fmin fminffminfminl---
浮動小数点乗算加算 fma fmaffmafmal---
複素数操作 carg ---cargfcargcargl
conj ---conjfconjconjl
creal ---crealfcrealcreall
cimag ---cimagfcimagcimagl
cproj ---cprojfcprojcprojl

型ジェネリック式

ISO/IEC 9899:2011(C11)で、キーワード _Generic が導入されました[4]。

_Generic は、型ジェネリック式を実現するためのもので、C++のオーバーロードの様に引数の型によって処理を選択します。

構文

_Generic( 式, 型: 値, 型: 値, 型: 値,  ... 型: 値)

型ジェネリック式は、与えられた「式」と一致する型に対応した値を取ります。 特別な「型」として default があり、他の全ての型に一致しなかった場合に default に対応した値を取ります。

_Generic の使用例(C11:WG14/N1570 Committee Draft ISO/IEC.p.68, §6.5.1.1 Generic selection.から抜粋[4])

#define cbrt(X) _Generic((X),                        \
                        long double: cbrtl,          \
                        default: cbrt,               \
                        float: cbrtf                 \
                        )(X)

crrt()は、立方根(cube root)を返す関数ですが、引数の種類によって、

     double cbrt(double x);
     float cbrtf(float x);
     long double cbrtl(long double x);

の3つのバリエーションがあります。

この例で示された型総称マクロcbrt()では、_Generic() で渡された仮引数 X によって呼び出す関数を切り替えています。

実際のプログラムで、このマクロを使って試してみましょう。

コード例
#include <stdio.h>
#include <math.h>
#include <complex.h>

#define cbrt(X) _Generic((X),                        \
                        long double: cbrtl,          \
                        default: cbrt,               \
                        float: cbrtf                 \
                        )(X)

int main()
{
    printf("%32.30f\n",  cbrt(2.0f));
    printf("%32.30f\n",  cbrt(2.0));
    printf("%32.30Lf\n", cbrt(2.0L));
}
結果
1.259921073913574218750000000000
1.259921049894873190666544360283
1.259921049894873164754112437880

脚註

  1. ↑ Type-generic macros
  2. ↑ テンプレート:Cite book
  3. ↑ マクロプロセッサは、マクロ展開の結果に再びマクロを適用することはしないのでマクロ名と展開先の関数名が一致している事は問題になりません。
  4. 1 2 テンプレート:Cite book

参考文献