有关java中Math类

一、核心特点

  1. 无需导包:属于 java.lang 包,Java 默认自动导入,直接使用。
  2. 静态方法/常量:所有功能都是静态的,直接 Math.xxx() 调用。
  3. 不可实例化:构造方法私有,不能 new Math()
  4. 精度高:基于 IEEE 754 标准,运算精度可靠。

二、常用常量

Math 类内置了两个最常用的数学常量:

// 自然对数的底数 e ≈ 2.71828
double e = Math.E;  

// 圆周率 π ≈ 3.1415926
double pi = Math.PI; 

三、高频常用方法(分类型整理)

1. 基本运算:绝对值、平方根、立方根、幂运算

// 1. 绝对值:正数返回本身,负数返回相反数
Math.abs(-10);    // 返回 10
Math.abs(3.5);    // 返回 3.5

// 2. 平方根:x ≥ 0,返回 double
Math.sqrt(16);    // 返回 4.0
Math.sqrt(2);     // 返回 1.4142...

// 3. 立方根:支持负数
Math.cbrt(-8);    // 返回 -2.0

// 4. 幂运算:Math.pow(底数, 指数)
Math.pow(2, 3);   // 2³ = 8.0
Math.pow(4, 0.5); // 平方根 = 2.0

2. 取整方法(面试/开发最常用)

方法 作用 示例
Math.round() 四舍五入(返回 long/int) Math.round(3.6) → 4
Math.floor() 向下取整(小于等于原数) Math.floor(3.9) → 3.0
Math.ceil() 向上取整(大于等于原数) Math.ceil(3.1) → 4.0
Math.rint() 就近取整(偶数优先) Math.rint(2.5) → 2.0

代码示例:

Math.round(5.2);   // 5
Math.floor(-2.3);  // -3.0(向下取整,负数更小)
Math.ceil(-2.3);   // -2.0(向上取整,负数更大)

3. 最大/最小值、符号判断

// 最大值
Math.max(10, 20);    // 20
Math.max(3.5, 1.9);  // 3.5

// 最小值
Math.min(-5, 0);     // -5

// 判断数字符号:正数返回1.0,负数返回-1.0,0返回0.0
Math.signum(10);     // 1.0
Math.signum(-7);     // -1.0

4. 随机数:Math.random()

  • 作用:生成一个 [0.0, 1.0) 区间的 double 随机数(包含0,不包含1)。
  • 常用场景:生成指定范围的整数。
// 基础用法
double r = Math.random();  // 例:0.457...

// 生成 [0, n) 的整数:(int)(Math.random() * n)
int num1 = (int)(Math.random() * 10);  // 0~9

// 生成 [min, max] 的整数:min + (int)(Math.random() * (max - min + 1))
int num2 = 5 + (int)(Math.random() * 6);  // 5~10

5. 三角函数(角度用弧度,需转角度)

Java 三角函数参数是弧度,角度转弧度公式:弧度 = 角度 × π / 180

// 正弦 sin(30°)
Math.sin(Math.PI / 6);  // 0.5

// 余弦 cos(60°)
Math.cos(Math.PI / 3);  // 0.5

// 正切 tan(45°)
Math.tan(Math.PI / 4);  // 1.0

// 角度 ↔ 弧度转换
Math.toRadians(90);   // 90度转弧度 → π/2
Math.toDegrees(Math.PI); // π弧度转角度 → 180

6. 对数运算

// 自然对数 ln(x),以e为底
Math.log(Math.E);  // 1.0

// 以10为底的对数 log10(x)
Math.log10(100);   // 2.0

四、完整示例代码

public class MathDemo {
    public static void main(String[] args) {
        // 常量
        System.out.println("π = " + Math.PI);
        System.out.println("e = " + Math.E);

        // 基本运算
        System.out.println("绝对值:" + Math.abs(-99));
        System.out.println("平方根:" + Math.sqrt(25));
        System.out.println("2的5次方:" + Math.pow(2, 5));

        // 取整
        System.out.println("四舍五入:" + Math.round(4.7));
        System.out.println("向下取整:" + Math.floor(4.9));
        System.out.println("向上取整:" + Math.ceil(4.1));

        // 最大最小值
        System.out.println("最大值:" + Math.max(8, 15));
        System.out.println("最小值:" + Math.min(8, 15));

        // 随机数
        int randomNum = 1 + (int)(Math.random() * 100); // 1~100随机数
        System.out.println("1-100随机数:" + randomNum);
    }
}

五、注意事项

  1. 运算异常
    • Math.sqrt(-4):返回 NaN(非数字),负数无平方根。
    • Math.log(-1):返回 NaN
  2. 返回值类型
    • 大部分方法返回 double,需要整数时强转(如 (int)Math.sqrt(16))。
  3. 随机数替代
    • 如需更灵活的随机数,推荐使用 Random 类,但 Math.random() 适合简单场景。

总结

  1. Math 是静态工具类,直接 Math.方法名() 调用,无需实例化。
  2. 核心功能:取整、随机数、绝对值、幂运算、三角函数
  3. 重点记忆:round()(四舍五入)、random()(随机数)、pow()(幂)、sqrt()(平方根)。
  4. 三角函数必须用弧度,用 Math.toRadians() 转换角度。
posted @ 2026-06-07 20:34  李伯韬  阅读(5)  评论(0)    收藏  举报