栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java常用API--Math类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java常用API--Math类

Math类常用API详解

Math类中全部是静态方法,都可以用ClassName.funName()

字段
double E   自然对数e
double PI  圆周率
三角函数 cos()余弦
参数是弧度。 弧度= 2 Math.PI /360 * 度数    比如:60度 = 2 * Math.PI / 360 * 60
cos(60度)  Math.cos( 1.0/3 * Math.PI) = 0.5
sin()正弦
sin(30度) Math.sin(1.0 / 6 * Math.PI) = 0.5 
tan()正切
tan(45度) Math.tan(1.0 /4 * Math.PI) = 1
acos()反余弦
acos(0.5) = Math.acos(0.5) / Math.PI * 180 = 60度
asin()反正弦
asin(0.5) = Math.asin(0.5) / Math.PI * 180 = 30度
atan()反正切
atan(1) = Math.atan(1.0) / Math.PI * 180 = 45度
角度转换 toDegrees()将弧度转为角度
 System.out.println(Math.toDegrees(Math.PI)); //180
toRadians()将角度转弧度
System.out.println(Math.toRadians(180)); //3.141592653589793
求幂,开方,指数,对数 hypot(x,y) 返回x平方+y平方再求开方
经典的勾三股四弦五
System.out.println(Math.hypot(3, 4)); //5.0  sqrt(3^2 + 4^2)
cbrt(x)返回x的立方根
System.out.println(Math.cbrt(27)); //3.0
sprt(x)返回x的平方根
 System.out.println(Math.sqrt(4)); //2.0
scalb(x,y)返回 x * 2^y
System.out.println(Math.scalb(1.344, 2)); // 1.344 * 2^2 = 5.376
exp(x) 自然数e的x次方
 System.out.println(Math.exp(2.0)); //7.38905609893065  e^2
pow(x,y)返回x的y次幂
System.out.println(Math.pow(2, 3)); //8.0
expm1()自然数e的x次方减1
System.out.println(Math.expm1(2.0)); //6.38905609893065
log1p(x) 求x + 1 以e为底的对数
System.out.println(Math.log1p(1.718281828459045)); //1.0
log(x)求x以e为底的对数
 System.out.println(Math.log(2.718281828459045));
log10(x)求x以10为底的对数
System.out.println(Math.log10(10)); //1.0
getExponent(x)返回以2为底x的指数
System.out.println(Math.getExponent(1024)); //10
取整、取模四舍五入 ceil()向上取整
   System.out.println(Math.ceil(3.6)); //4.0
   System.out.println(Math.ceil(3.4)); //4.0
floor()向下取整
System.out.println(Math.floor(3.5)); //3.0
floor(x,y) x除于y的值再向下取整
System.out.println(Math.floorDiv(10, 4)); //2
floorMod(x,y)返回x对y取模
   System.out.println(Math.floorMod(4, 2)); //0
   System.out.println(Math.floorMod(3, 2));  //1
rint()向靠近的数取整
  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
round() 四舍五入(负数有些和我们平常的不一样)
  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
返回加工后的数 abs()取绝对值
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
copySing(x,y)用y的符号返回第一个数的值
    System.out.println(Math.copySign(0.5, -1.0)); //-0.5
    System.out.println(Math.copySign(0.5f, 1.0f)); //0.5
random()返回一个0-1之间的随机小数
 for (int i = 0; i < 10; i++) {
     System.out.println(Math.random());
 }
//        0.9072383311558234
//        0.7659940574619751
//        0.7978623317262958
//        0.38447672747439443
//		  ...
signum返回大于0则为1,等于0为0,小于0为-1
   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

toIntExact()将Long类型截取最大的Int类型
System.out.println(Math.toIntExact(32134234312312321L));//321342343
nextAfter(x,y)返回x以y方向最相近的数
  -------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
nextDown()、nextUp()返回以负无穷/正无穷大方向的最相近的数
   ----负无穷<----1.2233---
   ----1.2233---->正无穷---     
System.out.println(Math.nextDown(1.2233)); //1.2232999999999998
System.out.println(Math.nextUp(1.2233));  //1.2233000000000003
返回一个或两个数之间的操作 decrementExact()返回一个减1的数超过返回则会报错
        System.out.println(Math.decrementExact(-1));
        System.out.println(Math.decrementExact(-2147483648)); //报异常
incrementExact()返回一个增1的数超过返回则会报错
System.out.println(Math.incrementExact(2147483647)); //报异常
multiplyExact(x,y)返回x*y超出范围则报异常
      System.out.println(Math.multiplyExact(3, 4)); //12
      System.out.println(Math.multiplyExact(3, 2147483647)); //报错
subtractExact(x,y)返回x-y,超出范围报错
     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
addExact(x,y)返回两数之和,超过范围则会报相应的错误
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
negateExact(x)对x取反超出范围则报错
System.out.println(Math.negateExact(2147483647)); //-2147483647
System.out.println(Math.negateExact(-2147483647)); //2147483647
System.out.println(Math.negateExact(-2147483648)); //报错
最小最大值 max(x,y)求两数中最大的数
  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
min(x,y)求两数中最小的数
  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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/888412.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号