Class -- 11 -- Math类常用方法解析

原文链接:Class – 11 – Math类常用方法解析


相关文章:


这次主要整理下 Java 中 Math 类的常用方法


一、Math 类定义

  • Math 类位于 java.lang 包中,主要提供了一些常用的数学函数和计算

二、Math 类常用方法

  • 三角函数运算

    • Math.toDegrees(double angrad)

      • 将弧度转换为角度

        System.out.println(Math.toDegrees(1.5707963267948966)); // 90.0
        

    • Math.toRadians(double angdeg)

      • 将角度转换为弧度

        System.out.println(Math.toRadians(90)); // 1.5707963267948966
        

    • Math.sins(double a)

      • 计算正弦值

        // 先将30°转换为弧度,再计算其正弦值,30°的正弦值为0.5
        System.out.println(Math.sin(Math.toRadians(30))); // 0.49999999999999994约等于0.5
        

    • Math.asin(double a)

      • 计算反正弦值

        // 先求出值为0.5的反正弦值(弧度),再将弧度转换为角度
         System.out.println(Math.toDegrees(Math.asin(0.5))); // 30.000000000000004约等于30°
        

    • Math.cos(double a)

      • 计算余弦值

        // 先将60°转换为弧度,再计算其余弦值,60°的余弦值为0.5
        System.out.println(Math.cos(Math.toRadians(60))); // 0.5000000000000001约等于0.5
        

    • Math.acos(double a)

      • 计算反余弦值

        // 先求出值为0.5的反余弦值(弧度),再将弧度转换为角度
        System.out.println(Math.toDegrees(Math.acos(0.5))); // 60.00000000000001约等于60°
        

    • Math.tan(double a)

      • 计算正切值

      • 30°的正切值: $\frac{1}{\sqrt{3}}$

        // 先将30°转换为弧度,再计算其正切值
        System.out.println(Math.tan(Math.toRadians(30))); // 0.5773502691896257
        

    • Math.atan(double a)

      • 计算反正切值

        // 先求出值为0.5的反正切值(弧度),再将弧度转换为角度
        System.out.println(Math.toDegrees(Math.atan(0.5773502691896257))); // 29.999999999999996约等于30°
        

    • Math.sinh(double x)

      • 计算双曲正弦值

      • 双曲正弦值表达式:$\frac{e^x - e^{-x}}{2}$

        System.out.println(Math.sinh(1)); // 1.1752011936438014
        

    • Math.cosh(double x)

      • 计算双曲余弦值

      • 双曲余弦值表达式:$\frac{e^x + e^{-x}}{2}$

        System.out.println(Math.cosh(1)); // 1.543080634815244
        

    • Math.tanh(double x)

      • 计算双曲正切值

      • 双曲正切值表达式:$\frac{e^x - e^{-x}}{e^x + e^{-x}}$

        System.out.println(Math.tanh(1)); // 0.7615941559557649
        

    • Math.atan2(double y, double x)

      • 计算指定点与极坐标的弧度

      • 指定点的y轴坐标为$\sqrt{3}$,x 轴坐标为 1,其与 (0, 0) 所成的角度为 60°

        System.out.println(Math.toDegrees(Math.atan2(Math.sqrt(3), 1))) // 59.99999999999999约等于60°
        

  • 算术运算

    • Math.addExact(int x, int y)

      • 计算两参数之和,参数类型:int 或 long

        System.out.println(Math.addExact(1, 2)); // 3
        

    • Math.subtractExact(int x, int y)

      • 计算两参数之差(第一个参数 - 第二个参数),参数类型:int 或 long

        System.out.println(Math.subtractExact(3, 2)); // 1
        

    • Math.multiplyExact(int x, int y)

      • 计算两参数的乘积,参数类型:int 或 long

        System.out.println(Math.multiplyExact(2, 3)); // 6
        

    • Math.floorMod(int x, int y)

      • 计算两参数相除的余数(第一个参数 / 第二个参数),参数类型:int 或 long

        System.out.println(Math.floorMod(2, 3)); // 2
        

    • Math.floorDiv(int x, int y)

      • 计算小于或等于商的最大整数值,参数类型:int 或 long

        System.out.println(Math.floorDiv(4, 3)); // 1
        

    • Math.pow(double a, double b)

      • 计算 a 的 b 次幂,$a^b$

        System.out.println(Math.pow(2, 3)); // 8.0
        

    • Math.exp(double a)

      • 计算自然常数 e 的 a 次幂,$e^a$

        System.out.println(Math.exp(1)); // 2.718281828459045
        

    • Math.sqrt(double a)

      • 计算平方根,$\sqrt{a}$

        System.out.println(Math.sqrt(16)); // 4.0
        

    • Math.cbrt(double a)

      • 计算立方根,$\sqrt[3]{a}$

        System.out.println(Math.cbrt(8)); // 2.0
        

    • Math.hypot(double x, double y)

      • 计算两个参数平方和的平方根,$\sqrt{x^2 + y^2}$

        System.out.println(Math.hypot(6, 8)); // 10.0
        

    • Math.log(double x)

      • 计算以 e 为底的对数 (自然对数),$\log{e^x}$$\ln{x}$

        System.out.println(Math.log(Math.E)); // 1.0
        

    • Math.log10(double x)

      • 计算以 10 为底的对数,$\log{10^x}$$\lg{x}$

        System.out.println(Math.log10(100)); // 2.0
        

    • Math.log1p(double x)

      • 计算以 e 为底,1 与参数之和为指数的对数,$\log{e^{1 + x}}$$\ln{(1 + x)}$

        System.out.println(Math.log1p(Math.E - 1)); // 1.0
        

    • Math.expm1(double x)

      • 计算 e 的 x 次幂 - 1, $\log{e^x-1}$$\ln{x} - 1$

        System.out.println(Math.expm1(1) + 1); // 2.718281828459045
        

    • Math.scalb(float x, int y)

      • 计算 x 乘以 2 的 y 次幂,$x*2^y$

        System.out.println(Math.scalb(2, 3)); // 16.0
        

    • Math.IEEEremainder(double x, double y)

      • 按照 IEEE 754 标准的规定,对两个参数进行余数运算,余数的算术值等于 $x - y × n$

      • 其中 n 为最接近 $x/y$ 的商的整数

      • 如果两个整数同样接近,则选其中的偶数

      • 如果余数为 0,那么它的符号与第一个参数的符号相同

        System.out.println(Math.IEEEremainder(105, 10)); // 5.0
        System.out.println(Math.IEEEremainder(106, 10)); // -4.0
        

  • 取整运算

    • Math.ceil(double x)

      • 向上取整,返回大于该值的最近 double 值

        System.out.println(Math.ceil(1.23)); // 2.0
        System.out.println(Math.ceil(-1.23)); // -1.0
        

    • Math.floor(double x)

      • 向下取整,返回小于该值的最近 double 值

        System.out.println(Math.floor(1.23)); // 1.0
        System.out.println(Math.floor(-1.23)); // -2.0
        

    • Math.round(double x)

      • 四舍五入取整,参数类型:double、float

        System.out.println(Math.round(1.43)); // 1
        System.out.println(Math.round(1.53)); // 2
        System.out.println(Math.round(-1.43)); // -1
        System.out.println(Math.round(-1.53)); // -2
        

  • 随机运算

    • Math.random()

      • 内部调用了 Random.nextDouble() 方法,生成一个伪均匀分布在 0.0 到 1.0 之间的 double 值

        System.out.println((int)(Math.random() * 10 + 1)); // 生成范围再[1, 11)之间的伪随机数
        

  • 符号运算

    • Math.abs(int x)

      • 计算绝对值,参数类型:int、long、float、double

        System.out.println(Math.abs(-1)); // 1
        

    • Math.negateExact(int x)

      • 计算相反值,参数类型:int、long

        System.out.println(Math.negateExact(-1)); // 1
        

    • Math.signum(double x)

      • 如果参数为 0.0,则返回 0.0

      • 如果参数大于 0.0,则返回 1.0

      • 如果参数小于 0.0,则返回 -1.0

      • 参数类型:float、double

        System.out.println(Math.signum(0.1)); // 1.0
        System.out.println(Math.signum(0.0)); // 0.0
        System.out.println(Math.signum(-0.1)); // -1.0
        

    • Math.copySign(double magnitude, double sign)

      • 获取带有第二个浮点参数符号的第一个浮点参数,参数类型:(double, double)、(float, float)

        System.out.println(Math.copySign(1.1, -0.1)); // -1.1
        

  • 大小运算

    • Math.max(int x, int y)

      • 获取两个参数中的最大值,参数类型:int、long、float、double

        System.out.println(Math.max(1, 2)); // 2
        

    • Math.min(int x, int y)

      • 获取两个参数中的最小值,参数类型:int、long、float、double

        System.out.println(Math.min(1, 2)); // 1
        

    • Math.decrementExact(int x)

      • 获取该参数递减之后的数,参数类型:int、long

        System.out.println(Math.decrementExact(2)); // 1
        

    • Math.incrementExact(int x)

      • 获取该参数递增之后的数,参数类型:int、long

        System.out.println(Math.incrementExact(1)); // 2
        

    • Math.nextUp(double x)

      • 获取比参数略大的相邻浮点数,参数类型:float、double

        System.out.println(Math.nextUp(1.1)); // 1.1000000000000003
        

    • Math.nextDown(double x)

      • 获取比参数略小的相邻浮点数,参数类型:float、double

        System.out.println(Math.nextDown(1.1)); // 1.0999999999999999
        

    • Math.nextAfter(double start, double direction)

      • 获取第一个参数和第二个参数之间与第一个参数相邻的浮点数,参数类型:(double, double)、(float, double)

        System.out.println(Math.nextAfter(1.2, 1.3)); // 1.2000000000000002
        

    • Math.rint(double x)

      • 获取与参数最接近的double值

        System.out.println(Math.rint(1.4)); // 1.0
        System.out.println(Math.rint(1.5)); // 2.0
        System.out.println(Math.rint(-1.4)); // -1.0
        System.out.println(Math.rint(-1.5)); // -2.0
        

  • 其他运算

    • Math.ulp(double x)

      • 获取参数的 ulp 值,ulp 值是该浮点值与下一个数值较大的浮点值之间的正距离,参数类型:float、double

        System.out.println(Math.ulp(0.1)); // 1.3877787807814457E-17
        

    • Math.getExponent(double x)

      • 获取在表示浮点数时使用的无偏指数,参数类型:float、double

        System.out.println(Math.getExponent(1024)); // 10
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值