グアバの数学ユーティリティの手引き

グアバの数学ユーティリティのガイド

1. 概要

この記事では、Guava Libraryで利用可能ないくつかの便利な数学演算について説明します。

Guavaで利用できる4つの数学ユーティリティクラスがあります。

  1. IntMath –int値の操作

  2. LongMath –長い値に対する操作

  3. BigIntegerMath –BigIntegersの操作

  4. DoubleMath –double値の操作

2. IntMathユーティリティ

IntMathは、整数値に対して数学演算を実行するために使用されます。 それぞれの動作を説明する利用可能なメソッドリストを確認します。

2.1. binomial(int n, int k)

この関数は、nとkの二項係数を計算します。 結果が整数範囲内であることを確認します。 それ以外の場合は、Integer.MAX_VALUEを返します。 答えは、式n / k(n-k)を使用して導き出すことができます。

@Test
public void whenBinomialOnTwoInt_shouldReturnResultIfUnderInt() {
    int result = IntMath.binomial(6, 3);

    assertEquals(20, result);
}

@Test
public void whenBinomialOnTwoInt_shouldReturnIntMaxIfOVerflowInt() {
    int result = IntMath.binomial(Integer.MAX_VALUE, 3);

    assertEquals(Integer.MAX_VALUE, result);
}

2.2. ceilingPowerOfTwo(int x)

これは、x以上の最小の2のべき乗の値を計算します。 結果nは、2 ^(n-1)

@Test
public void whenCeilPowOfTwoInt_shouldReturnResult() {
  int result = IntMath.ceilingPowerOfTwo(20);

  assertEquals(32, result);
}

2.3. checkedAdd(int a, int b)およびその他

この関数は、2つのパラメーターの合計を計算します。 これは、結果がオーバーフローした場合にArithmeticExceptionをスローする追加のチェックを提供します。

@Test
public void whenAddTwoInt_shouldReturnTheSumIfNotOverflow() {
    int result = IntMath.checkedAdd(1, 2);

    assertEquals(3, result);
}

@Test(expected = ArithmeticException.class)
public void whenAddTwoInt_shouldThrowArithmeticExceptionIfOverflow() {
    IntMath.checkedAdd(Integer.MAX_VALUE, 100);
}

Guavaは、オーバーフローする可能性のある他の3つの演算子、checkedMultiply、checkedPow,、およびcheckedSubtractのメソッドをチェックしました。

2.4. divide(int p, int q, RoundingMode mode)

これは単純な除算ですが、丸めモードを定義できます。

@Test
public void whenDivideTwoInt_shouldReturnTheResultForCeilingRounding() {
    int result = IntMath.divide(10, 3, RoundingMode.CEILING);

    assertEquals(4, result);
}

@Test(expected = ArithmeticException.class)
public void whenDivideTwoInt_shouldThrowArithmeticExIfRoundNotDefinedButNeeded() {
    IntMath.divide(10, 3, RoundingMode.UNNECESSARY);
}

2.5. factorial(int n)

nの階乗値を計算します。 i.e the product of the first n positive integers. n = 0の場合は1を返し、結果がint範囲に収まらない場合はInteger.MAX_VALUEを返します。 結果は、n x(n-1)x(n-2)x…によって取得できます。 x 2 x 1:

@Test
public void whenFactorialInt_shouldReturnTheResultIfInIntRange() {
    int result = IntMath.factorial(5);

    assertEquals(120, result);
}

@Test
public void whenFactorialInt_shouldReturnIntMaxIfNotInIntRange() {
    int result = IntMath.factorial(Integer.MAX_VALUE);

    assertEquals(Integer.MAX_VALUE, result);
}

2.6. floorPowerOfTwo(int x)

2の最大の累乗を返します。結果はx以下です。 結果nは、2 ^ n

@Test
public void whenFloorPowerOfInt_shouldReturnValue() {
    int result = IntMath.floorPowerOfTwo(30);

    assertEquals(16, result);
}

2.7. gcd(int a, int b)

この関数は、aとbの最大公約数を与えます。

@Test
public void whenGcdOfTwoInt_shouldReturnValue() {
    int result = IntMath.gcd(30, 40);
    assertEquals(10, result);
}

2.8. isPowerOfTwo(int x)

xが2のべき乗かどうかを返します。 値が2のべき乗の場合はtrueを返し、それ以外の場合はfalseを返します。

@Test
public void givenIntOfPowerTwo_whenIsPowOfTwo_shouldReturnTrue() {
    boolean result = IntMath.isPowerOfTwo(16);

    assertTrue(result);
}

@Test
public void givenIntNotOfPowerTwo_whenIsPowOfTwo_shouldReturnFalse() {
    boolean result = IntMath.isPowerOfTwo(20);

    assertFalse(result);
}

2.9. isPrime(int n)

この関数は、渡された数値が素数であるかどうかを示します。

@Test
public void givenNonPrimeInt_whenIsPrime_shouldReturnFalse() {
    boolean result = IntMath.isPrime(20);

    assertFalse(result);
}

2.10. log10(int x, RoundingMode mode)

このAPIは、指定された数値の10を底とする対数を計算します。 結果は、提供された丸めモードを使用して丸められます。

