度をつけてMath.sinを使う

度でMath.sinを使用する

1. 前書き

この短いチュートリアルでは、JavaのMath.sin()関数を使用して正弦値を計算する方法と、角度の値を度とラジアンの間で変換する方法について説明します。

2. ラジアン対 度

デフォルトでは、Java Math library expects values to its trigonometric functions to be in radiansです。

念のため、radians are just another way to express the measure of an angle、変換は次のとおりです。

double inRadians = inDegrees * PI / 180;
inDegrees = inRadians * 180 / PI;

Javaは、toRadians toDegreesを使用してこれを簡単にします。

double inRadians = Math.toRadians(inDegrees);
double inDegrees = Math.toDegrees(inRadians);

Javaの三角関数のいずれかを使用しているときはいつでも、we should first think about what is the unit of our input

3. Math.sinの使用

Javaが提供する多くのメソッドの1つであるMath.sinメソッドを見ると、この原則が実際に機能していることがわかります。

public static double sin(double a)

これは、数学的な正弦関数とit expects its input to be in radiansに相当します。 それで、度単位であることがわかっている角度があるとしましょう。

double inDegrees = 30;

まず、ラジアンに変換する必要があります。

double inRadians = Math.toRadians(inDegrees);

そして、サイン値を計算できます。

double sine = Math.sin(inRadians);

しかし、if we know it to already be in radians, then we don’t need to do the conversion

@Test
public void givenAnAngleInDegrees_whenUsingToRadians_thenResultIsInRadians() {
    double angleInDegrees = 30;
    double sinForDegrees = Math.sin(Math.toRadians(angleInDegrees)); // 0.5

    double thirtyDegreesInRadians = 1/6 * Math.PI;
    double sinForRadians = Math.sin(thirtyDegreesInRadians); // 0.5

    assertTrue(sinForDegrees == sinForRadians);
}

thirtyDegreesInRadians はすでにラジアンであるため、同じ結果を得るために最初に変換する必要はありませんでした。

4. 結論

この簡単な記事では、ラジアンと度を確認してから、Math.sin.を使用してそれらを操作する方法の例を確認しました。

いつものように、この例のover on GitHubのソースコードを確認してください。