Java中的Math函数常用方法都在这里

Math函数中常用的一些方法

  • Math包含用于执行基本数字运算的方法

算术运算

  1. Math.abs(a):取a的绝对值
  2. Math.sqrt(a):取a的平方根
  3. Math.cbrt(a):取a的立方根
  4. Math.max(a,b):取a、b之间的最大值
  5. Math.min(a,b):取a、b之间的最小值
  6. Math.pow(a,b):取a的b平方

Math.abs(a) :取绝对值

  • 方法 描述
    abs(double a) 返回 double值的绝对值。
       /*
        Math.abs() 取绝对值
        */
		System.out.println(7);//7
        System.out.println(-7);//-7
        System.out.println(Math.abs(10.3));//10.3
        System.out.println(Math.abs(-10.3));//10.3

Math.sqrt(a):取平方根

  • 方法 描述
    sqrt(double a) 返回 double值的正确舍入正平方根。
        /*
        Math.sqrt();开平方根
         */
        System.out.println(Math.sqrt(9));//3.0
        System.out.println(Math.sqrt(16));//4.0
        System.out.println(Math.sqrt(-16));//NaN

注:开平方根的参数不能为负数。

Math.cbrt(a):取立方根

  • 方法 描述
    cbrt(double a) 返回 double值的立方根。
		/*
        Math.cbrt();开立方根
         */
        System.out.println(Math.cbrt(8));//2.0
        System.out.println(Math.cbrt(27));//3.0
        System.out.println(Math.cbrt(-27));//-3.0

Math.max(a,b): 取最大值

  • 方法 描述
    max(double a, double b) 返回两个 double值中较大的 double
        System.out.println(Math.max(2,3));//3
        System.out.println(Math.max(5.3,4.6));//5.3
        System.out.println(Math.max(-2.7,4));//4.0

Math.min(a,b):取最小值

  • 方法 描述
    min(double a, double b) 返回两个 double值中较小的 double
        /*
        Math.min();取最小值
         */
        System.out.println(Math.min(-1.8,6));//-1.8
        System.out.println(Math.min(0.7,10));//0.7
        System.out.println(Math.min(19,6));//6

Math.pow(a,b):计算a的b平方

  • 方法 描述
    pow(double a, double b) 返回第一个参数的值,该值是第二个参数的幂。
        /*
        Math.pow()
         */
        System.out.println(Math.pow(2,0));//1.0
        System.out.println(Math.pow(3,1));//3.0
        System.out.println(Math.pow(2,2));//4.0

算术进位

  1. Math.ceil():逢余进一
  2. Math.floor():逢余舍一
  3. Math.rint():四舍五入
  4. Math.round():四舍五入

Math.ceil(a):取大于等于a的最小整数

  • 方法 描述
    ceil(double a) 返回大于或等于参数且等于整数的最小值 double
		/*
        Math.ceil()
         */
		System.out.println(Math.ceil(10.2));//11.0
        System.out.println(Math.ceil(9.8));//10.0
        System.out.println(Math.ceil(-10.2));//-10.0

Math.floor(a):取小于等于a的最大整数

  • 方法 描述
    floor(double a) 返回小于或等于参数且等于整数的最大值double
        /*
        Math.floor()
         */
        System.out.println(Math.floor(1.3));//1.0
        System.out.println(Math.floor(0.8));//0.0
        System.out.println(Math.floor(10.5));//10.0
        System.out.println(Math.floor(-100.9));//-101.0

Math.rint():四舍五入,返回double值

  • 方法 描述
    rint(double a) 返回与 double值最接近的 double值,该值等于数学整数。
System.out.println(Math.rint(10.1));//10.0
        System.out.println(Math.rint(10.5));//10.0
        System.out.println(Math.rint(10.8));//11.0
        System.out.println(Math.rint(0.2));//0.0
        System.out.println(Math.rint(0.5));//0.0
        System.out.println(Math.rint(0.8));//1.0
        System.out.println(Math.rint(-0.2));//-0.0
        System.out.println(Math.rint(-0.5));//-0.0
        System.out.println(Math.rint(-0.8));//-1.0
        System.out.println(Math.rint(-10.2));//-10.0
        System.out.println(Math.rint(-10.8));//-11.0
        System.out.println(Math.rint(-10.5));//-10.0

