资讯专栏INFORMATION COLUMN

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

antyiwei / 2214人阅读

超越基本算术

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

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

Math.cos(angle);

使用静态导入语言功能,你不必在每个数学函数前面写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返回。
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说明了如何使用其中一些方法:

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

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72888.html

相关文章

  • Java教程(字符)

    字符 大多数情况下,如果使用单个字符值,则将使用原始char类型,例如: char ch = a; // Unicode for uppercase Greek omega character char uniChar = u03A9; // an array of chars char[] charArray = { a, b, c, d, e }; 但是,有时候需要使用字符作为对象 — 例如...

    lscho 评论0 收藏0
  • Java教程(目录)

    Java™ 教程 Java教程是为JDK 8编写的,本页面中描述的示例和实践没有利用在后续版本中引入的改进。 Java教程是希望使用Java编程语言创建应用程序的程序员的实用指南,其中包括数百个完整的工作示例和数十个课程,相关课程组被组织成教程。 覆盖基础知识的路径 这些教程以书籍的形式提供,如Java教程,第六版,前往Amazon.com购买。 入门 介绍Java技术和安装Java开发软件并使用...

    lifesimple 评论0 收藏0
  • Java教程(格式化数字打印输出)

    格式化数字打印输出 之前你已经看到使用print和println方法将字符串打印到标准输出(System.out),由于所有数字都可以转换为字符串(你将在本课后面看到),你可以使用这些方法打印出任意的字符串和数字混合,但是,Java编程语言还有其他方法,可以在包含数字时对打印输出进行更多控制。 printf和format方法 java.io包中包含一个PrintStream类,它有两种格式化方法可...

    rubyshen 评论0 收藏0
  • Java 8 API 示例:字符串、数值、算术和文件

    摘要:示例字符串数值算术和文件原文译者飞龙协议大量的教程和文章都涉及到中最重要的改变,例如表达式和函数式数据流。不仅仅是字符串,正则表达式模式串也能受益于数据流。 Java 8 API 示例:字符串、数值、算术和文件 原文:Java 8 API by Example: Strings, Numbers, Math and Files 译者:飞龙 协议:CC BY-NC-SA 4.0 ...

    KavenFan 评论0 收藏0
  • Java教程(运算符)

    运算符 既然你已经学会了如何声明和初始化变量,那么你可能想知道如何使用它们,学习Java编程语言的运算符是一个很好的起点,运算符是对一个、两个或三个操作数执行特定运算的特殊符号,然后返回结果。 在我们探索Java编程语言的运算符时,提前知道哪些运算符具有最高优先级可能会对你有所帮助,下表中的运算符按优先顺序列出,运算符出现在离表顶部越近,其优先级越高,优先级较高的运算符在优先级相对较低的运算符之前...

    taowen 评论0 收藏0

发表评论

0条评论

antyiwei

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<