[C言語 標準ライブラリ] math.hの使い方

※本ブログでは、商品・サービスのリンク先にプロモーションを含みます。

スポンサーリンク

math.hには、数学的な演算を行う関数、およびマクロが宣言、定義されています。

  1. マクロ定数
    1. HUGE_VAL
    2. HUGE_VALF
    3. HUGE_VALL
    4. INFINITY
    5. NAN
    6. FP_INFINITE
    7. FP_NAN
    8. FP_NORMAL
    9. FP_SUBNORMAL
    10. FP_ZERO
    11. FP_FAST_FMA
    12. FP_FAST_FMAF
    13. FP_FAST_FMAL
    14. FP_ILOGB0
    15. FP_ILOGBNAN
    16. MATH_ERRNO
    17. MATH_ERREXCEPT
    18. math_errhandling
    1. float_t
    2. double_t
  2. 三角関数
    1. double sin(double x)
    2. double cos(double x)
    3. double tan(double x)
    4. double asin(double x)
    5. double acos(double x)
    6. double atan(double x)
    7. double atan2(double y, double x)
  3. 双曲線関数
    1. double sinh(double x)
    2. double cosh(double x)
    3. double tanh(double x)
    4. double asinh(double x)
    5. double acosh(double x)
    6. double atanh(double x)
  4. 指数と対数関数
    1. double exp(double x)
    2. double frexp(double value, int *exp)
    3. double ldexp(double x, int exp)
    4. double log(double x)
    5. double log10(double x)
    6. double modf(double value, double *iptr)
    7. double exp2(double x)
    8. double expm1(double x)
    9. int ilogb(double x)
    10. double log1p(double x)
    11. double log2(double x)
    12. double scalbn(double x, int n)
    13. double scalbln(double x, long int n)
    14. double logb(double x)
  5. pow関数
    1. double pow(double x, double y)
    2. double sqrt(double x)
    3. double cbrt(double x)
    4. double hypot(double x, double y)
  6. 誤差と絶対値関数
    1. double fabs(double x)
    2. double fma(double x, double y, double z)
  7. 浮動所数点操作関数
    1. int fpclassify(real-floating x)
    2. int isfinite(real-floating x)
    3. int isinf(real-floating x)
    4. int isnan(real-floating x)
    5. int isnormal(real-floating x)
    6. int signbit(real-floating x)
  8. 浮動小数点比較関数
    1. int isgreater(real-floating x, real-floating y)
    2. int isgreaterequal(real-floating x, real-floating y)
    3. int isless(real-floating x, real-floating y)
    4. int islessequal(real-floating x, real-floating y)
    5. int islessgreater(real-floating x, real-floating y)
    6. int isunordered(real-floating x, real-floating y)
  9. その他の数学関数
    1. double ceil(double x)
    2. double floor(double x)
    3. double fmod(double x, double y)
    4. double round(double x)
    5. long int lrint(double x)
    6. long int lround(double x)
    7. double nearbyint(double x)
    8. double rint(double x)
    9. long long int llrint(double x)
    10. long long int llround(double x)
    11. double fdim(double x, double y)
    12. double fmax(double x, double y)
    13. double fmin(double x, double y)
    14. double remainder(double x, double y)
    15. double remquo(double x, double y, int *quo)
    16. double nan(const char *tagp)
    17. double nextafter(double x, double y)
    18. double nexttoward(double x, long double y)
    19. double erf(double x)
    20. double erfc(double x)
    21. double lgamma(double x)
    22. double tgamma(double x)
    23. double trunc(double x)
    24. double copysign(double x, double y)
スポンサーリンク

マクロ定数

HUGE_VAL

倍精度浮動小数点数(double型)の正の無限大を表す定数です。
数学関数や計算の結果が正の無限大を返す場合に使用されます。
この値は、数学的な演算の結果が有効な範囲を超えた場合やオーバーフローが発生した場合に返されることがあります。

HUGE_VALF

単精度浮動小数点数(float型)の正の無限大を表す定数です。
HUGE_VALと同様に数学関数や計算の結果が正の無限大を返す場合に使用されます。
この値は、数学的な演算の結果が有効な範囲を超えた場合やオーバーフローが発生した場合に返されることがあります。

HUGE_VALL

拡張精度浮動小数点数(long double型)の正の無限大を表す定数です。
HUGE_VALと同様に数学関数や計算の結果が正の無限大を返す場合に使用されます。
この値は、数学的な演算の結果が有効な範囲を超えた場合やオーバーフローが発生した場合に返されることがあります。

INFINITY

正の、または符号無しの無限大を表すfloat型の定数式です。
それが利用不可の場合は、コンパイル時においてオーバフローするfloat型の正の定数となります。

NAN

QNaNを表すfloat型の定数式です。
処理系がfloat型のQNaNをサポートしている場合にのみ定義されます。

FP_INFINITE

値を分類するための整数定数式です。
fpclassify()マクロ関数の戻り値に使用されます。

FP_NAN

値を分類するための整数定数式です。
fpclassify()マクロ関数の戻り値に使用されます。

FP_NORMAL

値を分類するための整数定数式です。
fpclassify()マクロ関数の戻り値に使用されます。

FP_SUBNORMAL

値を分類するための整数定数式です。
fpclassify()マクロ関数の戻り値に使用されます。

FP_ZERO

値を分類するための整数定数式です。
fpclassify()マクロ関数の戻り値に使用されます。

FP_FAST_FMA

fma(a,b,c) 関数が、a * b + c と計算する速さより、同じか、より速く計算できる場合に定義されます。

FP_FAST_FMAF

fmaf(a,b,c) 関数が、a * b + c と計算する速さより、同じか、より速く計算できる場合に定義されます。

FP_FAST_FMAL

fmal(a,b,c) 関数が、a * b + c と計算する速さより、同じか、より速く計算できる場合に定義されます。

FP_ILOGB0

ilogb(0)の戻り値である整数定数式です。
INT_MINまたは-INT_MAX。

FP_ILOGBNAN

ilogb(NaN)の戻り値である整数定数式です。
INT_MAXまたはINT_MIN。

