1. 概要

この記事では、Guavaライブラリで利用できるいくつかの便利な数学演算を紹介します。

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(nk)を使用して導き出すことができます。

@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. 階乗(int n)

nの階乗値を計算します。 つまり、最初のn個の正の整数の積です。 n = 0の場合は1を返し、結果がint範囲に収まらない場合はInteger.MAX_VALUEを返します。 結果は、nx(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)

結果がx以下である2の最大の累乗を返します。 結果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モード)

この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モード)

指定された数値の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)

ある数値を他の数値で除算した余りを返します。

@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モード)

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

@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)

xmodmを返します。 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数学ユーティリティ関数について説明しました。

いつものように、ソースコードはGitHubにあります。