Java 在 Math 类中提供了许多实用的方法,来计算常用的数学函数。 这些方法分为三类:三角函数方法(trigonometric method)、指教函数方法(exponent method) 和服务方法(service method)。服务方法包括取整、求最小值、求最大值、求绝对值和随机方法。除了这些方法之外,Math 类还提供了两 个很有用的 double 型常量,PI ( TT ) 和 E (自然对数的底)。可以在任意程序中用Math.PI 和 Math.E 的形式来使用这两个常量。
三角函数方法
Math 类包含的三角函数方法如表4-1所示。
方法 | 描述 |
sin(radians) | 返回以弧度为单位的角度的三角正弦函数值 |
cos(radians) | 返回以弧度为单位的角度的三角余弦函数值 |
tan(radians) | 返回以弧度为单位的角度的三角正切函数值 |
toRadians(degree) | 将以度为单位的角度值转换为以弧度表示 |
toDegrees(radians) | 将以弧度为单位的角度值转换为以度表示 |
asin(a) | 返回以弧度为单位的角度的反三角正弦函数值 |
acos(a) | 返回以弧度为单位的角度的反三角余弦函数值 |
atan(a) | 返回以弧度为单位的角度的反三角正切函数值 |
sin、cos 和 tan 的参数都是以弧度为单位的角度。asin 和 atan的返回值是-TT /2 TT /2 的一个弧度值,acos 的返回值在 0到之间。 1°相当于 TT /180 弧度,90°相当于 TT /2 弧度, 而 30°相当于 TT /6 弧度。
class Demo4_1
{
public static void main(String[] args)
{
System.out.println(Math.sin(Math.PI/6));
System.out.println(Math.cos(Math.PI/6));
System.out.println(Math.tan(Math.PI/6));
System.out.println(Math.asin(0.5));
System.out.println(Math.acos(0.6));
System.out.println(Math.atan(0.6));
System.out.println(Math.toDegrees(Math.PI/6));
System.out.println(Math.toRadians(0.6));
}
}
指数函数方法
Math 类中有 5 个与指数函数有关的方法,如表 4-2 所示。
方法 | 描述 |
exp(x) | 返回e的 x 次方 |
log(x) | 返回 X 的自然底数,即lna |
logl0(x) | 返回 x 的以 10 为底的对数 |
pow(a, b) | 返回 a的b 次方 |
sqrt(x) | 对于 x >=0的数字,返回 x 的平方根 |
class Demo4_2
{
public static void main(String[] args)
{
System.out.println("e的平方数值为:"+Math.exp(2));
System.out.println("4的自然底数为:"+Math.log(4));
System.out.println("返回8的以10为底的对数"+Math.log10(8));
System.out.println("2的5次方为:"+Math.pow(2,5));
System.out.println("4的平方根为:"+Math.sqrt(4));
}
}
取整方法
Math 类包括五个取整方法,如表 4-3 所示。
方法 | 描述 |
ceil(x) | 返回大于等于a的整数值,返回值类型为double; |
floor(x) | 返回小于等于a的整数值,返回值类型为double; |
rint(x) | 返回与a最接近的整数值,返回值类型为double;(如果两个同为整数且同样接近,选取偶数值的那个) |
random() | 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0 |
round(x) | 如果 X 是单精度数,返回(int) Malh.floor(x+0.5) |
如果 x 是双精度数,返回(long) Math. floor(x-K).5) |
class Dome4_3
{
public static void main(String[] args)
{
System.out.println("随机数为:"+Math.random());
System.out.println("整数为:"+Math.ceil(4.1));
System.out.println("整数为"+Math.floor(8.3));
System.out.println("整数为:"+Math.rint(2.5));
System.out.println("整数为:"+Math.round(4.8));
}
}
min、max 和 abs 方法
方法 | 描述 | 例子 |
min | 用于返回两个数(int、long、float 或 double 型)的最小值 | max(4.4,5.0) 返回 5.0 |
max | 用于返回两个数(int、long、float 或 double 型)的最大值 | min(3,2) 返回 2 |
abs | 用于返回一个数(int、long、float 或 double 型)的绝对值。 | abs(-3.2) 返回3.2 |
class Demo4_4
{
public static void main(String[] args)
{
System.out.println("最大值为:"+Math.max(7.8,4.9));
System.out.println("最小值为:"+Math.min(4.1,3.2));
System.out.println("绝对值为:"+Math.abs(-8.3));
}
}
random 方法
random()方 法:生 成 大 于 等 于 0.0 且 小 于 1.0的 double 型 随 机 数 (0.0<=Math.random()<1.0)。可以使用它编写简单的表达式,生成任意范围的随机数。例如:
- (int)(Math, randomf) * 10) //返回0~9之间的一个随机整数
- 50 + (int)(Math.random() * 50) //返回 50~99之间的一个随机整数
- a + Math.random() * b //返回a~a+b之间的一个 随机整数,不包括 a+b
import java.util.ArrayList;
import java.util.Random;
public class TestRandom {
public static void main(String[] args) {
// 案例2
// 对于种子相同的Random对象,生成的随机数序列是一样的。
Random ran1 = new Random(10);
System.out.println("使用种子为10的Random对象生成[0,10)内随机整数序列: ");
for (int i = 0; i < 10; i++) {
System.out.print(ran1.nextInt(10) + " ");
}
System.out.println();
Random ran2 = new Random(10);
System.out.println("使用另一个种子为10的Random对象生成[0,10)内随机整数序列: ");
for (int i = 0; i < 10; i++) {
System.out.print(ran2.nextInt(10) + " ");
}
}
}