MATH_ERRNO

定数(1)が定義されます。

MATH_ERREXCEPT

定数(2)が定義されます。

math_errhandling

数学関数がエラーをどのように処理するかを指定するために使用されます。
math_errhandlingマクロは、以下のいくつかの定数を定義しています。

math_errhandlingの値マクロ名意味
0エラー処理がサポートされていないことを示します。
1MATH_ERRNO数学関数がエラーを検出すると、関数はエラーコードを設定します。このフラグが設定されると、関数はグローバル変数errnoを使用してエラーコードを設定します。
2MATH_ERREXCEPT数学関数がエラーを検出すると、関数は浮動小数点例外を発生させます。このフラグが設定されると、関数は浮動小数点例外を発生させるために浮動小数点制御ワードを変更します。

MATH_ERRNO、MATH_ERREXCEPT、あるいは両方のビット単位の論理和のいずれか。

float.hで定義されている FLT_EVAL_METHOD の値によって、float_t型とdouble_t型の定義が決まります。

FLT_EVA_METHODの値float_t型の定義double_t型の定義
0float型double型
1double型double型
2long double型long double型
上記以外実装依存実装依存

これらの型は、プログラムが特定の浮動小数点数の幅に依存することなく、浮動小数点数の計算や変換を行うために使用されます。例えば、プログラムが異なるプラットフォームやコンパイラ上で実行される場合でも、浮動小数点数の幅が異なっていても、float_tdouble_tを使用することで互換性を確保することができます。

float_t

単精度浮動小数点数(float型)の正確な幅を表すために使用されます。
一般的に、float_tは32ビット幅であることが期待されますが、実装によって異なる場合があります。

double_t

倍精度浮動小数点数(double型)の正確な幅を表すために使用されます。
一般的に、double_tは64ビット幅であることが期待されますが、実装によって異なる場合があります。

三角関数

double sin(double x)

// 宣言
float sinf(float x);
double sin(double x);
long double sinl(long double x);

引数xのラジアン値の正弦(サイン)を計算します。
-1~1の正弦値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;  // ラジアンで指定された角度
    double result = sin(x);

    printf("sin(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

sin(1.00) = 0.84

double cos(double x)

// 宣言
float cosf(float x);
double cos(double x);
long double cosl(long double x);

引数xのラジアン値の余弦(コサイン)を計算します。
-1~1の余弦値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;  // ラジアンで指定された角度
    double result = cos(x);

    printf("cos(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

cos(1.00) = 0.54

double tan(double x)

// 宣言
float tanf(float x);
double tan(double x);
long double tanl(long double x);

引数xのラジアン値の正接(タンジェント)を計算します。
正接値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;  // ラジアンで指定された角度
    double result = tan(x);

    printf("tan(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

tan(1.00) = 1.56

double asin(double x)

// 宣言
float asinf(float x);
double asin(double x);
long double asinl(long double x);

引数xの逆正弦(アークサイン)を計算します。
引数xのアークサインを計算し、結果を浮動小数点数で返します。
引数xが-1~+1の範囲内にない場合は定義域エラーが発生します。
返される結果は、-π/2~+π/2の弧度法(ラジアン)で表されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double data1  = 0.5;
    double result = asin(data1);

    printf("asin(%.2f) = %.2f\n", data1, result);

    return 0;
}

実行結果:

asin(0.50) = 0.52

double acos(double x)

// 宣言
float acosf(float x);
double acos(double x);
long double acosl(long double x);

引数xの逆余弦(アークコサイン)を計算します。
引数xのアークコサインを計算し、結果を浮動小数点数で返します。
引数xが-1~+1の範囲内にない場合は定義域エラーが発生します。
返される結果は、0~+πの弧度法(ラジアン)で表されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double data1  = 0.5;
    double result = acos(data1);

    printf("acos(%.2f) = %.2f\n", data1, result);

    return 0;
}

実行結果:

acos(0.50) = 1.05

double atan(double x)

// 宣言
float atanf(float x);
double atan(double x);
long double atanl(long double x);

引数xの逆正接(アークタンジェント)を計算を計算します。
引数xの範囲は無制限です。任意の実数値を受け取ることができます。
-π/2~+π/2の範囲のラジアンの逆正接値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double data1  = 1.0;
    double result = atan(data1);

    printf("atan(%.2f) = %.2f\n", data1, result);

    return 0;
}

実行結果:

atan(1.00) = 0.79

double atan2(double y, double x)

// 宣言
float atan2f(float y, float x);
double atan2(double y, double x);
long double atan2l(long double y, long double x);

引数の逆正接(アークタンジェント)を計算します。
引数の範囲は無制限です。任意の実数値を受け取ることができます。
xとyが0の場合、定義域エラーが発生する可能性があります。
返される結果は、-π~+πの弧度法(ラジアン)で表されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double y = 1.0;
    double x = 1.0;
    double result = atan2(y, x);

    printf("atan2(%.2f, %.2f) = %.2f\n", y, x, result);

    return 0;
}

実行結果:

atan2(1.00, 1.00) = 0.79

スポンサーリンク

双曲線関数

double sinh(double x)

// 宣言
float sinhf(float x);
double sinh(double x);
long double sinhl(long double x);