@Test
public void whenLog10Int_shouldReturnTheResultForCeilingRounding() {
    int result = IntMath.log10(30, RoundingMode.CEILING);

    assertEquals(2, result);
}

@Test(expected = ArithmeticException.class)
public void whenLog10Int_shouldThrowArithmeticExIfRoundNotDefinedButNeeded() {
    IntMath.log10(30, RoundingMode.UNNECESSARY);
}

2.11. log2(int x, RoundingMode mode)

指定された数値の2を底とする対数を返します。 結果は、提供された丸めモードを使用して丸められます。

@Test
public void whenLog2Int_shouldReturnTheResultForCeilingRounding() {
    int result = IntMath.log2(30, RoundingMode.CEILING);

    assertEquals(5, result);
}

@Test(expected = ArithmeticException.class)
public void whenLog2Int_shouldThrowArithmeticExIfRoundNotDefinedButNeeded() {
    IntMath.log2(30, RoundingMode.UNNECESSARY);
}

2.12. mean(int x, int y)

この関数を使用すると、2つの値の平均を計算できます。

@Test
public void whenMeanTwoInt_shouldReturnTheResult() {
    int result = IntMath.mean(30, 20);

    assertEquals(25, result);
}

2.13. mod(int x, int m)

1つの数値を他の数値で整数除算した余りを返します。

@Test
public void whenModTwoInt_shouldReturnTheResult() {
    int result = IntMath.mod(30, 4);
    assertEquals(2, result);
}

2.14. pow(int b, int k)

bの値をkの累乗で返します。

@Test
public void whenPowTwoInt_shouldReturnTheResult() {
    int result = IntMath.pow(6, 4);

    assertEquals(1296, result);
}

2.15. saturatedAdd(int a, int b)およびその他

オーバーフローまたはアンダーフローが発生したときにそれぞれ値Integer.MAX_VALUEまたはInteger.MIN_VALUEを返すことにより、オーバーフローまたはアンダーフローを制御できるという利点を持つ合計関数:

@Test:
public void whenSaturatedAddTwoInt_shouldReturnTheResult() {
    int result = IntMath.saturatedAdd(6, 4);

    assertEquals(10, result);
}

@Test
public void whenSaturatedAddTwoInt_shouldReturnIntMaxIfOverflow() {
    int result = IntMath.saturatedAdd(Integer.MAX_VALUE, 1000);

    assertEquals(Integer.MAX_VALUE, result);
}

他に3つの飽和APIがあります:saturatedMultiply、saturatedPow、およびsaturatedSubtract。

2.16. sqrt(int x, RoundingMode mode)

指定された数値の平方根を返します。 結果は、提供された丸めモードを使用して丸められます。

@Test
public void whenSqrtInt_shouldReturnTheResultForCeilingRounding() {
    int result = IntMath.sqrt(30, RoundingMode.CEILING);

    assertEquals(6, result);
}

@Test(expected = ArithmeticException.class)
public void whenSqrtInt_shouldThrowArithmeticExIfRoundNotDefinedButNeded() {
    IntMath.sqrt(30, RoundingMode.UNNECESSARY);
}

3. LongMathユーティリティ

LongMathには、Long値のユーティリティがあります。 ほとんどの操作はIntMathユーティリティに似ていますが、ここで説明する例外がいくつかあります。

3.1. mod(long x, int m)およびmod(long x, long m)

x mod mを返します。 xのmによる整数除算の剰余:

@Test
public void whenModLongAndInt_shouldModThemAndReturnTheResult() {
    int result = LongMath.mod(30L, 4);

    assertEquals(2, result);
}
@Test
public void whenModTwoLongValues_shouldModThemAndReturnTheResult() {
    long result = LongMath.mod(30L, 4L);

    assertEquals(2L, result);
}

4. BigIntegerMathユーティリティ

BigIntegerMathは、タイプBigIntegerで数学演算を実行するために使用されます。

このユーティリティには、IntMath.に似たメソッドがいくつかあります。

5. DoubleMathユーティリティ

DoubleMathユーティリティは、double値に対して操作を実行するために使用されます。

BigIntegerユーティリティと同様に、使用可能な操作の数は制限されており、IntMathユーティリティと類似性を共有しています。 このユーティリティクラスでのみ使用可能な例外的な機能をいくつかリストします。

5.1. isMathematicalInteger(double x)

xが数学的な整数かどうかを返します。 数値がデータ損失なしで整数として表現できるかどうかを確認します。

@Test
public void givenInt_whenMathematicalDouble_shouldReturnTrue() {
    boolean result = DoubleMath.isMathematicalInteger(5);

    assertTrue(result);
}

@Test
public void givenDouble_whenMathematicalInt_shouldReturnFalse() {
    boolean result = DoubleMath.isMathematicalInteger(5.2);

    assertFalse(result);
}

5.2. log2(double x)

xの2を底とする対数を計算します。

@Test
public void whenLog2Double_shouldReturnResult() {
    double result = DoubleMath.log2(4);

    assertEquals(2, result, 0);
}

6. 結論

このクイックチュートリアルでは、Guava数学ユーティリティの便利な機能をいくつか紹介しました。

いつものように、ソースコードはover on GitHubで見つけることができます。