Cライブラリ関数 -asin()
説明
asin()はC標準ライブラリ<math.h>内の関数で、数値の逆正弦値(逆三角関数の正弦逆関数とも呼ばれます)を計算するために使用されます。この関数は -1 から 1 の間の実数を受け取り、その値の逆正弦値をラジアン単位で返します。
宣言
以下は asin() 関数の宣言です:
#include <math.h> double asin(double x); float asinf(float x); long double asinl(long double x);
パラメータ
- x-- -1 から 1 の間の浮動小数点数。
戻り値
- の逆正弦値を
xラジアン単位で返します。戻り値の範囲は [-π/2, π/2] です。 - もし
xx が [-1, 1] の範囲内にない場合、関数は NaN を返し、適切な数学エラーを設定します(例:EDOM)。
例
以下の例は asin() 関数の使用方法を示しています。
例
#include <stdio.h>
#include <math.h>
int main() {
double x = 0.5;
double result = asin(x);
printf("asin(%f) = %f radians\n", x, result);
printf("asin(%f) = %f degrees\n", x, result * 180 / M_PI);
return 0;
}
#include <math.h>
int main() {
double x = 0.5;
double result = asin(x);
printf("asin(%f) = %f radians\n", x, result);
printf("asin(%f) = %f degrees\n", x, result * 180 / M_PI);
return 0;
}
コード解説:
- コードはまずヘッダーファイルを含めます
<math.h>と<stdio.h>。 - 変数を定義します
x、値は 0.5 です。 - を使用して
asin()の逆正弦値をx計算し、結果をresultに格納します。 - の逆正弦値を
xラジアン単位で出力します。 - 結果を角度に変換して出力します。変換式は
弧度 * (180 / π)。
上記のプログラムをコンパイルして実行すると、次の結果が得られます:
asin(0.500000) = 0.523599 radians asin(0.500000) = 30.000000 degrees
エラー処理
入力されたxが有効範囲 [-1, 1] を超える場合、asin()は NaN を返し、errno を設定します。errnoためにEDOM。isnan()関数を使用して結果が NaN かどうかを確認できます:
例
#include <stdio.h>
#include <math.h>
#include <errno.h>
int main() {
double x = 2.0; // 無効な入力、[-1, 1] の範囲外
errno = 0; // errno をリセット
double result = asin(x);
if (errno == EDOM) {
printf("Input value is out of domain. asin(%f) is not defined.\n", x);
} else {
printf("asin(%f) = %f radians\n", x, result);
}
return 0;
}
#include <math.h>
#include <errno.h>
int main() {
double x = 2.0; // 無効な入力、[-1, 1] の範囲外
errno = 0; // errno をリセット
double result = asin(x);
if (errno == EDOM) {
printf("Input value is out of domain. asin(%f) is not defined.\n", x);
} else {
printf("asin(%f) = %f radians\n", x, result);
}
return 0;
}
コード解説:
- コードは
asin(2.0)の計算を試みます。2.0 は [-1, 1] の範囲内にないため、これは無効な入力です。 errnoが 0 にリセットされ、asin(x)。- を呼び出して
errnoがEDOMと等しいかどうかを確認します。等しい場合は、エラーメッセージを出力します。 - エラーがない場合は、計算結果を出力します。
上記のプログラムをコンパイルして実行すると、次の結果が得られます:
asin(2.000000) = nan radians
asin()この関数は指定された値の逆正弦値を計算するために使用され、三角関数の逆演算を処理する一般的なツールです。使用時には、入力値が [-1, 1] の範囲内にあることを確認し、可能性のあるエラー条件を処理する必要があります。結果を角度に変換することで、結果をより直感的に理解できます。
C標準ライブラリ - <math.h>