Java Math类

1、public static final double E

比任何其他值都更接近 e(即自然对数的底数)的 double 值。

2、public static final double PI

比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。

Java提供了Math工具类来完成复杂的运算,Math类是一个工具类,构造器被private 的,无法创建Math类的对象;Math类中的所有方法都是静态方法(类方法),可以直 接通过类名来调用它们。Math类还提供了两个类变量:PI(圆周率)和E(自然对数 的底数)

public class MathClassDemo {

    public static void main(String[] args) {

       

                   /*---------两个类变量-----------*/

             System.out.println("---------两个类变量-----------");

        System.out.println("Math.PI = "+Math.PI);

        System.out.println("Math.E = "+Math.E); 

       

        /*---------三角运算---------*/

             System.out.println("---------三角运算-----------");

        // 将弧度转换角度

        System.out.println("Math.toDegrees(1.57) = "

            + Math.toDegrees(1.57));

        // 将角度转换为弧度

        System.out.println("Math.toRadians(90) = "

            + Math.toRadians(90));

        // 计算反余弦,返回的角度范围在 0.0 到 pi 之间。

        System.out.println("Math.acos(1.2) = " + Math.acos(1.2));

        // 计算反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。

        System.out.println("Math.asin(0.8) = " + Math.asin(0.8));

        // 计算反正切;返回的角度范围在 -pi/2 到 pi/2 之间。

        System.out.println("Math.atan(2.3) = " + Math.atan(2.3));

        // 计算三角余弦。

        System.out.println("Math.cos(1.57) = " + Math.cos(1.57));

        // 计算值的双曲余弦。

        System.out.println("Math.cosh(1.2 ) = " + Math.cosh(1.2 ));

        // 计算正弦

        System.out.println("Math.sin(1.57 ) = " + Math.sin(1.57 ));

        // 计算双曲正弦

        System.out.println("Math.sinh(1.2 ) = " + Math.sinh(1.2 ));

        // 计算三角正切

        System.out.println("Math.tan(0.8 ) = " + Math.tan(0.8 ));

        // 计算双曲正切

        System.out.println("Math.tanh(2.1 ) = " + Math.tanh(2.1 ));

        // 将矩形坐标 (x, y) 转换成极坐标 (r, thet));

        System.out.println("Math.atan2(0.1, 0.2) = " + Math.atan2(0.1, 0.2));

       

        /*---------取整运算---------*/

             System.out.println("---------取整运算-----------");

        // 取整,返回小于目标数的最大整数。

        System.out.println("Math.floor(-1.2 ) = " + Math.floor(-1.2 ));

        // 取整,返回大于目标数的最小整数。

        System.out.println("Math.ceil(1.2) = " + Math.ceil(1.2));

        // 四舍五入取整

        System.out.println("Math.round(-2.6) = " + Math.round(-2.6));

       

        /*---------乘方、开方、指数运算---------*/

             System.out.println("---------乘方、开方、指数运算-----------");

        // 计算平方根。

        System.out.println("Math.sqrt(9) = " + Math.sqrt(9));

        // 计算立方根。

        System.out.println("Math.cbrt(27) = " + Math.cbrt(27));

        // 返回欧拉数 e 的n次幂。

        System.out.println("Math.exp(2) = " + Math.exp(2));

        // 返回 sqrt(x2 +y2)

        System.out.println("Math.hypot(4 , 5) = " + Math.hypot(4 , 5));

        // 按照 IEEE 754 标准的规定,对两个参数进行余数运算。

        System.out.println("Math.IEEEremainder(5 , 2) = "

            + Math.IEEEremainder(5 , 2));

        // 计算乘方

        System.out.println("Math.pow(3, 2) = " + Math.pow(3, 2));

        // 计算自然对数

        System.out.println("Math.log(12) = " + Math.log(12));

        // 计算底数为 10 的对数。

        System.out.println("Math.log10(9) = " + Math.log10(9));

        // 返回参数与 1 之和的自然对数。

        System.out.println("Math.log1p(9) = " + Math.log1p(9));

       

        /*---------符号相关的方法---------*/

             System.out.println("---------符号相关的方法-----------");

        // 计算绝对值。

        System.out.println("Math.abs(-4.5) = " + Math.abs(-4.5));

        // 符号赋值,返回带有第二个浮点数符号的第一个浮点参数。

        System.out.println("Math.copySign(1.2, -1.0) = "

            + Math.copySign(1.2, -1.0));

        // 符号函数;如果参数为 0,则返回 0;如果参数大于 0,

        // 则返回 1.0;如果参数小于 0,则返回 -1.0。

        System.out.println("Math.signum(2.3) = " + Math.signum(2.3));

       

        /*---------大小比较相关的方法---------*/

             System.out.println("---------大小比较相关的方法-----------");

        // 找出最大值

        System.out.println("Math.max(2.3 , 4.5) = " + Math.max(2.3 , 4.5));

        // 计算最小值

        System.out.println("Math.min(1.2 , 3.4) = " + Math.min(1.2 , 3.4));

        // 返回第一个参数和第二个参数之间与第一个参数相邻的浮点数。

        System.out.println("Math.nextAfter(1.2, 1.0) = "

            + Math.nextAfter(1.2, 1.0));

        // 返回比目标数略大的浮点数

        System.out.println("Math.nextUp(1.2 ) = " + Math.nextUp(1.2 ));

        // 返回一个伪随机数,该值大于等于 0.0 且小于 1.0。

        System.out.println("Math.random() = " + Math.random());//[0,1)

                  输出结果:

---------三角运算-----------

Math.toDegrees(1.57) = 89.95437383553926

Math.toRadians(90) = 1.5707963267948966

Math.acos(1.2) = NaN

Math.asin(0.8) = 0.9272952180016123

Math.atan(2.3) = 1.1606689862534056

Math.cos(1.57) = 7.963267107332633E-4

Math.cosh(1.2 ) = 1.8106555673243747

Math.sin(1.57 ) = 0.9999996829318346

Math.sinh(1.2 ) = 1.5094613554121725

Math.tan(0.8 ) = 1.0296385570503641

Math.tanh(2.1 ) = 0.9704519366134539

Math.atan2(0.1, 0.2) = 0.4636476090008061

---------取整运算-----------

Math.floor(-1.2 ) = -2.0

Math.ceil(1.2) = 2.0

Math.round(-2.6) = -3

---------乘方、开方、指数运算-----------

Math.sqrt(9) = 3.0

Math.cbrt(27) = 3.0

Math.exp(2) = 7.38905609893065

Math.hypot(4 , 5) = 6.4031242374328485

Math.IEEEremainder(5 , 2) = 1.0

Math.pow(3, 2) = 9.0

Math.log(12) = 2.4849066497880004

Math.log10(9) = 0.9542425094393249

Math.log1p(9) = 2.302585092994046

---------符号相关的方法-----------

Math.abs(-4.5) = 4.5

Math.copySign(1.2, -1.0) = -1.2

Math.signum(2.3) = 1.0

---------大小比较相关的方法-----------

Math.max(2.3 , 4.5) = 4.5

Math.min(1.2 , 3.4) = 1.2

Math.nextAfter(1.2, 1.0) = 1.1999999999999997

Math.nextUp(1.2 ) = 1.2000000000000002

Math.random() = 0.7549043101896806

 

         }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值