グアバの数学ユーティリティの手引き
1概要
この記事では、Guava Libraryで利用可能ないくつかの便利な数学演算を見ます。
Guavaには4つの数学ユーティリティクラスがあります。
-
IntMath
– int値に対する操作 -
LongMath
– long値に対する操作 -
BigIntegerMath
–
BigIntegers
に対する操作 -
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)<x <2 ^ nのようになります。
@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モード)
これは単純な除算ですが、丸めモードを定義することができます。
@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の階乗値を計算します。すなわち、最初のn個の正の整数の積です。 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 <x <2 ^(n 1)のようになります。
@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
の値を返すことによって、オーバーフローまたはアンダーフローを制御するという利点を持つsum関数。
@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);
}
他に飽和状態のAPIが3つあります。
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)
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(ダブルx)
xの2を底とする対数を計算します。
@Test
public void whenLog2Double__shouldReturnResult() {
double result = DoubleMath.log2(4);
assertEquals(2, result, 0);
}
6. 結論
このクイックチュートリアルでは、便利なGuava数学ユーティリティ関数をいくつか紹介しました。
いつものように、ソースコードはhttps://github.com/eugenp/tutorials/tree/master/guava[over on GitHub]にあります。