引数xの双曲線正弦(ハイパボリックサイン)を計算します。
xの絶対値が大きすぎる場合は値域エラーが発生します。
双曲線正弦値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;
    double result = sinh(x);

    printf("sinh(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

sinh(1.00) = 1.18

double cosh(double x)

// 宣言
float coshf(float x);
double cosh(double x);
long double coshl(long double x);

引数xの双曲線余弦(ハイパボリックコサイン)を計算します。
xの絶対値が大きすぎる場合は値域エラー発生が発生します。
非負の双曲線余弦値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;
    double result = cosh(x);

    printf("cosh(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

cosh(1.00) = 1.54

double tanh(double x)

// 宣言
float tanhf(float x);
double tanh(double x);
long double tanhl(long double x);

引数xの双曲線正接(ハイパボリックタンジェント)を計算します。
双曲線正接値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;
    double result = tanh(x);

    printf("tanh(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

tanh(1.00) = 0.76

double asinh(double x)

// 宣言
float asinhf(float x);
double asinh(double x);
long double asinhl(long double x);

引数xの逆双曲線正弦(アークハイパボリックサイン)を計算します。
双曲線逆正弦値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 2.0;
    double result = asinh(x);

    printf("asinh(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

asinh(2.00) = 1.44

double acosh(double x)

// 宣言
float acoshf(float x);
double acosh(double x);
long double acoshl(long double x);

引数xの非負の逆双曲線余弦(アークハイパボリックコサイン)を計算します。
xが1未満の場合、定義域エラーが発生します。
0~+∞の双曲線逆余弦値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 2.0;
    double result = acosh(x);

    printf("acosh(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

acosh(2.00) = 1.32

double atanh(double x)

// 宣言
float atanhf(float x);
double atanh(double x);
long double atanhl(long double x);

引数xの逆双曲線逆正接(アークハイパボリックタンジェント)を計算します。
xが-1~+1の範囲内にない場合は、定義域エラーが発生します。
xが-1、または+1の場合は、値域エラーが発生する可能性があります。
双曲線逆正接値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 0.5;
    double result = atanh(x);

    printf("atanh(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

atanh(0.50) = 0.55

指数と対数関数

double exp(double x)

// 宣言
float expf(float x);
double exp(double x);
long double expl(long double x);

引数xの指数関数を計算します。(自然対数の底eのx乗を計算します)
引数xの絶対値が大きすぎる場合は、値域エラーが発生します。
eのx乗の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;
    double result = exp(x);

    printf("exp(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

exp(1.00) = 2.72

double frexp(double value, int *exp)

// 宣言
float frexpf(float value, int *exp);
double frexp(double value, int *exp);
long double frexpl(long double value, int *exp);

浮動小数点数を正規化した数と2の整数べき乗に分割し、その整数をexpが指すオブジェクトに格納します。
次の二つの条件を満足する値xが返されます。
 条件①:xの絶対値が1/2 ~ 1の範囲に含まれる、または0に等しい。
 条件②:valueが2の*exp乗のx倍に等しい。valueが0の場合、xおよび*expの値は0。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 123.45;
    int exponent;
    double mantissa = frexp(x, &exponent);

    printf("Number   : %.2f\n", x);
    printf("Mantissa : %.2f\n", mantissa);
    printf("Exponent : %d\n", exponent);

    return 0;
}

実行結果:

Number : 123.45
Mantissa : 0.96
Exponent : 7

double ldexp(double x, int exp)

// 宣言
float ldexpf(float x, int exp);
double ldexp(double x, int exp);
long double ldexpl(long double x, int exp);

浮動小数点数と2の整数べき乗を乗算します。
値域エラーが発生する可能性があります。
2のexp乗のx倍が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 0.5;
    int exponent = 3;
    double result = ldexp(x, exponent);

    printf("Number   : %.2f\n", x);
    printf("Exponent : %d\n", exponent);
    printf("Result   : %.2f\n", result);

    return 0;
}

実行結果:

Number : 0.50
Exponent : 3
Result : 4.00

double log(double x)

// 宣言
float logf(float x);
double log(double x);
long double logl(long double x);

引数xのeを底とする自然対数を計算します。
xが負の値の場合、定義域エラーが発生します。
xが0の場合、値域エラーが発生する可能性があります。
xの自然対数の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 2.0;
    double result = log(x);

    printf("log(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

log(2.00) = 0.69

double log10(double x)

// 宣言
float log10f(float x);
double log10(double x);
long double log10l(long double x);

引数xの10を底とする常用対数を計算します。
xが負の値の場合、定義域エラーが発生します。
xが0の場合、値域エラーが発生する可能性があります。
xの常用対数の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 100.0;
    double result = log10(x);

    printf("log10(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

log10(100.00) = 2.00

double modf(double value, double *iptr)

// 宣言
float modff(float value, float *iptr);
double modf(double value, double *iptr);
long double modfl(long double value, long double *iptr);

引数valueを整数部と小数部に分割します。
整数部および小数部の符号は、valueと同じになります。
引数iptrの参照先に整数部を格納します。
valueの符号付き小数部が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.14;
    double intpart;
    double fracpart = modf(x, &intpart);

    printf("Number          : %.2f\n", x);
    printf("Integer part    : %.2f\n", intpart);
    printf("Fractional part : %.2f\n", fracpart);

    return 0;
}

実行結果:

Number : 3.14
Integer part : 3.00
Fractional part : 0.14

double exp2(double x)

// 宣言
float exp2f(float x);
double exp2(double x);
long double exp2l(long double x);

引数xの2を底とする指数関数を計算します。(2のx乗を計算します)
引数xの絶対値が大きすぎる場合は、値域エラーが発生します。
2のx乗の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 2.0;
    double result = exp2(x);

    printf("exp2(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

exp2(2.00) = 4.00

double expm1(double x)

// 宣言
float expm1f(float x);
double expm1(double x);
long double expm1l(long double x);

自然対数の底eのx乗から1を引いた値(exp(x) – 1よりも正確な値)を計算します。
引数xが大きすぎる場合は、値域エラーが発生します。
eのx乗から1を引いた値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;
    double result = expm1(x);

    printf("expm1(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

expm1(1.00) = 1.72

int ilogb(double x)

// 宣言
int ilogbf(float x);
int ilogb(double x);
int ilogbl(long double x);

int型の値として引数xの指数を抽出します。
xが0の場合は計算結果はFP_ILOGB0となります。
xが無限大の場合は計算結果はINT_MAXとなります。
xがNaNの場合は計算結果はFP_ILOGBNANとなります。
それ以外の場合は、対応するlogb関数を呼び出し、その戻り値をint型にキャストします。
xが0の場合、値域エラーが発生する可能性があります。
xの指数が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 123.45;
    int exponent = ilogb(x);

    printf("Number   : %.2f\n", x);
    printf("Exponent : %d\n", exponent);

    return 0;
}

実行結果:

Number : 123.45
Exponent : 6

double log1p(double x)

// 宣言
float log1pf(float x);
double log1p(double x);
long double log1pl(long double x);

引数xに1を加えた値のeを底とする自然対数を計算します。
xが-1より小さい場合は、定義域エラーが発生します。
xが-1の場合は、値域エラーが発生する可能性があります。
(x+1)の自然対数の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 0.5;
    double result = log1p(x);

    printf("log1p(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

log1p(0.50) = 0.41

double log2(double x)

// 宣言
float log2f(float x);
double log2(double x);
long double log2l(long double x);

引数xの2を底とする対数を計算します。
xが負の値の場合は、定義域エラーが発生します。
xが0の場合は、値域エラーが発生する可能性があります。
xの2を底とする対数の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 8.0;
    double result = log2(x);

    printf("log2(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

log2(8.00) = 3.00

double scalbn(double x, int n)

// 宣言
float scalbnf(float x, int n);
double scalbn(double x, int n);
long double scalbnl(long double x, int n);

効率よくFLT_RADIXのn乗のx倍を計算します。
値域エラーが発生する可能性があります。
FLT_RADIXのn乗のx倍が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 0.5;
    int exponent = 3;
    double result = scalbn(x, exponent);

    printf("Number   : %.2f\n", x);
    printf("Exponent : %d\n", exponent);
    printf("Result   : %.2f\n", result);

    return 0;
}

実行結果:

Number : 0.50
Exponent : 3
Result : 4.00

double scalbln(double x, long int n)

// 宣言
float scalblnf(float x, long int n);
double scalbln(double x, long int n);
long double scalblnl(long double x, long int n);

効率よくFLT_RADIXのn乗のx倍を計算します。
値域エラーが発生する可能性があります。
FLT_RADIXのn乗のx倍が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 0.5;
    long int exponent = 3;
    double result = scalbln(x, exponent);

    printf("Number   : %.2f\n", x);
    printf("Exponent : %ld\n", exponent);
    printf("Result   : %.2f\n", result);

    return 0;
}

実行結果:

Number : 0.50
Exponent : 3
Result : 4.00

double logb(double x)

// 宣言
float logbf(float x);
double logb(double x);
long double logbl(long double x);

浮動小数点フォーマットにおける符号付き整数の値として引数xの指数を抽出します。
xが非正規化数の場合は、正規化されているものとして処理されます。
xが有限で正の値の場合、1 ≦ x × FLT_RADIX-logb(x) となります。
xの符号付き指数が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1000.0;
    double base = 10.0;
    double result = logb(x) / logb(base);

    printf("log(%g, %.2f) = %.2f\n", base, x, result);

    return 0;
}

実行結果:

log(10, 1000.00) = 3.00

pow関数

double pow(double x, double y)

// 宣言
float powf(float x, float y);
double pow(double x, double y);
long double powl(long double x, long double y);

xのy乗を計算します。
値域エラーが発生する可能性があります。
xが有限な負の値、かつyが有限だが整数値ではない場合は、定義域エラーが発生します。
xが0で、かつyが0以下の場合は、定義域エラーが発生する可能性があります。
xのy乗の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double base = 2.0;
    double exponent = 3.0;
    double result = pow(base, exponent);

    printf("%.2f raised to the power of %.2f = %.2f\n", base, exponent, result);

    return 0;
}

実行結果:

2.00 raised to the power of 3.00 = 8.00

double sqrt(double x)

// 宣言
float sqrtf(float x);
double sqrt(double x);
long double sqrtl(long double x);

引数xの平方根を計算します。
xが0より小さい場合は、定義域エラーが発生します。
xの平方根が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 16.0;
    double result = sqrt(x);

    printf("sqrt(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

sqrt(16.00) = 4.00

double cbrt(double x)

// 宣言
float cbrtf(float x);
double cbrt(double x);
long double cbrtl(long double x);

引数xの実数の立方根を計算します。
xの1/3乗の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 27.0;
    double result = cbrt(x);

    printf("cbrt(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

cbrt(27.00) = 3.00

double hypot(double x, double y)

// 宣言
float hypotf(float x, float y);
double hypot(double x, double y);
long double hypotl(long double x, long double y);

オーバフローやアンダフローを発生させること無く、引数xの2乗と引数yの2乗の和の平方根を計算します。
値域エラーが発生する可能性があります。
xの2乗とyの2乗の和の平方根が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.0;
    double y = 4.0;
    double result = hypot(x, y);

    printf("hypot(%.2f, %.2f) = %.2f\n", x, y, result);

    return 0;
}

実行結果:

hypot(3.00, 4.00) = 5.00

スポンサーリンク

誤差と絶対値関数

double fabs(double x)

// 宣言
float fabsf(float x);
double fabs(double x);
long double fabsl(long double x);

引数xの絶対値を計算します。
xの絶対値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = -3.14;
    double result = fabs(x);

    printf("fabs(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

fabs(-3.14) = 3.14

double fma(double x, double y, double z)

// 宣言
float fmaf(float x, float y, float z);
double fma(double x, double y, double z);
long double fmal(long double x, long double y, long double z);

(x*y)+zをまとめて計算します。
計算は、無限の精度があるものとして実行されます。
丸めは1回だけ行われます。
丸めモードはFLT_ROUNDSの値が示すモードです。
(x*y)+zが返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 2.5;
    double y = 3.0;
    double z = 1.2;
    double result = fma(x, y, z);

    printf("fma(%.2f, %.2f, %.2f) = %.2f\n", x, y, z, result);

    return 0;
}

実行結果:

fma(2.50, 3.00, 1.20) = 8.70

浮動所数点操作関数

int fpclassify(real-floating x)

// 宣言
int fpclassify(float x);
int fpclassify(double x);
int fpclassify(long double x);

引数xの浮動小数点がどのカテゴリ(NaN、無限大、正規化数、非正規化数、0、その他の実装依存)に分類されるかをチェックします。
この関数は、引数として1つの浮動小数点値を受け取り、その値が何の種類であるかを整数値(分類用マクロ)で返します。

関数が返す可能性のある値は、以下のマクロで定義されています。

戻り値説明
FP_INFINITE引数が正の無限大または負の無限大であることを示します。
FP_NAN引数が非数(Not a Number)であることを示します。
FP_NORMAL引数が正規化された浮動小数点数であることを示します。
FP_SUBNORMAL引数が非正規化浮動小数点数(非常に小さい値)であることを示します。
FP_ZERO引数がゼロであることを示します。

使用例:

#include <stdio.h>
#include <math.h>

double g_d0 = 0.0;

int main(void)
{
    double data1 = 0.0;
    double data2 = 1.0 / g_d0;
    double data3 = 0.0 / g_d0;
    double data4 = 3.141592653589793;

    printf("data1: %d\n", fpclassify(data1));  // FP_ZERO
    printf("data2: %d\n", fpclassify(data2));  // FP_INFINITE
    printf("data3: %d\n", fpclassify(data3));  // FP_NAN
    printf("data4: %d\n", fpclassify(data4));  // FP_NORMAL

    return 0;
}

実行結果:

data1: 0
data2: 1
data3: 2
data4: -1

このプログラムでは、4つの浮動小数点数がそれぞれどのカテゴリに分類されるかを調べています。fpclassify関数は、それぞれの値に対応するマクロ値を返します。

ただし、直接これらのマクロ値を printf で出力すると、整数が表示されます。もし具体的なカテゴリ名を表示したい場合は、switch文やif文を用いて整数値を、対応するカテゴリ名に変換する必要があります。

int isfinite(real-floating x)

// 宣言
int isfinite(float x);
int isfinite(double x);
int isfinite(long double x);

引数xの浮動小数点が有限であるかどうかを確認します。
つまり、その値が非数 (NaN) または無限大 (正の無限大または負の無限大) でない場合、この関数は true (非ゼロ) を返し、それ以外の場合は false (ゼロ) を返します。

使用例:

#include <stdio.h>
#include <math.h>

double g_d0 = 0.0;

int main(void)
{
    double data1 = 0.0;
    double data2 = 1.0 / g_d0;
    double data3 = 0.0 / g_d0;
    double data4 = 3.141592653589793;

    printf("data1: %d\n", isfinite(data1));  // 1
    printf("data2: %d\n", isfinite(data2));  // 0
    printf("data3: %d\n", isfinite(data3));  // 0
    printf("data4: %d\n", isfinite(data4));  // 1

    return 0;
}

実行結果:

data1: 1
data2: 0
data3: 0
data4: 1

このプログラムでは、4つの浮動小数点数がそれぞれ有限であるかどうかを調べています。isfinite関数は、有限の値に対して 1 を返し、無限大または非数に対して 0 を返します。

int isinf(real-floating x)

// 宣言
int isinf(float x);
int isinf(double x);
int isinf(long double x);

引数xの浮動小数点数が正の無限大(+∞)または、負の無限大(-∞)であるかどうかを判定します。引数xが無限大の値を表す場合に非0の値を返し、それ以外の場合には0を返します。

使用例:

#include <stdio.h>
#include <math.h>

double g_d0 = 0.0;

int main(void)
{
    double data1 = 1.0 / g_d0;  // 正の無限大
    double data2 = -1.0 / g_d0; // 負の無限大
    double data3 = 0.0;        // 有限の値

    if(isinf(data1)){
        printf("data1は無限大です。\n");
    }else{
        printf("data1は無限大ではありません。\n");
    }

    if(isinf(data2)){
        printf("data2は無限大です。\n");
    }else{
        printf("data2は無限大ではありません。\n");
    }

    if(isinf(data3)){
        printf("data3は無限大です。\n");
    }else{
        printf("data3は無限大ではありません。\n");
    }

    return 0;
}

実行結果:

data1は無限大です。
data2は無限大です。
data3は無限大ではありません。

int isnan(real-floating x)

// 宣言
int isnan(float x);
int isnan(double x);
int isnan(long double x);

引数xが非数(NaN:Not a Number)であるかどうかを判定します。
引数xが非数である場合に非0の値を返し、そうでない場合には0を返します。

使用例:

#include <stdio.h>
#include <math.h>

double g_d0 = 0.0;

int main(void)
{
    double data1 = 0.0 / g_d0;   // NaN
    double data2 = 1.0;         // 有効な値

    if(isnan(data1)){
        printf("data1は非数です。\n");
    }else{
        printf("data1は非数ではありません。\n");
    }

    if(isnan(data2)){
        printf("data2は非数です。\n");
    }else{
        printf("data2は非数ではありません。\n");
    }

    return 0;
}

実行結果:

data1は非数です。
data2は非数ではありません。

int isnormal(real-floating x)

// 宣言
int isnormal(float x);
int isnormal(double x);
int isnormal(long double x);

引数xが正規化数(normalized number)であるかどうかを判定します。
引数xが正規化数である場合に非0の値を返し、そうでない場合には0を返します。

正規化数とは、浮動小数点数表現において、指数部が最小の非負の値を持ち、かつ仮数部が0でない数値を表す数のことです。つまり、非0の有効桁数を持つ正しい浮動小数点数を指します。

使用例:

#include <stdio.h>
#include <math.h>

double g_d0 = 0.0;

int main(void)
{
    double data1 = 123.45;     // 正規化数
    double data2 = 0.0;        // 非正規化数
    double data3 = 1.0 / g_d0; // 無限大
    double data4 = 0.0 / g_d0; // 非数

    if(isnormal(data1)){
        printf("data1は正規化数です。\n");
    }else{
        printf("data1は正規化数ではありません。\n");
    }

    if(isnormal(data2)){
        printf("data2は正規化数です。\n");
    }else{
        printf("data2は正規化数ではありません。\n");
    }

    if(isnormal(data3)){
        printf("data3は正規化数です。\n");
    }else{
        printf("data3は正規化数ではありません。\n");
    }

    if(isnormal(data4)){
        printf("data4は正規化数です。\n");
    }else{
        printf("data4は正規化数ではありません。\n");
    }

    return 0;
}

実行結果:

data1は正規化数です。
data2は正規化数ではありません。
data3は正規化数ではありません。
data4は正規化数ではありません。

int signbit(real-floating x)

// 宣言
int signbit(float x);
int signbit(double x);
int signbit(long double x);

引数xの符号が負であるかどうかを判定します。
引数xの符号が負である場合に非0の値を返し、そうでない場合には0を返します。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double data1 = -1.23;    // 負の数
    double data2 = 2.34;     // 正の数

    if(signbit(data1)){
        printf("data1は負の数です。\n");
    }else{
        printf("data1は負の数ではありません。\n");
    }

    if(signbit(data2)){
        printf("data2は負の数です。\n");
    }else{
        printf("data2は負の数ではありません。\n");
    }

    return 0;
}

実行結果:

data1は負の数です。
data2は負の数ではありません。

浮動小数点比較関数

int isgreater(real-floating x, real-floating y)

// 宣言
int isgreater(float x, float y);
int isgreater(double x, double y);
int isgreater(long double x, long double y);

xがyより大きいかどうかを判定します。
isgreater(x, y)の値は、(x) > (y)に等しいです。
isgreater(x, y)が、(x) > (y)と異なる点は、xとyが順序付けられていない場合に、無効演算の浮動小数点例外が発生しない点です。
(x) > (y)の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    float x = 3.5;
    float y = 2.8;
    int result = isgreater(x, y);

    printf("isgreater(%.2f, %.2f) = %d\n", x, y, result);

    return 0;
}

実行結果:

isgreater(3.50, 2.80) = 1

int isgreaterequal(real-floating x, real-floating y)

// 宣言
int isgreaterequal(float x, float y);
int isgreaterequal(double x, double y);
int isgreaterequal(long double x, long double y);

xがyより大きい、または等しいかどうかを判定します。
isgreaterequal(x, y)の値は、(x) >= (y)に等しいです。
isgreaterequal(x, y)が、(x) >= (y)と異なる点は、xとyが順序付けられていない場合に、無効演算の浮動小数点例外が発生しない点です。
(x) >= (y)の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    float x = 3.5;
    float y = 3.5;
    int result = isgreaterequal(x, y);

    printf("isgreaterequal(%.2f, %.2f) = %d\n", x, y, result);

    return 0;
}

実行結果:

isgreaterequal(3.50, 3.50) = 1

int isless(real-floating x, real-floating y)

// 宣言
int isless(float x, float y);
int isless(double x, double y);
int isless(long double x, long double y);

xがyより小さいかどうかを判定します。
isless(x, y)の値は、(x) < (y)に等しいです。
isless(x, y)が(x) < (y)と異なる点は、xとyが順序付けられていない場合に、無効演算の浮動小数点例外が発生しない点です。
(x) < (y)の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    float x = 2.5;
    float y = 3.2;
    int result = isless(x, y);

    printf("isless(%.2f, %.2f) = %d\n", x, y, result);

    return 0;
}

実行結果:

isless(2.50, 3.20) = 1

int islessequal(real-floating x, real-floating y)

// 宣言
int islessequal(float x, float y);
int islessequal(double x, double y);
int islessequal(long double x, long double y);

xがyより小さい、または等しいかどうかを判定します。
islessequal(x, y)の値は、(x) <= (y)と等しいです。
islessequal(x, y)が(x) <= (y)と異なる点は、xとyが順序付けられていない場合に、無効演算の浮動小数点例外が発生しない点です。
(x) <= (y)の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    float x = 2.5;
    float y = 2.5;
    int result = islessequal(x, y);

    printf("islessequal(%.2f, %.2f) = %d\n", x, y, result);

    return 0;
}

実行結果:

islessequal(2.50, 2.50) = 1

int islessgreater(real-floating x, real-floating y)

// 宣言
int islessgreater(float x, float y);
int islessgreater(double x, double y);
int islessgreater(long double x, long double y);

xがyより小さい、または大きいかどうかを判定します。
islessgreater(x, y)の値は、(x) < (y) || (x) > (y)とほぼ同じです。
islessgreater(x, y)が(x) < (y) || (x) > (y)と異なる点は、xとyが順序付けられていない場合に、無効演算の浮動小数点例外が発生しない点です。
また、islessgreater(x, y)は、xとyを2回評価することはありません。
(x) < (y) || (x) > (y)の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    float x = 2.5;
    float y = 3.2;
    int result = islessgreater(x, y);

    printf("islessgreater(%.2f, %.2f) = %d\n", x, y, result);

    return 0;
}

