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

本文是小编最新给大家整理的关于Java中Math类常用方法的知识,通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧,

Java常用类库Math

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

一、Field Summary

Modifier and TypeFieldDescription
static 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^b
static 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>> 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) = 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 TypeFieldDescription
static 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)y
static TfloorDiv​(A x, B y)返回≤ x/y的最大值,y=0则抛出ArithmeticException异常,int(int,int),long(long,int/long
static TfloorMod​(A x, B y)返回floor(x%y),即x-(x/y)*y,int(int/long,int),long(long,long)

2.3 三角函数

Modifier and TypeFieldDescription
static doublesin​(double a)返回角度的三角正弦值
static doublecos​(double a)返回角度的三角余弦值
static doubletan​(double a)返回角度的三角正切
static doubleasin​(double a)返回a的反正弦值,返回的角度-pi/2~pi/2
static doubleacos​(double a)返回a的反余弦值,返回的角度0.0~pi
static doubleatan​(double a)返回a的反正切值,返回的角度-pi/2~pi/2

2.4 其他不常用方法

Modifier and TypeFieldDescription
static 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^a
static doubleexpm1​(double x)返回 e^x - 1
static doublefma​(double a, double b, double c)返回a*b+c
static floatfma​(float a, float b, float c)返回a*b+c
static 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.0
static floatsignum​(float f)返回参数的signum函数; 如果参数为零则为零,如果参数大于零则为1.0f,如果参数小于零则为-1.0f
static 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大小

总结

以上就是深入了解Java核心类库--Math类的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java