14.10.2 指数函数
Math定义了下面的指数方法:
方法 描述
static double exp(double arg) 返回arg的e
static double log(double arg) 返回arg的自然对数值
static double pow(double y, double x) 返回以y为底数,以x为指数的幂值;例如pow(2.0, 3.0)返回8.0
static double sqrt(double arg) 返回arg的平方根
14.10.3 舍入函数
Math类定义了几个提供不同类型舍入运算的方法。这些方法列在表14-15中。
表14-15 由Math 定义的舍入方法
方法 描述
static int abs(int arg) 返回arg的绝对值
static long abs(long arg) 返回arg的绝对值
static float abs(float arg) 返回arg的绝对值
static double abs(double arg) 返回arg的绝对值
static double ceil(double arg) 返回大于或等于arg的最小整数
static double floor(double arg) 返回小于或等于arg的最大整数
static int max(int x, int y) 返回x和y中的最大值
static long max(long x, long y) 返回x和y中的最大值
static float max(float x, float y) 返回x和y中的最大值
static double max(double x, double y) 返回x和y中的最大值
static int min(int x, int y) 返回x和y中的最小值
static long min(long x, long y) 返回x和y中的最小值
static float min(float x, float y) 返回x和y中的最小值
static double min(double x, double y) 返回x和y中的最小值
static double rint(double arg) 返回最接近arg的整数值
static int round(float arg) 返回arg的只入不舍的最近的整型(int)值
static long round(double arg) 返回arg的只入不舍的最近的长整型(long)值
14.10.4 其他的数学方法
除了给出的方法,Math还定义了下面这些方法:
static double IEEEremainder(double dividend, double divisor)
static double random( )
static double toRadians(double angle)
static double toDegrees(double angle)
IEEEremainder( )方法返回dividend/divisor的余数。random( )方法返回一个伪随机数,其值介于0与1之间。在大多数情况下,当需要产生随机数时,通常用Random类。toRadians( )方法将角度的度转换为弧度。而toDegrees( )方法将弧度转换为度。这后两种方法是在Java 2中新增加的。
下面是一个说明toRadians( )和toDegrees( )方法的例子:
// Demonstrate toDegrees() and toRadians().
class Angles {
public static void main(String args[]) {
double theta = 120.0;
System.out.println(theta + " degrees is " +
Math.toRadians(theta) + " radians.");
theta = 1.312;
System.out.println(theta + " radians is " +
Math.toDegrees(theta) + " degrees.");
}
}
程序输出如下所示:
120.0 degrees is 2.0943951023931953 radians.