ジェネリック型数値演算

ISO C 標準 11 (C11) 以降では、<tgmath.h> ヘッダーと、<math.h><complex.h> には、パラメーターの型に基づいて対応する数値演算関数を呼び出すマクロが用意されています。

C ランタイム ライブラリの数値演算関数には、実数と複合値のバリアントがあります。 各バリアントは引数の型に応じて floatdoublelong double の 3 種類に分かれます。 C は C++ のようにオーバーロードをサポートしていないため、各バリアントには異なる名前が付けられています。 たとえば、実数の浮動小数点値の絶対値を取得するには、floatdoublelong double のどの値を渡すかに応じて、それぞれ fabsffabsfabsl のいずれかを呼び出します。 複合値の絶対値を取得するには、floatdoublelong double のどの複合値を渡しているかに応じて、それぞれ cabsfcabscabsl のいずれかを呼び出します。 引数が上記のメンション型のいずれにも一致しない場合は、引数が double であるかのように関数が選択されます。

<tgmath.h> には、呼び出す適切な数値演算関数の選択を簡略化するマクロが含まれています。 これらのマクロを使うと、渡した型を調べてから、適切な関数を呼び出すことができます。 たとえば、sqrt マクロを使うと、sqrt(9.9f)sqrtf() にバインドされますが、sqrt(9.9)sqrt() にバインドされます。 ジェネリック パラメーターの少なくとも 1 つのマクロ引数が複合値である場合、マクロによって複合型の関数にバインドされます。それ以外の場合は実数型の関数が呼び出されます。

<tgmath.h> でジェネリック型マクロを使うと、より移植性の高いコードを記述できます。これは、キャストを管理したり、引数の型に応じて異なる関数名を選ぶ必要がないからです。

これらのマクロは、<math.h> ヘッダーを使って書かれたプログラムが破損しないように、独自のヘッダー内にあります。 そのため、<math.h> をインクルードすると、double x = sin(42); は常に同じように動作します。 それでも、既存のほとんどの C プログラムは、<math.h> または <complex.h> ではなく <tgmath.h> ヘッダーがインクルードされいる場合に、影響を受けないことが期待されます。

<tgmath.h> で使用できるマクロと、それらが拡張する内容を次の表に示します。 modf がこの表に含まれていないのは、型の解決を複雑にすることなく安全にする方法が明確でなく、対応するジェネリック型のマクロがないためです。

マクロ 実数型
float
実数型
double
実数型
long double
複合型
float
複合型
double
複合型
long double
acos acosf acos acosl cacosf cacos cacosl
acosh acoshf acosh acoshl cacoshf cacosh cacoshl
asin asinf asin asinl casinf casin casinl
asinh asinhf asinh asinhl casinhf casinh casinhl
atan atanf atan atanl catanf catan catanl
atanh atanhf atanh atanhl catanhf catanh catanhl
cos cosf cos cosl ccosf ccos ccosl
cosh coshf cosh coshl ccoshf ccosh ccoshl
exp expf exp expl cexpf cexp cexpl
fabs fabsf fabs fabsl cabsf cabs cabsl
log logf log logl clogf clog clogl
pow powf pow powl cpowf cpow cpowl
sin sinf sin sinl csinf csin csinl
sinh sinhf sinh sinhl csinhf csinh csinhl
sqrt sqrtf sqrt sqrtl csqrtf csqrt csqrtl
tan tanf tan tanl ctanf ctan ctanl
tanh tanhf tanh tanhl ctanhf ctanh ctanhl
atan2 atan2f atan2 atan2l - - -
cbrt cbrtf cbrt cbrtl - - -
ceil ceilf ceil ceill - - -
copysign copysignf copysign copysignl - - -
erf erff erf erfl - - -
erfc erfcf erfc erfcl - - -
exp2 exp2f exp2 exp2l - - -
expm1 expm1f expm1 expm1l - - -
fdim fdimf fdim fdiml - - -
floor floorf floor floorl - - -
fma fmaf fma fmal - - -
fmax fmaxf fmax fmaxl - - -
fmin fminf fmin fminl - - -
fmod fmodf fmod fmodl - - -
frexp frexpf frexp frexpl - - -
hypot hypotf hypot hypotl - - -
ilogb ilogbf ilogb ilogbl - - -
ldexp ldexpf ldexp ldexpl - - -
lgamma lgammaf lgamma lgammal - - -
llrint llrintf llrint llrintl - - -
llround llroundf llround llroundl - - -
log10 log10f log10 log10l - - -
log1p log1pf log1p log1pl - - -
log2 log2f log2 log2l - - -
logb logbf logb logbl - - -
lrint lrintf lrint lrintl - - -
lround lroundf lround lroundl - - -
nearbyint nearbyintf nearbyint nearbyintl - - -
nextafter nextafterf nextafter nextafterl - - -
nexttoward nexttowardf nexttoward nexttowardl - - -
remainder remainderf remainder remainderl - - -
remquo remquof remquo remquol - - -
rint rintf rint rintl - - -
round roundf round roundl - - -
scalbln scalblnf scalbln scalblnl - - -
scalbn scalbnf scalbn scalbnl - - -
tgamma tgammaf tgamma tgammal - - -
trunc truncf trunc truncl - - -
carg - - - cargf carg cargl
conj - - - conjf conj conjl
creal - - - crealf creal creall
cimag - - - cimagf cimag cimagl
cproj - - - cprojf cproj cprojl

必要条件

/std:c11 を使ってコンパイルします。

Windows SDK バージョン 10.0.20348.0 (バージョン 2104) 以降。 最新の SDK のダウンロードするには、「Windows SDK」を参照してください。 C11 および C17 開発用 SDK をインストールして使用する手順については、「C11 と C17 のサポートを Visual Studio にインストールする」を参照してください。

関連項目

C ランタイム ライブラリ リファレンス