Guava-intmath

提供:Dev Guides
移動先:案内検索

グアバ-IntMathクラス

IntMathはintでユーティリティメソッドを提供します。

クラス宣言

以下は com.google.common.math.IntMath クラスの宣言です-

@GwtCompatible(emulated = true)
public final class IntMath
   extends Object

方法

Sr.No Method & Description
1

static int binomial(int n, int k)

nを返します。nはk(nとkの2項係数とも呼ばれます)、または結果がintに収まらない場合はInteger.MAX_VALUEとしても知られています。

2

static int checkedAdd(int a, int b)

オーバーフローしない場合、aとbの合計を返します。

3

static int checkedMultiply(int a, int b)

オーバーフローしない場合、aとbの積を返します。

4

static int checkedPow(int b, int k)

オーバーフローしない場合、bのk乗を返します。

5

static int checkedSubtract(int a, int b)

オーバーフローしない場合、aとbの差を返します。

6

static int divide(int p, int q, RoundingMode mode)

pをqで除算し、指定されたRoundingModeを使用して丸めた結果を返します。

7

static int factorial(int n)

n !、つまり最初のn個の正の整数の積、n == 0の場合は1、結果がintに収まらない場合はInteger.MAX_VALUEを返します。

8

static int gcd(int a, int b)

a、bの最大公約数を返します。

9

static boolean isPowerOfTwo(int x)

xが2のべき乗を表す場合、trueを返します。

10

static int log10(int x, RoundingMode mode)

指定された丸めモードに従って丸められた、xの10を底とする対数を返します。

11

static int log2(int x, RoundingMode mode)

指定された丸めモードに従って丸められたxの2を底とする対数を返します。

12

static int mean(int x, int y)

xとyの算術平均を、負の無限大に丸めて返します。

13

static int mod(int x, int m)

x mod m、m未満の非負の値を返します。

14

static int pow(int b, int k)

bのk乗を返します。

15

static int sqrt(int x, RoundingMode mode)

指定された丸めモードで丸められたxの平方根を返します。

継承されるメソッド

このクラスは、次のクラスからメソッドを継承します-

  • java.lang.Object

IntMathクラスの例

たとえば、* C:/> Guava。*で選択したエディターを使用して、次のJavaプログラムを作成します。

GuavaTester.java

import java.math.RoundingMode;
import com.google.common.math.IntMath;

public class GuavaTester {

   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testIntMath();
   }

   private void testIntMath() {
      try {
         System.out.println(IntMath.checkedAdd(Integer.MAX_VALUE, Integer.MAX_VALUE));

      } catch(ArithmeticException e) {
         System.out.println("Error: " + e.getMessage());
      }

      System.out.println(IntMath.divide(100, 5, RoundingMode.UNNECESSARY));
      try {
        //exception will be thrown as 100 is not completely divisible by 3
        //thus rounding is required, and RoundingMode is set as UNNESSARY
         System.out.println(IntMath.divide(100, 3, RoundingMode.UNNECESSARY));

      } catch(ArithmeticException e) {
         System.out.println("Error: " + e.getMessage());
      }

      System.out.println("Log2(2): " + IntMath.log2(2, RoundingMode.HALF_EVEN));

      System.out.println("Log10(10): " + IntMath.log10(10, RoundingMode.HALF_EVEN));

      System.out.println("sqrt(100): " + IntMath.sqrt(IntMath.pow(10,2), RoundingMode.HALF_EVEN));

      System.out.println("gcd(100,50): " + IntMath.gcd(100,50));

      System.out.println("modulus(100,50): " + IntMath.mod(100,50));

      System.out.println("factorial(5): " + IntMath.factorial(5));
   }
}

結果を確認する

次のように javac コンパイラを使用してクラスをコンパイルします-

C:\Guava>javac GuavaTester.java

GuavaTesterを実行して結果を確認します。

C:\Guava>java GuavaTester

結果をご覧ください。

Error: overflow
20
Error: mode was UNNECESSARY, but rounding was necessary
Log2(2): 1
Log10(10): 1
sqrt(100): 10
gcd(100,50): 50
modulus(100,50): 0
factorial(5): 120