Java Math 类
最后修改时间:2025 年 4 月 13 日
java.lang.Math 类提供了用于执行各种数学运算的静态方法。这些运算包括指数、对数、三角函数、舍入和随机数函数。由于所有方法都是静态的,因此无法实例化该类。
Math 类适用于像 double 和 int 这样的原始类型。对于需要更高精度的应用程序,请考虑使用 BigDecimal,它为财务计算和科学计算提供更好的精度。
此类还定义了两个重要的常量:Math.PI,数学常量 π (~3.14159),以及 Math.E,自然对数的底数 (~2.71828)。这些常量广泛应用于几何、物理和数值分析中。
Math 类概述
Math 类提供了用于基本算术、三角学、指数运算、对数、舍入和随机数生成的方法。由于所有方法都是静态的和线程安全的,因此无需创建对象即可使用,并且对于并发操作是安全的。
此类属于 java.lang 包,这意味着它会自动导入到所有 Java 程序中,从而无需显式导入语句即可立即访问其功能。
基本算术运算
Math 类提供了用于基本算术运算的方法,如绝对值、最大值/最小值和平方根。这些方法针对性能进行了优化,并能正确处理边缘情况。
package com.zetcode;
public class Main {
public static void main(String[] args) {
// Absolute value
int absValue = Math.abs(-15);
System.out.println("Absolute value of -15: " + absValue);
// Maximum and minimum
int max = Math.max(10, 20);
int min = Math.min(10, 20);
System.out.println("Max of 10 and 20: " + max);
System.out.println("Min of 10 and 20: " + min);
// Square root
double sqrt = Math.sqrt(25);
System.out.println("Square root of 25: " + sqrt);
// Power
double power = Math.pow(2, 3);
System.out.println("2 raised to power 3: " + power);
}
}
此示例演示了基本算术运算。Math.abs 返回绝对值,Math.max/min 比较值,Math.sqrt 计算平方根,Math.pow 处理指数。所有方法都很容易使用。
三角函数
Math 类提供了标准的三角函数,包括正弦、余弦和正切。这些方法以弧度为单位取角度。对于度数转换,请使用 Math.toRadians 或 Math.toDegrees。
package com.zetcode;
public class Main {
public static void main(String[] args) {
// Convert 30 degrees to radians
double radians = Math.toRadians(30);
System.out.println("30 degrees in radians: " + radians);
// Trigonometric functions
double sinValue = Math.sin(radians);
double cosValue = Math.cos(radians);
double tanValue = Math.tan(radians);
System.out.println("Sine of 30°: " + sinValue);
System.out.println("Cosine of 30°: " + cosValue);
System.out.println("Tangent of 30°: " + tanValue);
// Inverse trigonometric functions
double asinValue = Math.asin(sinValue);
System.out.println("Arcsine of sin(30°): " +
Math.toDegrees(asinValue) + "°");
}
}
此示例显示了三角运算。我们首先将度数转换为弧度,然后计算正弦、余弦和正切。反函数返回以弧度表示的结果,为了可读性,我们将其转换回度数。
指数和对数函数
Math 类提供了用于指数和对数计算的方法。Math.exp 计算 e^x,而 Math.log 计算自然对数(底数为 e)。对于以 10 为底的对数,请使用 Math.log10。
package com.zetcode;
public class Main {
public static void main(String[] args) {
// Exponential function
double expValue = Math.exp(1);
System.out.println("e^1: " + expValue);
System.out.println("Math.E: " + Math.E);
// Natural logarithm
double logValue = Math.log(Math.E);
System.out.println("ln(e): " + logValue);
// Base 10 logarithm
double log10Value = Math.log10(100);
System.out.println("log10(100): " + log10Value);
// Combining operations
double result = Math.pow(2, Math.log10(100));
System.out.println("2^log10(100): " + result);
}
}
此示例演示了指数和对数函数。我们展示了 Math.exp 和 Math.log 之间的关系,以及它们如何与常量 Math.E 相关联。该示例还展示了如何将这些运算与其他 Math 方法结合使用。
舍入方法
Math 类提供了几种具有不同行为的舍入方法。Math.round 执行标准舍入,而 Math.ceil 始终向上舍入,Math.floor 始终向下舍入。
package com.zetcode;
public class Main {
public static void main(String[] args) {
double num1 = 3.4;
double num2 = 3.6;
double num3 = -3.4;
// Standard rounding
System.out.println("Round 3.4: " + Math.round(num1));
System.out.println("Round 3.6: " + Math.round(num2));
// Ceiling (round up)
System.out.println("Ceil 3.4: " + Math.ceil(num1));
System.out.println("Ceil -3.4: " + Math.ceil(num3));
// Floor (round down)
System.out.println("Floor 3.6: " + Math.floor(num2));
System.out.println("Floor -3.4: " + Math.floor(num3));
// Truncate using casting
System.out.println("Truncate 3.6: " + (int) num2);
}
}
此示例显示了不同的舍入方法。Math.round 遵循标准舍入规则,而 Math.ceil 和 Math.floor 始终在特定方向上舍入。该示例还展示了如何使用强制类型转换进行截断。
随机数生成
Math.random 方法生成 0.0(包括)和 1.0(不包括)之间的伪随机数。要更好地控制随机数,请考虑使用 java.util 包中的 Random 类。
package com.zetcode;
public class Main {
public static void main(String[] args) {
// Simple random number
double random1 = Math.random();
System.out.println("Random between 0 and 1: " + random1);
// Random number in range
int min = 10;
int max = 20;
int randomInRange = (int) (Math.random() * (max - min + 1)) + min;
System.out.println("Random between 10 and 20: " + randomInRange);
// Multiple random numbers
System.out.println("\nFive random numbers:");
for (int i = 0; i < 5; i++) {
System.out.println(Math.random());
}
}
}
此示例演示了随机数生成。我们展示了如何获取一个简单的随机数,以及如何将其缩放到特定范围。范围缩放公式对于随机数的实际应用非常重要。
数学常数
Math 类提供了两个重要的数学常数:Math.PI (π) 和 Math.E(自然对数的底数)。这些常数以高精度定义,并在许多数学计算中非常有用。
package com.zetcode;
public class Main {
public static void main(String[] args) {
// Circle calculations using PI
double radius = 5.0;
double circumference = 2 * Math.PI * radius;
double area = Math.PI * Math.pow(radius, 2);
System.out.println("Circle with radius 5:");
System.out.println("Circumference: " + circumference);
System.out.println("Area: " + area);
// Exponential growth using E
double initial = 100;
double rate = 0.05;
double time = 10;
double finalAmount = initial * Math.exp(rate * time);
System.out.println("\nExponential growth:");
System.out.println("Initial: " + initial);
System.out.println("Final after 10 years at 5%: " + finalAmount);
}
}
此示例显示了数学常数的实际用途。我们使用 Math.PI 计算圆的属性,并使用 Math.E 对指数增长进行建模。这些常数对于许多科学和工程计算至关重要。
高级数学运算
Math 类还提供更高级的运算,如双曲函数、角度转换和 IEEE 余数。这些方法对于专门的数学计算非常有用。
package com.zetcode;
public class Main {
public static void main(String[] args) {
// Hyperbolic functions
double x = 1.0;
System.out.println("sinh(1.0): " + Math.sinh(x));
System.out.println("cosh(1.0): " + Math.cosh(x));
System.out.println("tanh(1.0): " + Math.tanh(x));
// IEEE remainder
System.out.println("\nIEEE remainder:");
System.out.println("IEEEremainder(10, 3): " +
Math.IEEEremainder(10, 3));
System.out.println("10 % 3: " + (10 % 3));
// Copy sign
System.out.println("\nCopy sign:");
System.out.println("copySign(5.0, -1.0): " +
Math.copySign(5.0, -1.0));
System.out.println("copySign(-5.0, 1.0): " +
Math.copySign(-5.0, 1.0));
}
}
此示例演示了高级 Math 类运算。双曲函数类似于三角函数,但基于双曲线。IEEE 余数与其处理负数的方式不同于模运算符。copySign 可用于操作数字符号。
来源
在本文中,我们通过实际示例介绍了 Java Math 类的所有主要方法。这些方法提供了基本的数学运算,这些运算是许多编程任务的基础。
作者
列出所有Java教程。