java.Math类方法

java.lang.Math类方法
1.Math.max 求两数中最大/Math.min 求两数中最小
package Test0228;
public class Test007 {public static void main(String[] args) {
double x=12.45,y=52.4; System.out.println(Math.max(x, y)); System.out.println(x<y?x:y);}}
2.Math.round 四舍五入返回int型或者long型
package Test0228;
public class Test008 { public static void main(String[] args) {
double x = 12.56;
double y= 125.69;
double w=12.1; System.out.println(Math.round(x)); System.out.println(Math.round(y)); System.out.println(Math.round(w));
}}
3.Math.abs 求绝对值,典型使用场景就是浮点
数的等值判定问题
package Test0228;
public class Test0001 {public static void main(String[] args) {
double res = 0;
for (int i=0;i<10;i++) {
res+=0.1;
}
if(Math.abs(1-res)<1e-6) { System.out.println(“计算结果为:”);
}else
System.out.println(“结果为:”+res);}}
1e-6,其中e可以大写,也可以小写,这是浮点数中的科学计数法,表示1*10-6
三角函数
Math.sin 正弦函数 Math.asin 反正弦函数Math.cos 余弦函数 Math.acos 反余弦函数Math.tan 正切函数 Math.atan 反正切函数Math.toDegrees 弧度转化为角度 Math.toRadians 角度转化为弧度
指数计算
Math.sqrt 求开方 Math.sqrt(4)Math.pow 求某数的任意次方, 抛出ArithmeticException处理溢出异常Math.exp 求e的任意次方Math.log10 以10为底的对数Math.log 自然对数

发布了33 篇原创文章 · 获赞 27 · 访问量 1368

猜你喜欢

转载自blog.csdn.net/qq_45874107/article/details/104775000