実行結果:

islessgreater(2.50, 3.20) = 1

int isunordered(real-floating x, real-floating y)

// 宣言
int isunordered(float x, float y);
int isunordered(double x, double y);
int isunordered(long double x, long double y);

2つの引数のいずれかが非数(NaN)である場合に非0の値を返し、それ以外の場合に0を返します。
つまり、少なくとも1つの引数が非数である場合に真を表す非0の値を返します。

使用例:

#include <stdio.h>
#include <math.h>

double g_d0 = 0.0;

int main(void)
{
    double x = 1.0 / g_d0;  // Positive infinity
    double y = 0.0 / g_d0;  // NaN
    int result = isunordered(x, y);

    printf("isunordered(%f, %f) = %d\n", x, y, result);

    return 0;
}

実行結果:

isunordered(inf, -nan(ind)) = 1

スポンサーリンク

その他の数学関数

double ceil(double x)

// 宣言
float ceilf(float x);
double ceil(double x);
long double ceill(long double x);

引数x以上の最小の整数値を計算します。
x以上の最小の整数値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.7;
    double result = ceil(x);

    printf("ceil(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

ceil(3.70) = 4.00

double floor(double x)

// 宣言
float floorf(float x);
double floor(double x);
long double floorl(long double x);

引数x以下の最大の整数値を計算します。
x以下の最大の整数値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.7;
    double result = floor(x);

    printf("floor(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

floor(3.70) = 3.00

double fmod(double x, double y)

// 宣言
float fmodf(float x, float y);
double fmod(double x, double y);
long double fmodl(long double x, long double y);

x/yの浮動小数点剰余(余り)を計算します。
yが0以外の場合は、ある整数nに対するx-nyの値となります。
その値は、xと同じ符号でyの絶対値より小さい絶対値を持ちます。
yが0の場合は、結果を0とするか、または定義域エラーを発生させるかは、処理系依存です。
ある整数nに対するx – nyの値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 7.5;
    double y = 2.3;
    double result = fmod(x, y);

    printf("fmod(%.2f, %.2f) = %.2f\n", x, y, result);

    return 0;
}

実行結果:

fmod(7.50, 2.30) = 0.60

double round(double x)

// 宣言
float roundf(float x);
double round(double x);
long double roundl(long double x);

引数xを浮動小数点形式の最も近い整数値に丸めます。
xがちょうど中間にある場合は、丸め方向に関わらず、0から遠い方へ丸めます。
丸めた整数値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.7;
    double result = round(x);

    printf("round(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

round(3.70) = 4.00

long int lrint(double x)

// 宣言
long int lrintf(float x);
long int lrint(double x);
long int lrintl(long double x);

丸め方向に従って、引数xを最も近い整数値に丸めます。
丸めた値が、戻り値の型の範囲外の場合は、結果の値は未規定です。
xの絶対値が大きすぎる場合は、値域エラーが発生する可能性があります。
丸めた整数値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.7;
    long int result = lrint(x);

    printf("lrint(%.2f) = %ld\n", x, result);

    return 0;
}

実行結果:

lrint(3.70) = 4

long int lround(double x)

// 宣言
long int lroundf(float x);
long int lround(double x);
long int lroundl(long double x);

引数xを最も近い整数値に丸めます。
xがちょうど中間にある場合は、丸め方向に関わらず、0から遠い方へ丸めます。
丸めた値が、戻り値の型の範囲外の場合は、結果の値は未規定です。
xの絶対値が大きすぎる場合は、値域エラーが発生する可能性があります。
丸めた整数値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.7;
    long int result = lround(x);

    printf("lround(%.2f) = %ld\n", x, result);

    return 0;
}

