Java常用类整理3—结合源码理解java.lang.Math

基于java11 API和源码进行整理

Math

java.lang.Math,该类提供了大量的数学相关的计算方法(Math类是调用StrictMath类的方法计算,StrictMath类包含执行基本数值运算的方法,如初等指数、对数、平方根和三角函数。),结合源码整理Math常用方法如下:

E、PI

Math.E、Math.PI最接近自然对数常量e和圆周率π的常量

源码:
最接近e的全局静态常量:
public static final double E = 2.7182818284590452354;
最接近π的的全局静态常量
public static final double PI = 3.14159265358979323846;

exp

Math.exp(double e)返回结果为 e a {e^a} ea, 该方法掌握用法即可。

源码:
       public static double exp(double a) {
        return StrictMath.exp(a); // default impl. delegates to StrictMath
    }
源码解读:返回调用StrictMath.exp(a)方法计算结果
-----------------------------------------------------------------------------------------
  StrictMath.exp(a)源码:  
    
    public static double exp(double a) {
        return FdLibm.Exp.compute(a);
    }

源码解读:调用FdLibm.Exp.compute(a)方法计算结果。
    
   FdLibm类是 “自由分布的数学库”的端口,版本5.3,从CJavaExp是期内部类。conpute为Exp类的计算方法,对如何计算的不做了解。

log、log10​​​

Math.log(double a):返回双精度值的自然对数(以e为底)。

Math,log10(double a):返回以10为底的双精度值的对数

这两个方法掌握用法即可。

源码:
   public static double log(double a) {
        return StrictMath.log(a); // default impl. delegates to StrictMath
    }
-----------------------------------------------------------------------------------------
    public static double log10(double a) {
        return StrictMath.log10(a); // default impl. delegates to StrictMath
    } 

sqrt

Math.sqrt(double a):返回双精度值正确四舍五入的正平方根

该方法掌握用法即可。

源码:
public static double sqrt(double a) {
        return StrictMath.sqrt(a); // default impl. delegates to StrictMath
                                   // Note that hardware sqrt instructions
                                   // frequently can be directly used by JITs
                                   // and should be much faster than doing
                                   // Math.sqrt in software.
    }

ceil

Math.ceil(double a):返回大于或等于参数的最小数学整数的(最接近负无穷)双精度值。

源码:
  public static double ceil(double a) {
        return StrictMath.ceil(a); // default impl. delegates to StrictMath
    }  

floor

Math.floor(double a):返回小于或等于参数的最大数学整数的(最接近正无穷)双精度值。

源码:
  public static double floor(double a) {
     return StrictMath.floor(a); // default impl. delegates to StrictMath
  }

rint

Math.rint(double a):四舍五入的方式返回最接近的整数的双精度值。

源码:
public static double rint(double a) {
        return StrictMath.rint(a); // default impl. delegates to StrictMath
    }

round

Math.round(float a):四舍五入返回最接近的整数(int类型)。

Math.round(double a):四舍五入返回最接近的整数(long类型)。

pow

Math.pow(double a,double b):返回 a b {a^b} ab,为双精度值

源码:
    public static double pow(double a, double b) {
        return StrictMath.pow(a, b); // default impl. delegates to StrictMath
    }

abs

Math.abs(double a):返回绝对值

Math.abs(float a):返回绝对值

Math.abs(int a):返回绝对值

Math.abs(long a):返回绝对值

addExact

Math.addExact(int a,int b):返回其参数的总和,如果结果溢出 int则抛出异常。

Math.addExact(long a,long b):返回其参数的总和,如果结果溢出 long则抛出异常。

源码:
    public static int addExact(int x, int y) {
        int r = x + y;
        // HD 2-12 Overflow iff both arguments have the opposite sign of the result
        if (((x ^ r) & (y ^ r)) < 0) {
            throw new ArithmeticException("integer overflow");
        }
        return r;
    }

-----------------------------------------------------------------------------------------
	public static long addExact(long x, long y) {
        long r = x + y;
        // HD 2-12 Overflow iff both arguments have the opposite sign of the result
        if (((x ^ r) & (y ^ r)) < 0) {
            throw new ArithmeticException("long overflow");
        }
        return r;
    }


源码解读:
    先计算参数x+y的和,然后判断是否超出数据类型范围,没有返回结果,超出抛出ArithmeticException异常。

subtractExact

Math.subtractExat(int a,int b):返回其参数的差,如果结果溢出 int则抛出异常。

Math.subtractExat(long a,long b):返回其参数的差,如果结果溢出 int则抛出异常。

源码:
    public static int subtractExact(int x, int y) {
        int r = x - y;
        // HD 2-12 Overflow iff the arguments have different signs and
        // the sign of the result is different from the sign of x
        if (((x ^ y) & (x ^ r)) < 0) {
            throw new ArithmeticException("integer overflow");
        }
        return r;
    }
-----------------------------------------------------------------------------------------
    public static long subtractExact(long x, long y) {
        long r = x - y;
        // HD 2-12 Overflow iff the arguments have different signs and
        // the sign of the result is different from the sign of x
        if (((x ^ y) & (x ^ r)) < 0) {
            throw new ArithmeticException("long overflow");
        }
        return r;
    }

源码解读:
    先计算参数x-y的和,然后判断是否超出数据类型范围,没有返回结果,超出抛出ArithmeticException异常。

incremetExat

Math.incrementExat(int a):返回实参加1,超过int范围抛出异常。

Math.incrementExat(long a):返回实参加1,超过long范围抛出异常。

源码:
    public static int incrementExact(int a) {
        if (a == Integer.MAX_VALUE) {
            throw new ArithmeticException("integer overflow");
        }

        return a + 1;
    }
-----------------------------------------------------------------------------------------
    public static long incrementExact(long a) {
        if (a == Long.MAX_VALUE) {
            throw new ArithmeticException("long overflow");
        }

        return a + 1L;
    }

源码解读:
    首先判断是否达到范围最大值,达到则抛出异常,没有则加1

**Math.decrementExat(int a)、Math.decrementExat(double a)**为自减,用法和原理与自增一致,不做过多说明。

negateExact

Math.negateExact(int a):返回a的相反数,超出int范围抛出异常。

Math.negateExact(long a):返回a的相反数,超出long范围抛出异常。

源码:
    public static int negateExact(int a) {
        if (a == Integer.MIN_VALUE) {
            throw new ArithmeticException("integer overflow");
        }

        return -a;
    }
-----------------------------------------------------------------------------------------
    public static long negateExact(long a) {
        if (a == Long.MIN_VALUE) {
            throw new ArithmeticException("long overflow");
        }

        return -a;
    }

max、min

Math.max(int a,int b):返回两个参数的最大值

Math.min(int a,int b):返回两个参数的最小值

random

Math.random():返回0-1的随机双精度浮点数。

源码:
public static double random() {
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值