C ライブラリ関数 -cosh()
説明
C ライブラリ関数double cosh(double x)戻り値xの双曲余弦。
cosh()は C 標準ライブラリ<math.h>の1つの関数で、双曲余弦値を計算するために使用されます。双曲余弦関数(cosh)は重要な数学関数であり、多くの科学・工学アプリケーションで広く利用されています。
宣言
以下は cosh() 関数の宣言です。
#include <math.h> double cosh(double x); float coshf(float x); long double coshl(long double x);
引数
- x-- 入力された実数。双曲余弦関数の独立変数を示します。
双曲余弦関数の定義は次のとおりです:

戻り値
この関数は x の双曲余弦を返します。
例
以下の例は、cosh() 関数の使い方を示しています。
例
#include <stdio.h>
#include <math.h>
int main ()
{
double x;
x = 0.5;
printf(%lf の双曲余弦は %lf\n", x, cosh(x));
x = 1.0;
printf(%lf の双曲余弦は %lf\n", x, cosh(x));
x = 1.5;
printf(%lf の双曲余弦は %lf\n", x, cosh(x));
return(0);
}
#include <math.h>
int main ()
{
double x;
x = 0.5;
printf(%lf の双曲余弦は %lf\n", x, cosh(x));
x = 1.0;
printf(%lf の双曲余弦は %lf\n", x, cosh(x));
x = 1.5;
printf(%lf の双曲余弦は %lf\n", x, cosh(x));
return(0);
}
上記のプログラムをコンパイルして実行すると、次の結果が得られます:
0.500000 的双曲余弦是 1.127626 1.000000 的双曲余弦是 1.543081 1.500000 的双曲余弦是 2.352410
複数の値の双曲余弦を計算する
次の例は、複数の値の双曲余弦を計算する方法を示しています:
例
#include <stdio.h>
#include <math.h>
int main() {
double values[] = {0, 0.5, 1, 1.5, 2};
int num_values = sizeof(values) / sizeof(values[0]);
for (int i = 0; i < num_values; i++) {
double x = values[i];
double result = cosh(x);
printf("cosh(%f) = %f\n", x, result);
}
return 0;
}
#include <math.h>
int main() {
double values[] = {0, 0.5, 1, 1.5, 2};
int num_values = sizeof(values) / sizeof(values[0]);
for (int i = 0; i < num_values; i++) {
double x = values[i];
double result = cosh(x);
printf("cosh(%f) = %f\n", x, result);
}
return 0;
}
上記のプログラムをコンパイルして実行すると、次の結果が得られます:
cosh(0.000000) = 1.000000 cosh(0.500000) = 1.127626 cosh(1.000000) = 1.543081 cosh(1.500000) = 2.352410 cosh(2.000000) = 3.762196
コード解析
- コードは次の計算を試みます
cosh(1000.0)、これはオーバーフローを引き起こす可能性のある入力値です。 errno0 にリセットされ、次に呼び出されますcosh(x)。- 確認
errnoに等しいかどうかERANGE、もしそうであれば、オーバーフローエラーメッセージを出力します。 - エラーがなければ、計算結果を出力します。
まとめ
cosh()関数は指定された値の双曲余弦を計算するために使用され、双曲関数演算を処理するための重要なツールです。使用時には、発生する可能性のあるオーバーフローエラーの処理に注意する必要があります。適切に使用することでcosh()関数を使用すると、数学計算、物理シミュレーション、工学アプリケーションで正確な結果を得ることができます。
C 標準ライブラリ - <math.h>