JavaMath类的基本⽤法⼀、基本常⽤的Math类⽅法
Math.abs( ) - 返回参数的绝对值。
参数可以是 int, float, long, double, short, byte类型
1public static void main(String args[]){
2        Integer a = -8;
3double d = -100;
4float f = -90;
5
6        System.out.println(Math.abs(a));  //8
7        System.out.println(Math.abs(d));  //100.0
8        System.out.println(Math.abs(f));  //90.0
9    }
Math.max(  $ , $  )、Math.min(  $ , $  ) - 返回两个数的最⼤值、最⼩值
参数可以是 int, float, long, double类型(参数也可以是short,byte,但是返回类型会转成int)
1public static void main(String[] args) {
2
3double a = 1;
4double b = 2;
5      System.out.println(Math.max(a,b));  //2.0
6      System.out.println(Math.min(a,b));  //1.0
7      }
java Math.pow( $  ,  $ ) - 计算次⽅
java Math.pow()⽅法⽤于返回第⼀个参数的第⼆个参数次⽅。
printf输出格式java
public static void main(String args[]){
double x = 11.635;
double y = 2.76;
System.out.printf("e 的值为 %.4f%n", Math.E);
System.out.printf("pow(%.3f, %.3f) 为 %.3f%n", x, y, Math.pow(x, y));
}
/*输出结果:
e 的值为 2.7183
pow(11.635, 2.760) 为 874.008
*/
java Math.cbrt(  ) - 开⽴⽅、Math.sqrt()- 开平⽅
返回double类型
System.out.println(Math.sqrt(4.0));//输出2.0
System.out.println(Math.cbrt(8.0));//输出2.0
Java Math.log(  ) 计算对数
Math.log() ⽅法⽤于返回参数的⾃然数底数的对数值。
1public static void main(String args[]){
2double x =Math.E;
3double y = 2.76;
5 System.out.printf("e 的值为 %.4f%n", Math.E);
6 System.out.printf("log(%.3f) 为 %.3f%n", x, Math.log(x));
7 }
输出结果为:
e 的值为 2.7183
log(e) 为 1.000 //log(e)=lne=1.0
und(  )  - 四舍五⼊
und() ⽅法返回⼀个最接近的int、long型值。
1public static void main(String args[]){
2double d = 100.675;
3double e = 100.500;
4float f = 100;
5float g = 90f;
6
7        System.out.und(d)); //101
8        System.out.und(e)); //101
9        System.out.und(f)); //100
10        System.out.und(g)); //90
11    }
Java Math.rint(  ) - 就近取整
返回与参数最接近的整数。返回类型为double。
1 public static void main(String args[]){
2        double d = 10.675;
3        double e = 10.500;
4        float f = 100;
5        float g = 9.45;
6
7        System.out.und(d)); //11.0
8        System.out.und(e)); //11.0
9        System.out.und(f)); //100.0
10        System.out.und(g)); //9.0
}
Java Math.floor(  ) - 向下取整
Math.floor() ⽅法可对⼀个数进⾏下舍⼊,直接取整,返回给定参数最⼤的整数,该整数⼩于或等给定的参数。参数可以使double和float ,返回double floor:地板il(  ) - 向上取整
1public static void main(String args[]){
2double d = 100.675;
3float f = -90;
4
5        System.out.println(Math.floor(d));//100.0
6        System.out.println(Math.floor(f));//-90.0
7
8        System.out.il(d));//101.0
9        System.out.il(f));//-90.0
10    }
Java Math.random(  ) - 获取随机数
Java Math.random() ⽅法⽤于返回⼀个随机数,随机数范围为 0.0 =< Math.random < 1.0。返回double类型
1import java.util.Random;
2
3public class RandomTest{
4public static void main(String[] args){
5        Random rand=new Random();
6int i=(int)(Math.random()*100);      //  ⽣成0-100的随机数
7int Int(100);              // 这⾥是⼀个⽅法的重载,参数的内容是指定范围
8        System.out.println("i:"+i+"\nj:"+j); // 分别输出两个随机数
9    }
10 }
⼆、使⽤次数⽐较少的Math类函数
Degrees(  ) - 弧度转换成⾓度
Java中Degrees(  )⽤于将以弧度测量的⾓度转换为以度为单位的近似等效⾓度,也就是将-π/2到π/2之间的弧度值转化为度。如果要将。返回以度为单位的⾓度public class MathtoDegreesDemo{
public static void main(String args[]){
System.out.Degrees(Math.PI/2));
}
}
//输出:90.0
Radians(  ) - ⾓度转换为弧度
java中Radians(  )⽤于将以度为单位的⾓度转换为以弧度测量的⼤致相等的⾓度,也就是说将度转化为-π/2到π/2之间的弧度值。如果要将。
public class MathtoDegreesDemo{
public static void main(String args[]){
System.out.Radians(30));
}
}
//输出:π/6
计算三⾓函数
Java中s()⽤于计算余弦,返回的是 -1.0 到 1.0 之间的数,如果要想将。
Java中Math.acos()⽤于计算反余弦,返回值的单位为弧度,对于[-1,1]之间的元素,函数值域为[0,pi],如果要想将。
Java中Math.sin()⽤于计算正弦,返回的是 -1.0 到 1.0 之间的数,如果要想将。
Java中Math.asin()⽤于计算反正弦,返回值的单位为弧度,返回的⾓度范围在 -pi/2 到 pi/2 之间。如果要将.
Math.tan()⽤于计算正切,Math.tanh() ⽤于计算双曲正切。
java中Math.atan()⽤于计算反正切,返回的⾓度范围在 -pi/2 到 pi/2 之间,如果要。