実行結果:

lround(3.70) = 4

double nearbyint(double x)

// 宣言
float nearbyintf(float x);
double nearbyint(double x);
long double nearbyintl(long double x);

不正確例外(浮動小数点例外)を発生させずに、丸め方向に従って引数xを浮動小数点形式の整数値に丸めます。
丸めた整数値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.7;
    double result = nearbyint(x);

    printf("nearbyint(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

nearbyint(3.70) = 4.00

double rint(double x)

// 宣言
float rintf(float x);
double rint(double x);
long double rintl(long double x);

不正確例外(浮動小数点例外)が発生することがあるという点を除いて、nearbyint関数と同じ動作です。
丸めた整数値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.7;
    double result = rint(x);

    printf("rint(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

rint(3.70) = 4.00

long long int llrint(double x)

// 宣言
long long int llrintf(float x);
long long int llrint(double x);
long long int llrintl(long double x);

丸め方向に従って、引数xを最も近い整数値に丸めます。
丸めた値が、戻り値の型の範囲外の場合は、結果の値は未規定です。
xの絶対値が大きすぎる場合は、値域エラーが発生する可能性があります。
丸めた整数値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.7;
    long long int result = llrint(x);

    printf("llrint(%.2f) = %lld\n", x, result);

    return 0;
}

実行結果:

llrint(3.70) = 4

long long int llround(double x)

// 宣言
long long int llroundf(float x);
long long int llround(double x);
long long int llroundl(long double x);

引数xを最も近い整数値に丸めます。
xがちょうど中間にある場合は、丸め方向に関わらず、0から遠い方へ丸めます。
丸めた値が、戻り値の型の範囲外の場合が、結果の値は未規定です。
xの絶対値が大きすぎる場合は、値域エラーが発生する可能性があります。
丸めた整数値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.7;
    long long int result = llround(x);

    printf("llround(%.2f) = %lld\n", x, result);

    return 0;
}

