深入了解Java核心类库??Math类

深入了解Java核心类库??Math类,博智网带你了解详细信息 。
目录

  • Java常用类库Math
    • 一、Field Summary
    • 二、Method Summary
      • 2.1 常用方法
      • 2.1.1 部分方法源码
      • 2.2 算数运算
      • 2.3 三角函数
      • 2.4 其他不常用方法
  • 总结

    Java常用类库Math
    类Math包含用于执行基本数字运算的方法,例如基本指数,对数,平方根和三角函数

    一、Field SummaryModifier and TypeFieldDescriptionstatic doubleE自然对数的基数static doublePIπ
    二、Method Summary
    2.1 常用方法Modifier and TypeFieldDescription①static doubleceil​(double a)返回≥a的最小整数static doublefloor​(double a)返回≤a的最大整数static intround​(float a)返回四舍五入后的值②static Tmax​(T a, T b)返回两个数据类型为T中较大的static Tmin​(T a, T b)返回两个数据类型为T中较x小的③static doublerandom()返回[0.0,1.0) 。static doublesqrt​(double a)返回正平方根 。static Tabs(T a)返回数据类型为T的绝对值static doublepow​(double a, double b)返回a^bstatic doublelog​(double a)返回基数为e的对数static doublelog10​(double a)返回基数为10的对数
    2.1.1 部分方法源码 public static long round(double a) {long longBits = Double.doubleToRawLongBits(a);long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)>> (DoubleConsts.SIGNIFICAND_WIDTH - 1);long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2+ DoubleConsts.EXP_BIAS) - biasedExp;if ((shift & -64) == 0) { // shift >= 0 && shift < 64// a is a finite number such that pow(2,-64) <= ulp(a) < 1long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)| (DoubleConsts.SIGNIF_BIT_MASK + 1));if (longBits < 0) {r = -r;}// In the comments below each Java expression evaluates to the value// the corresponding mathematical expression:// (r) evaluates to a / ulp(a)// (r >> shift) evaluates to floor(a * 2)// ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)// (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)return ((r >> shift) + 1) >> 1;} else {// a is either// - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2// - a finite number with ulp(a) >= 1 and hence a is a mathematical integer// - an infinity or NaNreturn (long) a;}}public static int max(int a, int b) {return (a >= b) ? a : b;}public static int min(int a, int b) {return (a <= b) ? a : b;}public static int abs(int a) {return (a < 0) ? -a : a;}
    2.1.2 具体实现
    public class Test {public static void main(String[] args) {System.out.println("≥3.2的最小整数为:"+Math.ceil(3.2));//output:4System.out.println("≤3.2的最大整数为:"+Math.floor(3.2));//output:3System.out.println("3.2四舍五入为:"+Math.round(3.2));//output:3System.out.println("-1,5中较大的数为:"+Math.max(-1,5));//output:5System.out.println("-1,5中较小的数为:"+Math.min(-1,5));//output:-1System.out.println("随机产生[0,5)范围的数"+Math.random()*5);//output:[0,5)中任意的随机数System.out.println("25的平方根为:"+Math.sqrt(25));//output:5System.out.println("-9的绝对值为:"+Math.abs(-9));//output:9System.out.println("2^3的值为:"+Math.pow(2,3));//output:8System.out.println("以e为基数的对数为:"+Math.log(10));System.out.println("以10为基数的对数为:"+Math.log10(100));//output:2}}
    2.2 算数运算Modifier and TypeFieldDescriptionstatic TaddExact​(T x, T y)返回x+y,溢出则抛出异常T(int,long)static TmultiplyExact​(A x, B y)返回x*y,结果溢出则抛出异常int(int,int),long(long,int/long)static longmultiplyFull​(int x, int y)返回(long)x*(long)ystatic TfloorDiv​(A x, B y)返回≤ x/y的最大值,y=0则抛出ArithmeticException异常,int(int,int),long(long,int/longstatic TfloorMod​(A x, B y)返回floor(x%y),即x-(x/y)*y,int(int/long,int),long(long,long)
    2.3 三角函数Modifier and TypeFieldDescriptionstatic doublesin​(double a)返回角度的三角正弦值static doublecos​(double a)返回角度的三角余弦值static doubletan​(double a)返回角度的三角正切static doubleasin​(double a)返回a的反正弦值,返回的角度-pi/2~pi/2static doubleacos​(double a)返回a的反余弦值,返回的角度0.0~pistatic doubleatan​(double a)返回a的反正切值,返回的角度-pi/2~pi/2
    2.4 其他不常用方法Modifier and TypeFieldDescriptionstatic doublecosh​(double x)返回 double值的双曲余弦值static doublecbrt​(double a)返回 double值的多维数据集根static doublecopySign​(double magnitude, double sign)返回带有第二个浮点参数符号的第一个浮点参数static floatcopySign​(float magnitude, float sign)返回带有第二个浮点参数符号的第一个浮点参数static intdecrementExact​(int a)返回a-1,如果结果溢出int则抛出异常static longdecrementExact​(long a)返回a-1,如果结果溢出long则抛出异常static doubleexp​(double a)返回e^astatic doubleexpm1​(double x)返回 e^x - 1static doublefma​(double a, double b, double c)返回a*b+cstatic floatfma​(float a, float b, float c)返回a*b+cstatic intgetExponent​(double d)返回 double表示中使用的无偏指数static intgetExponent​(float f)返回 float表示中使用的无偏指数static doublehypot​(double x, double y)返回sqrt( x 2 + y 2 ),没有中间溢出或下溢static doubleIEEEremainder​(double f1, double f2)根据IEEE 754标准规定,计算两个参数的余数运算static intincrementExact​(int a)返回以1递增的参数,如果结果溢出 int则抛出异常static longincrementExact​(long a)返回以1递增的参数,如果结果溢出 long则抛出异常static doublelog1p​(double x)返回参数和的总和的自然对数static longmultiplyHigh​(long x, long y)返回 long作为两个64位因子的128位乘积的最高64位static intnegateExact​(int a)返回参数的否定,如果结果溢出 int则抛出异常static longnegateExact​(long a)返回参数的否定,如果结果溢出 long则抛出异常static doublenextAfter​(double start, double direction)返回第二个参数方向上第一个参数旁边的浮点数static floatnextAfter​(float start, double direction)返回第二个参数方向上第一个参数旁边的浮点数static doublenextDown​(double d)返回负无穷大方向上与 d相邻的浮点值static floatnextDown​(float f)返回负无穷大方向上与 f相邻的浮点值static doublenextUp​(double d)返回正无穷大方向上与 d相邻的浮点值static floatnextUp​(float f)返回正无穷大方向上与 f相邻的浮点值static doublerint​(double a)返回与 double值最接近的 double值,该值等于数学整数static doublescalb​(double d, int scaleFactor)返回 d ×2 scaleFactor舍入,就像通过单个正确舍入的浮点乘以双 scaleFactor值集的成员一样static floatscalb​(float f, int scaleFactor)返回 f ×2 scaleFactor舍入,就像通过单个正确舍入的浮点乘以浮点值集的成员一样static doublesignum​(double d)返回参数的signum函数; 如果参数为零,则为零;如果参数大于零,则为1.0;如果参数小于零,则为-1.0static floatsignum​(float f)返回参数的signum函数; 如果参数为零则为零,如果参数大于零则为1.0f,如果参数小于零则为-1.0fstatic doublesinh​(double x)返回 double值的双曲正弦值static intsubtractExact​(int x, int y)返回参数的差异,如果结果溢出 int则抛出异常static longsubtractExact​(long x, long y)返回参数的差异,如果结果溢出 long则抛出异常static doubletanh​(double x)返回 double值的双曲正切值static doubletoDegrees​(double angrad)将以弧度测量的角度转换为以度为单位测量的近似等效角度static inttoIntExact​(long value)返回long参数的值; 如果值溢出int则抛出异常static doubletoRadians​(double angdeg)将以度为单位测量的角度转换为以弧度为单位测量的近似等效角度static doubleulp​(double d)返回参数的ulp大小static floatulp​(float f)返回参数的ulp大小