数学函数

本文详细介绍了Java中的Math类,包括三角函数、指数函数、取整函数、随机数生成、min/max/abs方法的使用。通过具体示例展示了如何在程序中应用这些数学函数。

三角函数方法

  Math 类中三角函数方法:

  1. Math.toDegrees(Math.PI / 2)返回90.0
  2. Math.toRadians(30)返回0.5236(PI / 6)
  3. Math.sin(0)返回0.0
  4. Math.sin(Math.toRadians(270)返回-1.0
  5. Math.sin(Math.PI / 6)返回0.5
  6. Math.sin(Math.PI/2)返回1.0
  7. Math.cos(0)返回1.0
  8. Math.cos(Math.PI / 6)返回0.866
  9. Math.cos(Math.PI / 2)返回0
  10. Math.asin(0.5)返回0.523598333(PI/ 6)
  11. Math.acos(0.5)返回1.0472(PI/3)
  12. Math.atan(1.0)返回0.785398(PI/)

  指数函数:

  1. exp(x);
  2. log(x);
  3. log10(x);
  4. pow(a,b);
  5. sqrt(x);

  取整函数(返回都是双精度):

  1. 向上取整  ceil(x)     Math.ceil(2.1)返回3.0    Math.ceil(-2.1)返回-2.0
  2. 向下取整       floor(x)   Math.floor(2.1)返回2.0   Math.floor(-2.1)返回-3.0
  3. 取整为它最接近的整数。如果x与两个整数相等,偶数的整数作为返回值  

    Math.rint(2.1)返回2.0  Math.rint(-2.1)返回-2.0  Math.rint(2.5)返回2.0  Math.rint(2.6)返回3.0  Math.rint(1.5)返回2.0

  4.  round(x)  如果x是单精度数,返回(int)Math.floor(x+0.5);如果x是双精度数,返回(long)Math.floor(x+0.5)

    Math.round(2.6f)返回3  Math.round(2.0)返回2  Math.round(-2.4)返回-2

  min、max和abs方法:

  1. min和max方法用于返回两个数(int、long、float或double型)的最小值和最大值。
  2. abs方法以返回一个数(int、long、float或double型)的绝对值。

    Math.max(2, 3)  return 3;  Math.min(2.5, 4.6)  return 2.5;  

    Math.max(Math.max(2.5, 4.6), Math.min(3, 5.6))  return 4.6;

    Math.abs(-2)  return 2;

  random方法:

  1. a+Math.random() * b  返回a~a+b之间的一个随机数,不包括a+b

 

转载于:https://www.cnblogs.com/King-boy/p/10498558.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值