実行結果:

llround(3.70) = 4

double fdim(double x, double y)

// 宣言
float fdimf(float x, float y);
double fdim(double x, double y);
long double fdiml(long double x, long double y);

xとyの正の差を計算します。
xがyより大きい場合はx-yを、xがy以下の場合は+0を返します。
計算時に値域エラーが発生する可能性があります。
実引数の正の差が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 5.0;
    double y = 3.0;
    double result = fdim(x, y);

    printf("fdim(%.2f, %.2f) = %.2f\n", x, y, result);

    return 0;
}

実行結果:

fdim(5.00, 3.00) = 2.00

double fmax(double x, double y)

// 宣言
float fmaxf(float x, float y);
double fmax(double x, double y);
long double fmaxl(long double x, long double y);

xとyの大きい方の値を求めます。
実引数の大きい方の値が返されます。
引数にNaNと数値が指定された場合は、数値が返されます。
引数が両方ともNaNの場合は、NaNが返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 7.5;
    double y = 3.2;
    double result = fmax(x, y);

    printf("fmax(%.2f, %.2f) = %.2f\n", x, y, result);

    return 0;
}

実行結果:

fmax(7.50, 3.20) = 7.50

double fmin(double x, double y)

// 宣言
float fminf(float x, float y);
double fmin(double x, double y);
long double fminl(long double x, long double y);

