Java中 Math 类常用方法的详细介绍

在Java中,Math 类位于 java.lang 包中,提供了许多用于执行基本数学运算的静态方法。这些方法涵盖了从基本的算术运算到复杂的三角函数和指数运算。以下是对 Math 类常用方法的详细介绍:

1. 常量

  • Math.PI

    表示圆周率 π 的近似值,约为 3.141592653589793。

    double pi = Math.PI;
    
  • Math.E

    表示自然对数的底数 e,约为 2.718281828459045。

    double e = Math.E;
    

2. 绝对值

  • abs 方法

    返回参数的绝对值。

    int absInt = Math.abs(-10); // 10
    double absDouble = Math.abs(-5.5); // 5.5
    

3. 最大值与最小值

  • max 方法

    返回两个参数中的较大值。

    int maxInt = Math.max(10, 20); // 20
    double maxDouble = Math.max(5.5, 2.3); // 5.5
    
  • min 方法

    返回两个参数中的较小值。

    int minInt = Math.min(10, 20); // 10
    double minDouble = Math.min(5.5, 2.3); // 2.3
    

4. 幂运算与平方根

  • pow 方法

    返回第一个参数的第二个参数次幂。

    double power = Math.pow(2, 3); // 8.0
    
  • sqrt 方法

    返回参数的平方根。

    double squareRoot = Math.sqrt(16); // 4.0
    

5. 指数与对数

  • exp 方法

    返回 e 的参数次幂。

    double exponential = Math.exp(1); // e ≈ 2.718281828459045
    
  • log 方法

    返回参数的自然对数(以 e 为底)。

    double naturalLog = Math.log(Math.E); // 1.0
    
  • log10 方法

    返回参数的常用对数(以 10 为底)。

    double logBase10 = Math.log10(100); // 2.0
    

6. 三角函数

  • sincostan 方法

    计算给定弧度的正弦、余弦和正切值。

    double sine = Math.sin(Math.PI / 2); // 1.0
    double cosine = Math.cos(0); // 1.0
    double tangent = Math.tan(Math.PI / 4); // 1.0
    
  • asinacosatan 方法

    计算反正弦、反余弦和反正切值,返回值为弧度。

    double arcsine = Math.asin(1.0); // π/2
    double arccosine = Math.acos(1.0); // 0.0
    double arctangent = Math.atan(1.0); // π/4
    
  • toDegreestoRadians 方法

    在度和弧度之间转换。

    double degrees = Math.toDegrees(Math.PI); // 180.0
    double radians = Math.toRadians(180); // π
    

7. 取整方法

  • ceil 方法

    返回大于或等于参数的最小整数(以 double 形式返回)。

    double ceiling = Math.ceil(4.2); // 5.0
    
  • floor 方法

    返回小于或等于参数的最大整数(以 double 形式返回)。

    double floor = Math.floor(4.8); // 4.0
    
  • round 方法

    返回四舍五入后的整数值,类型根据参数不同而不同(intlong)。

    long rounded = Math.round(4.5); // 5
    int roundedInt = Math.round(4.4f); // 4
    
  • rint 方法

    返回最接近参数的整数值,作为 double 返回。如果参数恰好在两个整数之间,返回偶数。

    double rintValue = Math.rint(4.5); // 4.0
    

8. 随机数

  • random 方法

    返回一个 double 类型的伪随机数,范围在 [0.0, 1.0) 之间。

    double randomValue = Math.random();
    

    示例:生成 1 到 100 之间的随机整数

    int randomInt = (int)(Math.random() * 100) + 1;
    

9. 其他有用的方法

  • hypot 方法

    计算直角三角形的斜边长度,避免溢出或下溢。

    double hypotenuse = Math.hypot(3, 4); // 5.0
    
  • signum 方法

    返回参数的符号函数值:-1.0、0.0 或 1.0。

    double sign = Math.signum(-5.5); // -1.0
    
  • copySign 方法

    返回第一个参数的绝对值,并赋予第二个参数的符号。

    double result = Math.copySign(5.0, -3.0); // -5.0
    
  • nextAfternextUpnextDown 方法

    获取紧邻指定方向的浮点数。

    double next = Math.nextAfter(1.0, 2.0); // 1.0000000000000002
    double nextUp = Math.nextUp(1.0); // 1.0000000000000002
    double nextDown = Math.nextDown(1.0); // 0.9999999999999999
    
  • ulp 方法

    返回指定浮点数的单位的最后一位(ULP)。

    double ulpValue = Math.ulp(1.0); // 2.220446049250313E-16
    

10. 高级数学运算

  • atan2 方法

    计算给定 y 和 x 坐标的点相对于原点的角度(弧度)。

    double angle = Math.atan2(1.0, 1.0); // π/4
    
  • sinhcoshtanh 方法

    计算双曲正弦、双曲余弦和双曲正切。

    double sinhValue = Math.sinh(1.0);
    double coshValue = Math.cosh(1.0);
    double tanhValue = Math.tanh(1.0);
    
  • expm1 方法

    计算 e^x - 1,在 x 逼近 0 时更精确。

    double expm1Value = Math.expm1(1.0); // e - 1 ≈ 1.718281828459045
    
  • log1p 方法

    计算 ln(1 + x),在 x 逼近 0 时更精确。

    double log1pValue = Math.log1p(1.0); // ln(2) ≈ 0.6931471805599453
    

示例代码

以下是一个综合示例,展示了如何使用 Math 类的一些方法:

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

        // 绝对值
        System.out.println("Abs of -10: " + Math.abs(-10));

        // 最大值与最小值
        System.out.println("Max of 10 and 20: " + Math.max(10, 20));
        System.out.println("Min of 10 and 20: " + Math.min(10, 20));

        // 幂运算与平方根
        System.out.println("2^3: " + Math.pow(2, 3));
        System.out.println("Square root of 16: " + Math.sqrt(16));

        // 指数与对数
        System.out.println("exp(1): " + Math.exp(1));
        System.out.println("log(E): " + Math.log(Math.E));
        System.out.println("log10(100): " + Math.log10(100));

        // 三角函数
        System.out.println("sin(π/2): " + Math.sin(Math.PI / 2));
        System.out.println("cos(0): " + Math.cos(0));
        System.out.println("tan(π/4): " + Math.tan(Math.PI / 4));

        // 取整方法
        System.out.println("Ceil of 4.2: " + Math.ceil(4.2));
        System.out.println("Floor of 4.8: " + Math.floor(4.8));
        System.out.println("Round of 4.5: " + Math.round(4.5));

        // 随机数
        System.out.println("Random number: " + Math.random());

        // 高级运算
        System.out.println("Hypotenuse of 3 and 4: " + Math.hypot(3, 4));
        System.out.println("Signum of -5.5: " + Math.signum(-5.5));
        System.out.println("Next after 1.0 towards 2.0: " + Math.nextAfter(1.0, 2.0));
    }
}

输出示例:

PI: 3.141592653589793
E: 2.718281828459045
Abs of -10: 10
Max of 10 and 20: 20
Min of 10 and 20: 10
2^3: 8.0
Square root of 16: 4.0
exp(1): 2.718281828459045
log(E): 1.0
log10(100): 2.0
sin(π/2): 1.0
cos(0): 1.0
tan(π/4): 0.9999999999999999
Ceil of 4.2: 5.0
Floor of 4.8: 4.0
Round of 4.5: 5
Random number: 0.3746494481042324
Hypotenuse of 3 and 4: 5.0
Signum of -5.5: -1.0
Next after 1.0 towards 2.0: 1.0000000000000002
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YiHanXii

呜呜呜我想喝奶茶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值