Java™ 教程(超越基本算术)

超越基本算术

Java编程语言支持基本算术及其算术运算符:+-*/java.lang包中的Math类提供了用于执行更高级数学计算的方法和常量。html

Math类中的方法都是静态的,所以你能够直接从类中调用它们,以下所示:java

Math.cos(angle);

使用静态导入语言功能,你没必要在每一个数学函数前面写Mathgit

import static java.lang.Math.*;

这容许你经过简单名称调用Math类方法,例如:github

cos(angle);

常量和基本方法

Math类包含两个常量:编程

  • Math.E,是天然对数的基数。
  • Math.PI,这是圆周率。

Math类还包含40多种静态方法,下表列出了许多基本方法。segmentfault

方法 描述
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)
根据方法的返回类型,返回与参数最近的longint
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说明了如何使用其中一些方法:api

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));
    }
}

这是该程序的输出:oracle

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类的指数和对数方法。dom

方法 描述
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值的正弦值。
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 toDegrees(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。要获取不一样范围内的数字,能够对随机方法返回的值执行算术运算,例如,要生成0到9之间的整数,你能够编写:

int number = (int)(Math.random() * 10);

经过将该值乘以10,可能值的范围变为0.0 <= number < 10.0

当你须要生成单个随机数时,使用Math.random能够很好地工做,若是须要生成一系列随机数,则应建立java.util.Random实例并在该对象上调用方法以生成数字。


上一篇:格式化数字打印输出

下一篇:字符

相关文章
相关标签/搜索