Java Math案例如何计算数值

wen java案例 25

本文目录导读:

Java Math案例如何计算数值

  1. 基本运算:绝对值、四舍五入、幂运算
  2. 三角函数计算
  3. 随机数生成
  4. 取整操作
  5. 指数和对数运算
  6. 实际应用:计算两点距离
  7. 常用 Math 方法速查表

在Java中,java.lang.Math 类提供了丰富的静态方法用于执行常见的数学运算,下面通过几个典型案例来展示其用法。

基本运算:绝对值、四舍五入、幂运算

public class BasicMathExample {
    public static void main(String[] args) {
        // 绝对值
        int absInt = Math.abs(-10);        // 10
        double absDouble = Math.abs(-3.14); // 3.14
        // 四舍五入
        long rounded1 = Math.round(3.49);   // 3
        long rounded2 = Math.round(3.51);   // 4
        // 幂运算(2的3次方)
        double power = Math.pow(2, 3);      // 8.0
        // 平方根
        double sqrt = Math.sqrt(16);         // 4.0
        // 最大值和最小值
        int max = Math.max(10, 20);          // 20
        int min = Math.min(10, 20);          // 10
        System.out.println("绝对值: " + absInt);
        System.out.println("四舍五入: " + rounded2);
        System.out.println("幂运算: " + power);
        System.out.println("平方根: " + sqrt);
    }
}

三角函数计算

public class TrigonometryExample {
    public static void main(String[] args) {
        // 角度转弧度
        double degrees = 45;
        double radians = Math.toRadians(degrees);
        // 三角函数(参数为弧度)
        double sin = Math.sin(radians);      // sin(45°) ≈ 0.707
        double cos = Math.cos(radians);      // cos(45°) ≈ 0.707
        double tan = Math.tan(radians);      // tan(45°) ≈ 1.0
        // 反三角函数(返回弧度)
        double asin = Math.asin(0.5);        // ≈ 0.5236 rad (30°)
        double acos = Math.acos(0.5);        // ≈ 1.0472 rad (60°)
        double atan = Math.atan(1.0);        // ≈ 0.7854 rad (45°)
        // 弧度转角度
        double degreesFromRad = Math.toDegrees(asin); // 30°
        System.out.printf("sin(45°) = %.3f\n", sin);
        System.out.printf("asin(0.5) = %.1f°\n", degreesFromRad);
    }
}

随机数生成

public class RandomExample {
    public static void main(String[] args) {
        // 生成 [0.0, 1.0) 范围的随机数
        double random1 = Math.random();
        // 生成 [min, max] 范围的随机整数
        int min = 1;
        int max = 100;
        int randomInt = (int)(Math.random() * (max - min + 1)) + min;
        // 生成 [0, 100) 范围的随机浮点数
        double randomDouble = Math.random() * 100;
        System.out.println("随机小数: " + random1);
        System.out.println("1-100随机整数: " + randomInt);
        System.out.println("0-100随机浮点数: " + randomDouble);
    }
}

取整操作

public class RoundingExample {
    public static void main(String[] args) {
        double number = 3.7;
        double ceil = Math.ceil(number);      // 向上取整 → 4.0
        double floor = Math.floor(number);    // 向下取整 → 3.0
        long round = Math.round(number);      // 四舍五入 → 4
        double rint = Math.rint(number);      // 就近取整 → 4.0
        // 特殊:.5时取偶数
        double rint2 = Math.rint(2.5);        // 2.0(偶数)
        double rint3 = Math.rint(3.5);        // 4.0(偶数)
        System.out.println("向上取整: " + ceil);
        System.out.println("向下取整: " + floor);
        System.out.println("四舍五入: " + round);
        System.out.println("2.5就近取整: " + rint2);
    }
}

指数和对数运算

public class ExponLogExample {
    public static void main(String[] args) {
        // 自然指数 e^x
        double exp = Math.exp(1);             // e ≈ 2.71828...
        // 自然对数 ln(x)
        double log = Math.log(Math.E);        // 1.0
        // 以10为底的对数
        double log10 = Math.log10(100);       // 2.0
        // 计算任意底数的对数:log_a(b) = ln(b) / ln(a)
        double logBase2 = Math.log(8) / Math.log(2); // 3.0 (log₂8)
        System.out.println("e^1 = " + exp);
        System.out.println("ln(e) = " + log);
        System.out.println("log₁₀(100) = " + log10);
        System.out.println("log₂(8) = " + logBase2);
    }
}

实际应用:计算两点距离

public class DistanceCalculator {
    public static void main(String[] args) {
        // 计算二维平面两点间距离
        double x1 = 0, y1 = 0;
        double x2 = 3, y2 = 4;
        double distance = Math.sqrt(
            Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)
        );
        System.out.println("两点距离: " + distance); // 5.0
        // 使用 hypot 方法更安全(避免溢出)
        double distance2 = Math.hypot(x2 - x1, y2 - y1);
        System.out.println("使用hypot: " + distance2); // 5.0
    }
}

常用 Math 方法速查表

方法 说明 示例
abs(x) 绝对值 Math.abs(-5) → 5
ceil(x) 向上取整 Math.ceil(3.2) → 4.0
floor(x) 向下取整 Math.floor(3.8) → 3.0
round(x) 四舍五入 Math.round(3.5) → 4
pow(a,b) a的b次方 Math.pow(2,3) → 8.0
sqrt(x) 平方根 Math.sqrt(16) → 4.0
random() 随机数[0,1) Math.random()
max(a,b) 最大值 Math.max(5,10) → 10
min(a,b) 最小值 Math.min(5,10) → 5

注意: Math 类的方法是静态的,直接通过类名调用,如果需要大量随机数或更复杂的随机数生成(如正态分布),建议使用 java.util.Random 类。

抱歉,评论功能暂时关闭!