Guava - IntMath 類



IntMath 提供了關於 int 的實用方法。

類宣告

以下是 com.google.common.math.IntMath 類的宣告:

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

方法

序號 方法及描述
1

static int binomial(int n, int k)

返回 n 選 k 的結果,也稱為 n 和 k 的二項式係數,如果結果不適合 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 類的示例

使用您選擇的任何編輯器建立以下 Java 程式,例如在 C:/> Guava. 中。

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
guava_math_utilities.htm
廣告

© . All rights reserved.