xとyの小さい方の値を求めます。
小さい方の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 7.5;
    double y = 3.2;
    double result = fmin(x, y);

    printf("fmin(%.2f, %.2f) = %.2f\n", x, y, result);

    return 0;
}

実行結果:

fmin(7.50, 3.20) = 3.20

double remainder(double x, double y)

// 宣言
float remainderf(float x, float y);
double remainder(double x, double y);
long double remainderl(long double x, long double y);

IEEE60559が要求する剰余x REM yを計算します。
y≠0の場合は、剰余r = x REM yは、x/yの正確な値に最も近い整数をnとしたときのr = x – nyです。
|n-x/y| = 1/2 のときは、いつでもnは偶数となります。
r=0の場合は、符号はxの符号です。
x REM y の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double result;
    double numerator = 10.5;
    double denominator = 3.0;

    result = remainder(numerator, denominator);

    printf("The remainder of %.1lf/%.1lf is: %.1lf\n", numerator, denominator, result);
    
    return 0;
}

実行結果:

The remainder of 10.5/3.0 is: -1.5

double remquo(double x, double y, int *quo)

// 宣言
float remquof(float x, float y, int *quo);
double remquo(double x, double y, int *quo);
long double remquol(long double x, long double y, int *quo);

remainder関数と同じく、剰余を計算します。
x REM y の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 7.5;
    double y = 2.3;
    double result = remainder(x, y);

    printf("remainder(%.2f, %.2f) = %.2f\n", x, y, result);

    return 0;
}

