Java编程语言通过其算术运算符支持基本算术:+,-,*,/和%。java.lang
包中的 Math
类提供了用于执行更高级的数学计算的方法和常量。Math
类中的方法都是静态的,因此您可以直接从类中调用它们,如下所示:
Math.cos(angle);
注意: 使用 static import
语言功能,您不必在每个数学函数之前都写Math
:
import static java.lang.Math.*;
这使您可以通过Math
类方法的简单名称来调用它们。例如:
cos(angle);
常量和基本方法
Math
类包括两个常量:
Math.E
,这是自然对数的基础,和Math.PI
,它是圆的周长与其直径之比。
Math
类还包括超过40种静态方法。下表列出了许多基本方法。
基本数学方法
方法 | 描述 |
---|---|
`double abs(double d) |
float abs(float f)
int abs(int i)
long abs(long lng)| 返回参数的绝对值。 |
|
double ceil(double d)| 返回大于或等于参数的最小整数。返回双精度浮点数。 |
|
double floor(double d)| 返回小于或等于参数的最大整数。返回双精度浮点数。 |
|
double rint(double d)| 返回值最接近参数的整数。返回双精度浮点数。 |
|
long round(double d)
int round(float f)| 返回与参数最接近的长整型或整型(由方法的返回类型指示)。 |
|
double min(double arg1, double arg2)
float min(float arg1, float arg2)
int min(int arg1, int arg2)
long min(long arg1, long arg2)| 返回两个参数中较小的一个。 |
|
double max(double arg1, double arg2)
float max(float arg1, float arg2)
int max(int arg1, int arg2)
long max(long arg1, long arg2)` | 返回两个参数中较大的一个。 |
以下程序, BasicMathDemo
说明了如何使用其中一些方法:
public class BasicMathDemo {
public static void main(String[] args) {
double a = -191.635;
double b = 43.74;
int c = 16, d = 45;
System.out.printf("The absolute value " + "of %.3f is %.3f%n",
a, Math.abs(a));
System.out.printf("The ceiling of " + "%.2f is %.0f%n",
b, Math.ceil(b));
System.out.printf("The floor of " + "%.2f is %.0f%n",
b, Math.floor(b));
System.out.printf("The rint of %.2f " + "is %.0f%n",
b, Math.rint(b));
System.out.printf("The max of %d and " + "%d is %d%n",
c, d, Math.max(c, d));
System.out.printf("The min of of %d " + "and %d is %d%n",
c, d, Math.min(c, d));
}
}
这是该程序的输出:
The absolute value of -191.635 is 191.635
The ceiling of 43.74 is 44
The floor of 43.74 is 43
The rint of 43.74 is 44
The max of 16 and 45 is 45
The min of 16 and 45 is 16
指数和对数方法
下表列出了Math
类的指数和对数方法。
指数和对数方法
方法 | 描述 |
---|---|
double exp(double d) |
返回以自然对数e为底数,参数为指数的幂。 |
double log(double d) |
返回参数的自然对数。 |
double pow(double base, double exponent) |
返回以第一个参数为底数,第二个参数的为指数的幂。 |
double sqrt(double d) |
返回参数的平方根。 |
下面的程序 ExponentialDemo
显示e
的值,然后对任意选择的数字调用上表中列出的每个方法:
public class ExponentialDemo {
public static void main(String[] args) {
double x = 11.635;
double y = 2.76;
System.out.printf("The value of " + "e is %.4f%n",
Math.E);
System.out.printf("exp(%.3f) " + "is %.3f%n",
x, Math.exp(x));
System.out.printf("log(%.3f) is " + "%.3f%n",
x, Math.log(x));
System.out.printf("pow(%.3f, %.3f) " + "is %.3f%n",
x, y, Math.pow(x, y));
System.out.printf("sqrt(%.3f) is " + "%.3f%n",
x, Math.sqrt(x));
}
}
这是运行ExponentialDemo
时将看到的输出:
The value of e is 2.7183
exp(11.635) is 112983.831
log(11.635) is 2.454
pow(11.635, 2.760) is 874.008
sqrt(11.635) is 3.411
三角函数方法
Math
类还提供了三角函数,总结到下表中。传递到这些方法中的每个值都是以弧度表示的角度。您可以使用toRadians
方法将度数转换为弧度。
三角方法
方法 | 描述 |
---|---|
double sin(double d) |
返回指定双精度值的正弦值。 |
double cos(double d) |
返回指定双精度值的余弦值。 |
double tan(double d) |
返回指定双精度值的正切值。 |
double asin(double d) |
返回指定双精度值的反正弦值。 |
double acos(double d) |
返回指定双精度值的反余弦值。 |
double atan(double d) |
返回指定双精度值的反正切值。 |
double atan2(double y, double x) |
将直角坐标转换(x, y) 为极坐标(r, theta) 并返回theta 。 |
`double toDegrees(double d) |
double toRadians(double d)` | 将参数转换为度或弧度。 |
这是一个程序TrigonometricDemo
,它使用以下每种方法来计算45度角的各种三角值:
public class TrigonometricDemo {
public static void main(String[] args) {
double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.format("The value of pi " + "is %.4f%n",
Math.PI);
System.out.format("The sine of %.1f " + "degrees is %.4f%n",
degrees, Math.sin(radians));
System.out.format("The cosine of %.1f " + "degrees is %.4f%n",
degrees, Math.cos(radians));
System.out.format("The tangent of %.1f " + "degrees is %.4f%n",
degrees, Math.tan(radians));
System.out.format("The arcsine of %.4f " + "is %.4f degrees %n",
Math.sin(radians),
Math.toDegrees(Math.asin(Math.sin(radians))));
System.out.format("The arccosine of %.4f " + "is %.4f degrees %n",
Math.cos(radians),
Math.toDegrees(Math.acos(Math.cos(radians))));
System.out.format("The arctangent of %.4f " + "is %.4f degrees %n",
Math.tan(radians),
Math.toDegrees(Math.atan(Math.tan(radians))));
}
}
该程序的输出如下:
The value of pi is 3.1416
The sine of 45.0 degrees is 0.7071
The cosine of 45.0 degrees is 0.7071
The tangent of 45.0 degrees is 1.0000
The arcsine of 0.7071 is 45.0000 degrees
The arccosine of 0.7071 is 45.0000 degrees
The arctangent of 1.0000 is 45.0000 degrees
随机数
random()
方法返回一个介于0.0和1.0之间的伪随机选择的数字。范围包括0.0,但不包括1.0。换句话说:0.0 <= Math.random() < 1.0
。要获得不同范围内的数字,可以对random方法返回的值进行算术运算。例如,要生成0到9之间的整数,应编写:
int number = (int)(Math.random() * 10);
通过将该值乘以10,可能值的范围变为0.0 <= number < 10.0
。
当您需要生成一个随机数时,使用Math.random
效果很好。如果需要生成一系列随机数,则应创建java.util.Random
的实例,并对该对象调用方法以生成数字。