Math类中全部是静态方法,都可以用ClassName.funName()
字段
double E 自然对数e
double PI 圆周率
参数是弧度。 弧度= 2 Math.PI /360 * 度数 比如:60度 = 2 * Math.PI / 360 * 60
cos(60度) Math.cos( 1.0/3 * Math.PI) = 0.5
sin(30度) Math.sin(1.0 / 6 * Math.PI) = 0.5
tan(45度) Math.tan(1.0 /4 * Math.PI) = 1
acos(0.5) = Math.acos(0.5) / Math.PI * 180 = 60度
asin(0.5) = Math.asin(0.5) / Math.PI * 180 = 30度
atan(1) = Math.atan(1.0) / Math.PI * 180 = 45度
System.out.println(Math.toDegrees(Math.PI)); //180
System.out.println(Math.toRadians(180)); //3.141592653589793
经典的勾三股四弦五
System.out.println(Math.hypot(3, 4)); //5.0 sqrt(3^2 + 4^2)
System.out.println(Math.cbrt(27)); //3.0
System.out.println(Math.sqrt(4)); //2.0
System.out.println(Math.scalb(1.344, 2)); // 1.344 * 2^2 = 5.376
System.out.println(Math.exp(2.0)); //7.38905609893065 e^2
System.out.println(Math.pow(2, 3)); //8.0
System.out.println(Math.expm1(2.0)); //6.38905609893065
System.out.println(Math.log1p(1.718281828459045)); //1.0
System.out.println(Math.log(2.718281828459045));
System.out.println(Math.log10(10)); //1.0
System.out.println(Math.getExponent(1024)); //10
System.out.println(Math.ceil(3.6)); //4.0
System.out.println(Math.ceil(3.4)); //4.0
System.out.println(Math.floor(3.5)); //3.0
System.out.println(Math.floorDiv(10, 4)); //2
System.out.println(Math.floorMod(4, 2)); //0
System.out.println(Math.floorMod(3, 2)); //1
System.out.println(Math.rint(2.5)); //2.0
System.out.println(Math.rint(2.4)); //2.0
System.out.println(Math.rint(2.6)); //3.0
System.out.println(Math.rint(-2.6)); //-3.0
System.out.println(Math.rint(-2.5)); //-2.0
System.out.println(Math.rint(-2.4)); //-2.0
System.out.println(Math.round(2.6)); //3
System.out.println(Math.round(2.5)); //3
System.out.println(Math.round(2.4)); //2
System.out.println(Math.round(-2.6)); //-3
System.out.println(Math.round(-2.5)); //-2
System.out.println(Math.round(-2.4)); //-2
System.out.println(Math.abs(-1)); // 1
System.out.println(Math.abs(-1.1F)); // 1.1
System.out.println(Math.abs(-1L)); // 1
System.out.println(Math.abs(-1.0)); // 1.0
System.out.println(Math.copySign(0.5, -1.0)); //-0.5
System.out.println(Math.copySign(0.5f, 1.0f)); //0.5
for (int i = 0; i < 10; i++) {
System.out.println(Math.random());
}
// 0.9072383311558234
// 0.7659940574619751
// 0.7978623317262958
// 0.38447672747439443
// ...
System.out.println(Math.signum(2.0)); //1.0
System.out.println(Math.signum(0.5)); //1.0
System.out.println(Math.signum(0)); // 0.0
System.out.println(Math.signum(-1)); // -1.0
System.out.println(Math.toIntExact(32134234312312321L));//321342343
-------2.5---->3.6---
-----3.6<------4.5---
System.out.println(Math.nextAfter(2.5, 3.6)); //2.5000000000000004
System.out.println(Math.nextAfter(4.5, 3.6)); //4.499999999999999
----负无穷<----1.2233---
----1.2233---->正无穷---
System.out.println(Math.nextDown(1.2233)); //1.2232999999999998
System.out.println(Math.nextUp(1.2233)); //1.2233000000000003
System.out.println(Math.decrementExact(-1));
System.out.println(Math.decrementExact(-2147483648)); //报异常
System.out.println(Math.incrementExact(2147483647)); //报异常
System.out.println(Math.multiplyExact(3, 4)); //12
System.out.println(Math.multiplyExact(3, 2147483647)); //报错
System.out.println(Math.subtractExact(3, 4)); //-1
System.out.println(Math.subtractExact(3, 3)); //0
System.out.println(Math.subtractExact(3, 2)); //1
System.out.println(Math.subtractExact(-2, 2147483647)); //报异常 ArithmeticException
System.out.println(Math.addExact(2147483647, 23));
//Exception in thread "main" java.lang.ArithmeticException: integer overflow
System.out.println(Math.addExact(9223372036854775807L, 1L));
//Exception in thread "main" java.lang.ArithmeticException: long overflow
System.out.println(Math.negateExact(2147483647)); //-2147483647
System.out.println(Math.negateExact(-2147483647)); //2147483647
System.out.println(Math.negateExact(-2147483648)); //报错
System.out.println(Math.max(3, 4)); //4
System.out.println(Math.max(-3, -4)); //-3
System.out.println(Math.max(Math.abs(-3), 4)); //4
System.out.println(Math.min(3, 4)); //3
System.out.println(Math.min(-3, -4)); //-4
System.out.println(Math.min(Math.abs(-3), 4)); //3