実行結果:

remainder(7.50, 2.30) = 0.60

double nan(const char *tagp)

// 宣言
float nanf(const char *tagp);
double nan(const char *tagp);
long double nanl(const char *tagp);

引数の文字列をNaNに変換します。
nan(“n文字列”)の呼出しは、strtod(“NAN(n文字列)”,(char**)NULL)と同じです。
空の文字列を指定してのnan関数呼出しは、strtod(“NAN()”,(char**)NULL)と同じです。
tagpがn文字列または空の文字列を指していない場合のnan関数呼出しは、strtod(“NAN”,(char**)NULL)と同じです。
n文字列は、数字と非数字(英大文字小文字と_)から成る文字列で、意味は処理系に依存します。
tagpが示す内容を持つqNaNが返されます。
処理系がQNaN非サポートの場合は0が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double result = nan(NULL);

    printf("NaN: %f\n", result);

    return 0;
}

実行結果:

NaN: nan

double nextafter(double x, double y)

// 宣言
float nextafterf(float x, float y);
double nextafter(double x, double y);
long double nextafterl(long double x, long double y);

最初にxおよびyをその関数の型に変換します。
この変換はnextafter関数がマクロとして実装されている場合でも実行されます。
そして、実軸上でxから見てyの方向にある、xの次に表現可能な値を求めます。
xがその型で表現できる最大の有限な値で、かつ結果が無限大またはその型で表現できない場合は、値域エラーが発生する可能性があります。
yの方向にあるxの次の値が返されます。
xとyが同じ場合はyが返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;
    double y = 2.0;
    double result = nextafter(x, y);

    printf("nextafter(%.2f, %.2f) = %.17f\n", x, y, result);

    return 0;
}

実行結果:

nextafter(1.00, 2.00) = 1.00000000000000022

double nexttoward(double x, long double y)

// 宣言
float nexttowardf(float x, long double y);
double nexttoward(double x, long double y);
long double nexttowardl(long double x, long double y);

yの型がlong doubleであることと、xとyが同じならyをdouble型に変換して返すことを除いて、nextafter関数と同じです。
yの方向にあるxの次の値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;
    long double y = 2.0;
    double result = nexttoward(x, y);

    printf("nexttoward(%.2f, %.2Lf) = %.17f\n", x, y, result);

    return 0;
}

実行結果:

nexttoward(1.00, 2.00) = 1.00000000000000022

double erf(double x)

// 宣言
float erff(float x);
double erf(double x);
long double erfl(long double x);

引数xの誤差関数を計算します。
xの誤差関数が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;
    double result = erf(x);

    printf("erf(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

erf(1.00) = 0.84

double erfc(double x)

// 宣言
float erfcf(float x);
double erfc(double x);
long double erfcl(long double x);

引数xの余誤差関数を計算します。
xが大きすぎる場合は、値域エラーが発生します。
xの余誤差関数が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 1.0;
    double result = erfc(x);

    printf("erfc(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

erfc(1.00) = 0.16

double lgamma(double x)

// 宣言
float lgammaf(float x);
double lgamma(double x);
long double lgammal(long double x);

引数xのガンマ関数の絶対値の自然対数を計算します。
xが大きすぎる場合は、値域エラーが発生します。
xが負の整数または0の場合は、値域エラーが発生する可能性があります。
xのガンマ関数の絶対値の自然対数が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 5.0;
    double result = lgamma(x);

    printf("lgamma(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

lgamma(5.00) = 3.18

double tgamma(double x)

// 宣言
float tgammaf(float x);
double tgamma(double x);
long double tgammal(long double x);

引数xのガンマ関数を計算します。
xが負の整数の場合は、定義域エラーが発生します。
xが0、かつ計算結果が表現できない場合は、定義域エラーが発生します。
xの絶対値が、大きすぎたり小さすぎたりする場合は、値域エラーが発生する可能性があります。
xのガンマ関数が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 5.0;
    double result = tgamma(x);

    printf("tgamma(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

tgamma(5.00) = 24.00

double trunc(double x)

// 宣言
float truncf(float x);
double trunc(double x);
long double truncl(long double x);

引数xを浮動小数点形式の最も近い整数値に丸めます。
丸めた値は、その絶対値がxの絶対値以下の値です。
切り捨てた整数値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.7;
    double result = trunc(x);

    printf("trunc(%.2f) = %.2f\n", x, result);

    return 0;
}

実行結果:

trunc(3.70) = 3.00

double copysign(double x, double y)

// 宣言
float copysignf(float x, float y);
double copysign(double x, double y);
long double copysignl(long double x, long double y);

絶対値がx、符号がyの値を生成します。
xがNaNの場合は、符号がyのNaNを生成します。
符号付き0が存在していても、算術演算においては負の0を正常に処理できないという処理系では、0の符号を正とします。
xの絶対値を持ち、yの符号を持つ値が返されます。

使用例:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 3.7;
    double y = -2.5;
    double result = copysign(x, y);

    printf("copysign(%.2f, %.2f) = %.2f\n", x, y, result);

    return 0;
}

実行結果:

copysign(3.70, -2.50) = -3.70

スポンサーリンク
C言語
コッコ隊長の勉強部屋

コメント