Math 方法
第一组:常用的
方法名 | 描述 |
---|---|
Math.abs() | 返回参数的绝对值 |
Math.ceil() | 返回大于或等于参数的最小整数(向上取整) |
Math.floor() | 返回小于或等于参数的最大整数(向下取整) |
Math.max() | 返回两个参数中较大的值 |
Math.min() | 返回两个参数中较小的值 |
Math.pow(数,开方次数) | 返回第一个参数的第二个参数次方 |
Math.random() | 返回一个 0 到 1 之间的随机数(左闭右开) |
Math.sqrt() | 返回参数的平方根 |
Math.round() | 返回最接近参数的整数(四舍五入,存在精度缺失问题) |
第二组:数学相关
方法名 | 描述 |
---|---|
三角函数 :全部要求传入弧度 参数 |
|
Math.sin() | 返回参数的正弦值 |
Math.cos() | 返回参数的余弦值 |
Math.tan() | 返回参数的正切值 |
Math.asin() | 返回参数值的反正弦值(弧度) |
Math.acos() | 返回参数值的反余弦值(弧度) |
Math.atan() | 返回参数值的反正切值(弧度) |
Math.sinh() | 返回参数的双曲正弦值 |
Math.cosh() | 返回参数的双曲余弦值 |
Math.tanh() | 返回参数的双曲正切值 |
指数和对数 | |
Math.exp() | 返回参数值的指数值 |
Math.expm1() | 返回 e 的 x 次方减去 1 的结果 |
Math.log() | 返回参数的自然对数 |
Math.log10() | 返回参数的以 10 为底的对数 |
如果要计算其他数为底的对数,需要以 log10 为基准,使用换地公式解决 | |
常用计算 | |
Math.abs() | 返回参数的绝对值 |
Math.ceil() | 返回大于或等于参数的最小整数 |
Math.floor() | 返回小于或等于参数的最大整数 |
Math.max() | 返回两个参数中较大的值 |
Math.min() | 返回两个参数中较小的值 |
Math.pow() | 返回第一个参数的第二个参数次方 |
Math.sqrt() | 返回参数的平方根 |
Math.hypot() | 返回直角三角形的斜边长度 |
角度与弧度 | |
Math.toDegrees() | 将弧度转换为角度 |
Math.toRadians() | 将角度转换为弧度 |
使用说明
1. 三角函数部分:要求全部传参为弧度
2. Math.log()
:默认以 e 为底
3. 计算其他数字为底的对数(使用换底公式)
java
public class time_test {
public static void main(String[] args) {
System.out.println(tool.log_x_n(2,2));
}
}
class tool{
public static double log_x_n(int x,int n){
double temp1 = Math.log10(n);
double temp2 = Math.log10(x);
return temp1 / temp2;
}
}
4. randon 练习:生成 2 <= x <= 7
之间的随机数
获取 [a,b] 之间的随机数 --> 公式:a + Math.random*(b - a + 1)
java
for (int i = 0; i < 5; i++) {
System.out.println((int)(2 + Math.random()*(7 - 2 + 1)));
}
// 输出结果
6
7
3
2
5