Math

运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Created by KebabShell
* on 2020/3/28 上午 10:41
*/
public class MathTest {
@Test
public void test(){
double result = Math.sqrt(4);
System.out.println("sqrt result:" + result);//开方
result = Math.pow(4, 2);
System.out.println("pow result:" + result);//4的2次方
int i = Math.floorMod(5, 2);//5 % 2
System.out.println("i:" + i);
long round = Math.round(1.5);
System.out.println("round:" + round);//四舍五入

System.out.println("up:" + Math.ceil(4.000000001));//向上取整
System.out.println("floor:" + Math.floor(4.99999));//向下取整
}
@Test
public void leftMove(){
int result = 3 << 2;
System.out.println("<<:" + result);//左移一位都相当于乘以2的1次方,左移n位就相当于乘以2的n次方
}
@Test
public void rightMove(){
int result = 12 >> 2;
System.out.println(">>:" + result);//右移n位相当于除以2的n次方
}
}

大数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Created by KebabShell
* on 2020/3/29 下午 04:50
*/
public class BigDecimalTest {
@Test
public void test(){
//浮点型直接运算会失真
System.out.println(1.1 + 2.2);

double a = 1.1;
double b = 2.2;

//不要new
BigDecimal bigDecimal = BigDecimal.valueOf(a);
BigDecimal bigDecimal1 = BigDecimal.valueOf(b);

BigDecimal add = bigDecimal.add(bigDecimal1);
System.out.println(add);

// bigDecimal.divide() 除法
// bigDecimal.subtract() 减法
// bigDecimal.multiply() 乘法

//最终拿到double的数据
double value = bigDecimal.doubleValue();


int compare = Double.compare(1.1, 6.3);//比较
}
}