Math、BigInteger、BigDecimal

Math类

java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回值类型一般为double型。

 

  常用方法:

abs     绝对值
acos,asin,atan,cos,sin,tan  三角函数
sqrt     平方根
pow(double a,doble b)     a的b次幂
log    自然对数
exp    e为底指数
max(double a,double b)
min(double a,double b)
random()      返回0.0到1.0的随机数
long round(double a)     double型数据a转换为long型(四舍五入)
toDegrees(double angrad)     弧度—>角度
toRadians(double angdeg)     角度—>弧度

 

BigInteger类

   Integer类作为int的包装类,能存储的最大整型值为2的31次方-1,Long类也是有限的,最大为2的63次方-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类都无能为力,更不用说进行运算了。

java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供所有Java 的基本整数操作符的对应物,并提供java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。

构造器:

  BigInteger(String val):根据字符串构建BigInteger对象

 

常用方法:
public
BigInteger abs():返回此BigInteger 的绝对值的BigInteger。 BigInteger add(BigInteger val) :返回其值为(this + val) 的BigInteger BigInteger subtract(BigInteger val) :返回其值为(this - val) 的BigInteger BigInteger multiply(BigInteger val) :返回其值为(this * val) 的BigInteger BigInteger divide(BigInteger val) :返回其值为(this / val) 的BigInteger。整数相除只保留整数部分。 BigInteger remainder(BigInteger val) :返回其值为(this % val) 的BigInteger。 BigInteger[] divideAndRemainder(BigInteger val):返回包含(this / val) 后跟(this % val) 的两个BigInteger 的数组。 BigInteger pow(int exponent) :返回其值为(thisexponent) 的BigInteger。

 代码测试:

 

public static void main(String[] args) {
        
        BigInteger bigInteger = new BigInteger("111111111");
        BigInteger abs = bigInteger.abs();//返回绝对值
        System.out.println(abs);
        BigInteger add = bigInteger.add(new BigInteger("50011111"));//加法
        System.out.println(add);
        BigInteger subtract = bigInteger.subtract(new BigInteger("50011111"));//减法
        System.out.println(subtract);
        BigInteger multiply = bigInteger.multiply(new BigInteger("50011111"));//乘法
        System.out.println(multiply);
        BigInteger divide = bigInteger.divide(new BigInteger("50011111"));//除法
        System.out.println(divide);
        BigInteger remainder = bigInteger.remainder(new BigInteger("10"));//取余
        System.out.println(remainder);
        BigInteger [] divideAndRemainder = bigInteger.divideAndRemainder(new BigInteger("100000"));//除法和取余的结果
        for (BigInteger bigInteger2 : divideAndRemainder) {
            System.out.println(bigInteger2);
        }
        BigInteger pow = bigInteger.pow(2);//当前数的2次方
        System.out.println(pow);
        
    }

 

 BigDecimal类

  一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故用到java.math.BigDecimal类。

    BigDecimal类支持不可变的、任意精度的有符号十进制定点数。

构造器:
  public BigDecimal(double val)
  public BigDecimal(String val)

常用方法:
    public BigDecimal add(BigDecimal augend)//加
  public BigDecimal subtract(BigDecimal subtrahend)//减
  public BigDecimal multiply(BigDecimal multiplicand)//乘
  public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)//除

代码演示:

 

public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("11.111111111111111111111");
        System.out.println(bigDecimal);
        BigDecimal add = bigDecimal.add(new BigDecimal("11.11"));//
        System.out.println(add);
        BigDecimal subtract = bigDecimal.subtract(new BigDecimal("11.11"));//
        System.out.println(subtract);
        BigDecimal multiply = bigDecimal.multiply(new BigDecimal("11.11"));//
        System.out.println(multiply);
        BigDecimal divide = bigDecimal.divide(new BigDecimal("100"));//
        System.out.println(divide);//除不断会报异常Non-terminating decimal expansion; no exact representable decimal result.
        
        BigDecimal divide2 = bigDecimal.divide(new BigDecimal("11.11"), 6, BigDecimal.ROUND_HALF_UP);//除后四舍五入保留2位小数
        System.out.println(divide2);

    // 大小比较使用compareTo
    int a = 
bigDecimal.compareTo(new BigDecimal("11.111111111111111111111"))
    // a = -1,表示bigdemical小于bigdemical2;
    // a = 0,表示bigdemical等于bigdemical2;
    // a = 1,表示bigdemical大于bigdemical2; }

 使用注意:

  当double必须用作BigDecimal的源时,请注意,此构造方法提供了一个准确转换;它不提供与以下操作相同的结果:先使用Double.toString(double)方法,然后使用BigDecimal(String)构造方法,将double转换为String。要获取该结果,请使用static valueOf(double)方法。

 

 

 

 

 

posted @ 2021-03-04 20:07  不会掉头发的程序猿  阅读(116)  评论(0)    收藏  举报