Math
类
基本介绍:Math
类包含用于执行基本数学运算的方法,如初等指数,对数,平方根和三角函数,并且Math
所有的方法都是静态的。
Math
类常见方法
abs 绝对值
pow 求幂
ceil 向上取整
floor 向下取整
round 四舍五入
sqrt 求开方
max 求两个数的最大值
min 求两个数的最小值
random 求随机数
请写出获取 a - b之间的一个随机整数 , a,b均为整数
int num = (int)(a + Math.random()*(b - a + 1));
Math
类常见方法源码
绝对值
public static int abs(int a) {
return (a < 0) ? -a : a;
}
求幂
public static double pow(double a, double b) {
return StrictMath.pow(a, b); // default impl. delegates to StrictMath
}
向上取整
public static double ceil(double a) {
return StrictMath.ceil(a); // default impl. delegates to StrictMath
}
向下取整
public static double floor(double a) {
return StrictMath.floor(a); // default impl. delegates to StrictMath
}
四舍五入
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 double sqrt(double a) {
return StrictMath.sqrt(a); // default impl. delegates to StrictMath
// Note that hardware sqrt instructions
// frequently can be directly used by JITs
// and should be much faster than doing
// Math.sqrt in software.
}
最大值
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 double random() {
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
案例代码
package wrapper_.math;
/**
* @author: 海康
* @version: 1.0
*/
public class Math01 {
public static void main(String[] args) {
//绝对值
int abs = Math.abs(-9);
System.out.println(abs);
//求幂
double pow = Math.pow(2, 3);
System.out.println(pow);
//向上取整 ceil[天花板]
// ceil 向上取整 返回 >= 该参数的最大整数
double ceil = Math.ceil(5.3);
System.out.println(ceil);
//向下取整 floor[地板]
// floor 向下取整 返回 <= 该参数的最小整数
double floor = Math.floor(8.6);
System.out.println(floor);
//round 四舍五入
long round = Math.round(7.6);
System.out.println(round);
//sqrt 求开方
double sqrt = Math.sqrt(9.0);
System.out.println(sqrt);
//max 最大值
int max = Math.max(168, 88168);
System.out.println(max);
//min 最小值
double min = Math.min(9.6, 8.8);
System.out.println(min);
/**
* random :求随机数
* random 返回的是 0 <= x <1 之间的一个随机小数
* 思考:请写出获取 a - b 之间的一个随机整数 a b 均为整数,比如: a = 2 b = 7
*/
for (int i = 0; i < 100; i++) {
// double random = Math.floor(Math.random() * 100) + 1;//取 1 - 100 之间的整数 【包含100】
int num = (int) (2 + Math.random() * (7 - 2) + 1); // 取 2 - 7 之间的整数 【包含 2 和 7】
System.out.println(num);
}
}
}