超越基本算术

Java 编程语言通过其算术运算符(,-,*,/和%)支持基本算术。 java.lang包中的Math类提供了用于执行更高级的 math 计算的方法和常量。

Math类中的方法都是静态的,因此您可以直接从类中调用它们,如下所示:

Math.cos(angle);

Note:

使用static import语言功能,您不必在每个 math 函数前面都写Math

import static java.lang.Math.*;

这使您可以通过它们的简单名称来调用Math类方法。例如:

cos(angle);

常数和基本方法

Math类包含两个常量:

  • Math.E,它是自然对数的底数,并且

  • Math.PI,它是圆的周 Long 与其直径之比。

Math类还包括 40 多个静态方法。下表列出了许多基本方法。

基本 math 方法

MethodDescription
double abs(double d) float abs(float f) int abs(int i) long abs(long lng)返回参数的绝对值。
double ceil(double d)返回大于或等于参数的最小整数。返回 Double。
double floor(double d)返回小于或等于参数的最大整数。返回 Double。
double rint(double d)返回值最接近参数的整数。返回 Double。
long round(double d) int round(float f)返回与参数最接近的 long 或 int(由方法的返回类型指示)。
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类的指数和对数方法。

指数和对数方法

MethodDescription
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

Trigonometric Methods

Math类还提供了三角函数的集合,下表中对此进行了概述。传递给每种方法的值都是以弧度表示的角度。您可以使用toRadians方法将度数转换为弧度。

Trigonometric Methods

MethodDescription
double sin(double d)返回指定 Double 精度值的正弦值。
double cos(double d)返回指定 Double 精度值的余弦值。
double tan(double d)返回指定 Double 精度值的切线。
double asin(double d)返回指定 Double 精度值的反正弦值。
double acos(double d)返回指定 Double 精度值的反余弦值。
double atan(double d)返回指定 Double 精度值的反正切值。
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 Numbers

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的实例并对该对象调用方法以生成数字。