Javaクラス数学演算

数学演算クラス

Mathクラスのクラスの数学的な操作

クラス属性値

  • Math.Eを使用

    ^

  • Math.PI

    パイ

クラスメソッド

Mathクラスは、すべての方法がある静的 Mathクラスは普通のプロパティではありませんので、タイプ。

ラウンド()メソッド

  • 最寄りの戻りパラメータ値intに丸め
public static int round(float a)

ABS()メソッド

  • 絶対値を返します。
public static double abs(double a)

MAX()メソッド

  • 値の大きい方のint値を返します。
public static int max(int a , int b)

ランダムアクションクラスの確率

  • java.utilパッケージ

ランダム()コンストラクタ

  • 新しい乱数ジェネレータを作成します。

next()メソッド

  • 次の擬似乱数を生成します
protected int next (int bits)

nextInt()メソッド

  • 次の擬似乱数を返します。

  • nextInt(int型n)は、
    • 乱数nよりも小さい戻ります

36 7宝くじインスタンスから選択

import java.util.Random;

public class TestDemo {
    public static void main(String [] args) throws CloneNotSupportedException {
        Random ran = new Random();
        int data[] = new int [7] ; //开辟一个数组
        int foot = 0 ;
        while (foot < 7) {
            int t = ran.nextInt(37); // 随机生成返回一个不大于37的数
            if (!isRepeat(data,t)) { // 查重
                data[foot ++] = t ;
            }
        }
        java.util.Arrays.parallelSort(data);
        for (int x = 0 ; x < data.length ; x ++) {
            System.out.print(data[x] + "\t");
        }
    }
    public static boolean isRepeat(int temp[] , int num) { // 查重
        if (num == 0 ) {
            return true ;
        }
        for (int x = 0 ; x < temp.length ; x ++) {
            if (temp[x] == num) {
                return true ;
            }
        }
        return false;
    }
}

大きなクラスのデジタル操作

BigIntegerのクラス

import java.math.BigInteger;
import java.util.Random;

public class TestDemo {
    public static void main(String [] args) throws CloneNotSupportedException {
        BigInteger big_A = new BigInteger("12345678912345678912356789");
        BigInteger big_B = new BigInteger("218372948729847298347289") ;
        System.out.println("加法操作 >>> " + (big_A.add(big_B)));
        System.out.println("减法操作 >>> " + (big_A.subtract(big_B)));
        System.out.println("乘法操作 >>> " + (big_A.multiply(big_B)));
        System.out.println("除法操作 >>> " + (big_A.divide(big_B)));
    }
}
  • 結果:
加法操作 >>> 12564051861075526210704078
减法操作 >>> 12127305963615831614009500
乘法操作 >>> 2695962308160819899591376721692771747399598895021
除法操作 >>> 56

BigDecimal : 大浮点数

  • BigIntegerのは整数のみを保存することができ、小数は(浮動小数点)を保存しない、とのBigDecimalの小数(浮動小数点)データを格納することができる。設ける構成でBigDecimalクラス:
public BigDecimal(String val) ;
public BigDecimal(double val) ; 

小数を自動的に計算丸めしかし恐らくMath.round()メソッド、実装が丸め操作、

除算

public BigDecimal divide(BigDecimal divisor , int scale , int round);
  • BigDecimalのdivsor:配当
  • int型のスケール:予約小数点以下の桁
  • INTラウンド:キャリーモード

  • 例: * [重要]コンテンツ

import java.math.BigDecimal;

class MyMath {
    /**
     * 实现准确的位数的四舍五入操作
     * @param num 进行四舍五入操作的数字
     * @param scale 要保留的小数位数
     * @return 处理后的数据
     */
    public static double round(double num , int scale) {
        BigDecimal bigA = new BigDecimal(num) ;
        BigDecimal bigB = new BigDecimal(1) ;
        // ROUND_HALF_UP:向“最接近的”数字舍入
        // doubleValue() : 转 double 型数据
        return bigA.divide(bigB, scale , BigDecimal.ROUND_HALF_UP).doubleValue() ;
    }
}

public class TestDemo {
    public static void main(String [] args) {
        System.out.println(MyMath.round(19.224532 , 2));
        System.out.println(MyMath.round(3.1465926 , 2));
    }
}

要約:

  • 欠陥ラウンドをクリアするMathクラス()メソッド
  • ランダムクラスは、乱数を生成し、
  • データ処理、大量の場合は、使用してBigIntegerのとはBigDecimalを、2つのクラスは数のサブクラスに属しています

おすすめ

転載: www.cnblogs.com/wangyuyang1016/p/11105291.html
おすすめ