:在0.5时取偶数

Math.round():四舍五入,double时返回long值,float时返回int值

  • 方法 描述
    round(double a) 返回与参数最接近的 long ,并将关系四舍五入为正无穷大。
    round(float a) 返回与参数最接近的 int ,并将关系四舍五入为正无穷大。
        System.out.println(Math.round(10.1));//10
        System.out.println(Math.round(10.5));//11
        System.out.println(Math.round(10.53));//11
        System.out.println(Math.round(10.8));//11
        System.out.println(Math.round(-10.1));//-10
        System.out.println(Math.round(-10.5));//-10
        System.out.println(Math.round(-10.53));//-11
        System.out.println(Math.round(-10.9));//-11

:四舍五入,float时返回int值,double时返回long值

随机数

Math.random() 随机数,在范围 [0.0,1.0) 内随机取一个值

  • 方法 描述
    random() 返回带有正号的 double值,大于或等于 0.0且小于 1.0
        System.out.println(Math.random());//[0.0,1.0)
        System.out.println(Math.random()+1);//[1.0,2.0)
        System.out.println(Math.random()*10);//[0.0,10.0)
        System.out.println(Math.random()*10+1);//[1.0,11.0)
        System.out.println(Math.random()*100+0.5);//[0.5,100.5)

:返回类型为double类型。

三角函数

  1. Math.sin():正弦
  2. Math.cos():余弦
  3. Math.ten():正切

sin():正弦

public static double sin​(double a)

返回角度的三角正弦值。 特别案例:

  • 如果参数是NaN或无穷大,则结果为NaN。
  • 如果参数为零,则结果为零,其参数符号相同。

参数 :

  • a - 角度,以弧度表示。

(一周的弧度数为2πr/r=2π,360°角=2π弧度,因此,1弧度约为57.3°,即57°17’44.806’’,1°为π/180弧度)

double PI = Math.PI;//double值比任何其他 pi更接近,圆的圆周与其直径的比率。
System.out.println(Math.sin(0));//0.0
System.out.println(Math.sin(6.28));//-0.0031853017931379904
System.out.println(Math.sin(9.42));//0.0047779425901285115
System.out.println(Math.sin(1.57));//0.9999996829318346
System.out.println(Math.sin(3.14));//0.0015926529164868282

:输入的是以弧度值表示,返回的值在范围[-1,1]

cos():余弦

public static double cos​(double a)

返回角度的三角余弦值。 特别案例:

  • 如果参数是NaN或无穷大,则结果为NaN。
  • 结果必须是半单调的

参数

  • a - 角度,以弧度表示。
System.out.println(Math.cos(0));//1.0
System.out.println(Math.cos(1.57));//7.963267107332633E-4
System.out.println(Math.cos(3.14));//-0.9999987317275395
System.out.println(Math.cos(4.71));//-0.0023889781122815386
System.out.println(Math.cos(6.28));//-0.9999987317275395

:输入的是以弧度值表示,返回的值在范围[-1,1]

ten():正切

public static double tan​(double a)

返回角度的三角正切。 特别案例:

  • 如果参数是NaN或无穷大,则结果为NaN。
  • 如果参数为零,则结果为零,其参数符号相同。

计算结果必须在精确结果的1 ulp范围内。 结果必须是半单调的。

参数

  • a - 角度,以弧度表示。
System.out.println(Math.tan(0));//0.0
System.out.println(Math.tan(1.57));//1255.7655915007897
System.out.println(Math.tan(3.14));//-0.001592654936407223
System.out.println(Math.tan(4.71));//418.58782265388515
System.out.println(Math.tan(6.28));//-0.003185317952531891

ten90°不存在
即输入的弧度不能为(π/2±kπ)返回的值在范围[-∞,+∞]

总结

在Math函数中,还有许多关于数字的基本运算,但是基本上常用的一些方法都在上文有详细的列举,对于这些常用的方法还是需要熟练运用,避免在开发过程中或平时做任务遇到时,不知所措。

猜你喜欢

转载自blog.csdn.net/weixin_45819587/article/details/119488460