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

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

Java常用类库Math

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

一、Field Summary

Modifier and Type Field Description static double E 自然对数的基数 static double PI π

二、Method Summary

2.1 常用方法

Modifier and Type Field Description ① static double ceil​(double a) 返回≥a的最小整数 static double floor​(double a) 返回≤a的最大整数 static int round​(float a) 返回四舍五入后的值 ② static T max​(T a, T b) 返回两个数据类型为T中较大的 static T min​(T a, T b) 返回两个数据类型为T中较x小的 ③ static double random() 返回[0.0,1.0)。 static double sqrt​(double a) 返回正平方根。 static T abs(T a) 返回数据类型为T的绝对值 static double pow​(double a, double b) 返回a^b static double log​(double a) 返回基数为e的对数 static double log10​(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) < 1            long 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 NaN            return (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:4        System.out.println("≤3.2的最大整数为:"+Math.floor(3.2));//output:3        System.out.println("3.2四舍五入为:"+Math.round(3.2));//output:3        System.out.println("-1,5中较大的数为:"+Math.max(-1,5));//output:5        System.out.println("-1,5中较小的数为:"+Math.min(-1,5));//output:-1        System.out.println("随机产生[0,5)范围的数"+Math.random()*5);//output:[0,5)中任意的随机数        System.out.println("25的平方根为:"+Math.sqrt(25));//output:5        System.out.println("-9的绝对值为:"+Math.abs(-9));//output:9        System.out.println("2^3的值为:"+Math.pow(2,3));//output:8        System.out.println("以e为基数的对数为:"+Math.log(10));        System.out.println("以10为基数的对数为:"+Math.log10(100));//output:2    }}

2.2 算数运算

Modifier and Type Field Description static T addExact​(T x, T y) 返回x+y,溢出则抛出异常T(int,long) static T multiplyExact​(A x, B y) 返回x*y,结果溢出则抛出异常int(int,int),long(long,int/long) static long multiplyFull​(int x, int y) 返回(long)x*(long)y static T floorDiv​(A x, B y) 返回≤ x/y的最大值,y=0则抛出ArithmeticException异常,int(int,int),long(long,int/long static T floorMod​(A x, B y) 返回floor(x%y),即x-(x/y)*y,int(int/long,int),long(long,long)

2.3 三角函数

Modifier and Type Field Description static double sin​(double a) 返回角度的三角正弦值 static double cos​(double a) 返回角度的三角余弦值 static double tan​(double a) 返回角度的三角正切 static double asin​(double a) 返回a的反正弦值,返回的角度-pi/2~pi/2 static double acos​(double a) 返回a的反余弦值,返回的角度0.0~pi static double atan​(double a) 返回a的反正切值,返回的角度-pi/2~pi/2

2.4 其他不常用方法

Modifier and Type Field Description static double cosh​(double x) 返回 double值的双曲余弦值 static double cbrt​(double a) 返回 double值的多维数据集根 static double copySign​(double magnitude, double sign) 返回带有第二个浮点参数符号的第一个浮点参数 static float copySign​(float magnitude, float sign) 返回带有第二个浮点参数符号的第一个浮点参数 static int decrementExact​(int a) 返回a-1,如果结果溢出int则抛出异常 static long decrementExact​(long a) 返回a-1,如果结果溢出long则抛出异常 static double exp​(double a) 返回e^a static double expm1​(double x) 返回 e^x – 1 static double fma​(double a, double b, double c) 返回a*b+c static float fma​(float a, float b, float c) 返回a*b+c static int getExponent​(double d) 返回 double表示中使用的无偏指数 static int getExponent​(float f) 返回 float表示中使用的无偏指数 static double hypot​(double x, double y) 返回sqrt( x 2 + y 2 ),没有中间溢出或下溢 static double IEEEremainder​(double f1, double f2) 根据IEEE 754标准规定,计算两个参数的余数运算 static int incrementExact​(int a) 返回以1递增的参数,如果结果溢出 int则抛出异常 static long incrementExact​(long a) 返回以1递增的参数,如果结果溢出 long则抛出异常 static double log1p​(double x) 返回参数和的总和的自然对数 static long multiplyHigh​(long x, long y) 返回 long作为两个64位因子的128位乘积的最高64位 static int negateExact​(int a) 返回参数的否定,如果结果溢出 int则抛出异常 static long negateExact​(long a) 返回参数的否定,如果结果溢出 long则抛出异常 static double nextAfter​(double start, double direction) 返回第二个参数方向上第一个参数旁边的浮点数 static float nextAfter​(float start, double direction) 返回第二个参数方向上第一个参数旁边的浮点数 static double nextDown​(double d) 返回负无穷大方向上与 d相邻的浮点值 static float nextDown​(float f) 返回负无穷大方向上与 f相邻的浮点值 static double nextUp​(double d) 返回正无穷大方向上与 d相邻的浮点值 static float nextUp​(float f) 返回正无穷大方向上与 f相邻的浮点值 static double rint​(double a) 返回与 double值最接近的 double值,该值等于数学整数 static double scalb​(double d, int scaleFactor) 返回 d ×2 scaleFactor舍入,就像通过单个正确舍入的浮点乘以双 scaleFactor值集的成员一样 static float scalb​(float f, int scaleFactor) 返回 f ×2 scaleFactor舍入,就像通过单个正确舍入的浮点乘以浮点值集的成员一样 static double signum​(double d) 返回参数的signum函数; 如果参数为零,则为零;如果参数大于零,则为1.0;如果参数小于零,则为-1.0 static float signum​(float f) 返回参数的signum函数; 如果参数为零则为零,如果参数大于零则为1.0f,如果参数小于零则为-1.0f static double sinh​(double x) 返回 double值的双曲正弦值 static int subtractExact​(int x, int y) 返回参数的差异,如果结果溢出 int则抛出异常 static long subtractExact​(long x, long y) 返回参数的差异,如果结果溢出 long则抛出异常 static double tanh​(double x) 返回 double值的双曲正切值 static double toDegrees​(double angrad) 将以弧度测量的角度转换为以度为单位测量的近似等效角度 static int toIntExact​(long value) 返回long参数的值; 如果值溢出int则抛出异常 static double toRadians​(double angdeg) 将以度为单位测量的角度转换为以弧度为单位测量的近似等效角度 static double ulp​(double d) 返回参数的ulp大小 static float ulp​(float f) 返回参数的ulp大小

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!

我提着行李,独自一人向远方走去,夕阳将我的身影拉得斜长,

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

相关文章:

你感兴趣的文章:

标签云: