Javaの大きな数値
1436 ワード
基本的な整数と浮動小数点数の精度が需要を満たす場合、javaを使用することができる.mathパッケージの2つの有用なクラス:BigIntegerとBigDecimal.この2つのクラスは、任意の長さの数値シーケンスを含む数値を処理することができる.BigIntegerクラスは任意精度の整数演算を実現し,BigDecimalクラスは任意精度の浮動小数点数演算を実現した.
静的valueOf法を使用すると、通常の数値を大きな数値に変換できます.
ただし、+や*など、よく知られている算術演算子を使用して大きな数値を処理することはできません.大きな数値クラスのaddメソッドやmultiplyメソッドを使用する必要があります.
java.math.BigInteger BigInteger add(BigInteger other)------および BigInteger subtract(BigInteger other)----------差 Biginteger multiply(BigInteger other)--------積 BigInteger divide(BigInteger other)-------- を除く BigInteger mod(BigInteger other)--------余 int compareTo(BigInteger other)
この大きな整数が別の大きな整数otherと等しい場合は、0を返します.戻り負の数より小さい.戻り正数より大きい. static BigInteger valueOf(long x)
戻り値がxに等しい大きな整数 BigDecimal add(BigDecimal other)--------および BigDecimal substract(BigDecimal other)------差 BigDecimal multiply(BigDecimal other)--------積 BigDecimal divide(BigDecimal other RoundingMode)------商 計算者には、丸め方式(round mod)が必要です. int compareTo(BigDecimal other)
参照整数. static BigDecimal valueOf(long x) static BigDecimal valueOf(long x, int scale)
戻り値はxまたはx/10 scaleの大きな実数です.
静的valueOf法を使用すると、通常の数値を大きな数値に変換できます.
BigInteger a = BigInteger.valueOf(100);
ただし、+や*など、よく知られている算術演算子を使用して大きな数値を処理することはできません.大きな数値クラスのaddメソッドやmultiplyメソッドを使用する必要があります.
bigInteger c = a.add(b); //c = a+b;
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); //d = c*(b+2)
java.math.BigInteger
この大きな整数が別の大きな整数otherと等しい場合は、0を返します.戻り負の数より小さい.戻り正数より大きい.
戻り値がxに等しい大きな整数
参照整数.
戻り値はxまたはx/10 scaleの大きな実数です.