java.lang.Math类中定义了一些完成基本算术运算的函数,如指数函数、对数函数、平方根函数以及三角函数等。Math类是final类,因此不能被继承。它的构造方法是私有的,因此不能实例化。Math类中定义的两个常量PI和E以及所有的方法都是静态的,因此在使用时不需要创建Math类的对象,而直接使用类名作前缀,即可很方便地调用这些属性和方法。
(1)Math类定义的两个双精度常量如下
public final static double E;//数学常量
publie final static double PI;//圆周率常量
(2)Math类定义的常用方法
方法 | 说明 |
static double sin(double a) static double con(double a) static double tan(double a) |
返回角度a的正弦、余弦和正切值,其中a的单位为弧度 |
static double asin(double a) static double acos(double a) static double atan(double a) static double atan2(double y,double x) |
返回角度a的反正弦,反余弦和反正切值,其中a的单位为弧度 |
static double abs(double a) | 返回a的绝对值,另有3个重载的版本 |
static double ceil(double a) static double floor(double a) |
向上取整 向下取整 |
static double exp(double a) static double log(double a) static double sqrt(double a) static double pow(double a,double b) |
返回e的a次方 返回以e为底的自然对数 返回a的平方根 返回a的b次方 |
static double max(double a,double b) static double min(double a,double b) |
返回最大值/最小值,另有参数为float,long,int的重载版本 |
static double random() | 返回[0,1)之间的随机数 |
static int rint(double a) | 返回最接近a的整数值 |
static int round(float a) | 返回最接近参数a的int型值 |
static long round(double a) | 返回最接近参数a的long型值 |
static double IEEEremainder(double f1,double f2) | 计算f1除以f2的余数,符合IEEE754标准规定 |
static double toDegrees(double angrad) | 将弧度转换为角度 |
static double toRadius(double angrad) | 将角度转换为弧度 |