• Java基础知识强化79:被遗忘的Java Math类


    1. Math类概述

    Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

    2. 成员变量 和 成员方法(常用的)

    (1)成员变量

    1 public  static  final  double PI
    1 public  static  final  double E

    (2)成员方法

    1 public static int abs(int a):绝对值
    2 public static double ceil(double a):向上取整
    3 public static double floor(double a):向下取整
    4 public static int max(int a,int b):最大值 (min自学)
    5 public static double pow(double a,double b):a的b次幂
    6 public static double random():随机数 [0.0,1.0),0.0 <= x < 1.0
    7 public static int round(float a) 四舍五入(参数为double的自学)
    8 public static double sqrt(double a):正平方根    

    3. 演示案例

     1 package cn.itcast_01;
     2 
     3 /*
     4  * Math:用于数学运算的类。
     5  * 成员变量:
     6  *         public static final double PI
     7  *         public static final double E
     8  * 成员方法:
     9  *         public static int abs(int a):绝对值
    10  *        public static double ceil(double a):向上取整
    11  *        public static double floor(double a):向下取整
    12  *        public static int max(int a,int b):最大值 (min自学)
    13  *        public static double pow(double a,double b):a的b次幂
    14  *        public static double random():随机数 [0.0,1.0)
    15  *        public static int round(float a) 四舍五入(参数为double的自学)
    16  *        public static double sqrt(double a):正平方根
    17  */
    18 public class MathDemo {
    19     public static void main(String[] args) {
    20         // public static final double PI
    21         System.out.println("PI:" + Math.PI);
    22         // public static final double E
    23         System.out.println("E:" + Math.E);
    24         System.out.println("--------------");
    25 
    26         // public static int abs(int a):绝对值
    27         System.out.println("abs:" + Math.abs(10));
    28         System.out.println("abs:" + Math.abs(-10));
    29         System.out.println("--------------");
    30 
    31         // public static double ceil(double a):向上取整
    32         System.out.println("ceil:" + Math.ceil(12.34));
    33         System.out.println("ceil:" + Math.ceil(12.56));
    34         System.out.println("--------------");
    35 
    36         // public static double floor(double a):向下取整
    37         System.out.println("floor:" + Math.floor(12.34));
    38         System.out.println("floor:" + Math.floor(12.56));
    39         System.out.println("--------------");
    40 
    41         // public static int max(int a,int b):最大值
    42         System.out.println("max:" + Math.max(12, 23));
    43         // 需求:我要获取三个数据中的最大值
    44         // 方法的嵌套调用
    45         System.out.println("max:" + Math.max(Math.max(12, 23), 18));
    46         // 需求:我要获取四个数据中的最大值
    47         System.out.println("max:"
    48                 + Math.max(Math.max(12, 78), Math.max(34, 56)));
    49         System.out.println("--------------");
    50 
    51         // public static double pow(double a,double b):a的b次幂
    52         System.out.println("pow:" + Math.pow(2, 3));
    53         System.out.println("--------------");
    54 
    55         // public static double random():随机数 [0.0,1.0)
    56         System.out.println("random:" + Math.random());
    57         // 获取一个1-100之间的随机数
    58         System.out.println("random:" + ((int) (Math.random() * 100) + 1));
    59         System.out.println("--------------");
    60 
    61         // public static int round(float a) 四舍五入(参数为double的自学)
    62         System.out.println("round:" + Math.round(12.34f));
    63         System.out.println("round:" + Math.round(12.56f));
    64         System.out.println("--------------");
    65         
    66         //public static double sqrt(double a):正平方根
    67         System.out.println("sqrt:"+Math.sqrt(4));
    68     }
    69 }

    运行效果如下:

    4. 总结一下Math类的方式

     1 public class MathTest  
     2 {  
     3     public static void main(String[] args)   
     4     {  
     5         /*---------下面是三角运算---------*/  
     6         //将弧度转换角度  
     7         System.out.println("Math.toDegrees(1.57):"   
     8             + Math.toDegrees(1.57));   
     9         //将角度转换为弧度  
    10         System.out.println("Math.toRadians(90):"   
    11             + Math.toRadians(90));  
    12         //计算反余弦,返回的角度范围在 0.0 到 pi 之间。  
    13         System.out.println("Math.acos(1.2):" + Math.acos(1.2));   
    14         //计算反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。   
    15         System.out.println("Math.asin(0.8):" + Math.asin(0.8));   
    16         //计算反正切;返回的角度范围在 -pi/2 到 pi/2 之间。   
    17         System.out.println("Math.atan(2.3):" + Math.atan(2.3));   
    18         //计算三角余弦。  
    19         System.out.println("Math.cos(1.57):" + Math.cos(1.57));   
    20         //计算值的双曲余弦。   
    21         System.out.println("Math.cosh(1.2 ):" + Math.cosh(1.2 ));   
    22         //计算正弦  
    23         System.out.println("Math.sin(1.57 ):" + Math.sin(1.57 ));   
    24         //计算双曲正弦  
    25         System.out.println("Math.sinh(1.2 ):" + Math.sinh(1.2 ));  
    26         //计算三角正切  
    27         System.out.println("Math.tan(0.8 ):" + Math.tan(0.8 ));   
    28         //计算双曲正切  
    29         System.out.println("Math.tanh(2.1 ):" + Math.tanh(2.1 ));   
    30         //将矩形坐标 (x, y) 转换成极坐标 (r, thet));   
    31         System.out.println("Math.atan2(0.1, 0.2):" + Math.atan2(0.1, 0.2));  
    32         /*---------下面是取整运算---------*/  
    33         //取整,返回小于目标数的最大整数。  
    34         System.out.println("Math.floor(-1.2 ):" + Math.floor(-1.2 ));   
    35         //取整,返回大于目标数的最小整数。  
    36         System.out.println("Math.ceil(1.2):" + Math.ceil(1.2));   
    37         //四舍五入取整  
    38         System.out.println("Math.round(2.3 ):" + Math.round(2.3 ));   
    39         /*---------下面是乘方、开方、指数运算---------*/  
    40         //计算平方根。  
    41         System.out.println("Math.sqrt(2.3 ):" + Math.sqrt(2.3 ));   
    42         //计算立方根。   
    43         System.out.println("Math.cbrt(9):" + Math.cbrt(9));   
    44         //返回欧拉数 e 的n次幂。  
    45         System.out.println("Math.exp(2):" + Math.exp(2));   
    46         //返回 sqrt(x2 +y2),没有中间溢出或下溢。  
    47         System.out.println("Math.hypot(4 , 4):" + Math.hypot(4 , 4));  
    48         // 按照 IEEE 754 标准的规定,对两个参数进行余数运算。  
    49         System.out.println("Math.IEEEremainder(5 , 2):"   
    50             + Math.IEEEremainder(5 , 2));  
    51         //计算乘方  
    52         System.out.println("Math.pow(3, 2):" + Math.pow(3, 2));  
    53         //计算自然对数  
    54         System.out.println("Math.log(12):" + Math.log(12));   
    55         //计算底数为 10 的对数。  
    56         System.out.println("Math.log10(9):" + Math.log10(9));   
    57         //返回参数与 1 之和的自然对数。   
    58         System.out.println("Math.log1p(9):" + Math.log1p(9));   
    59         /*---------下面是符号相关的运算---------*/  
    60         //计算绝对值。  
    61         System.out.println("Math.abs(-4.5):" + Math.abs(-4.5));  
    62         //符号赋值,返回带有第二个浮点数符号的第一个浮点参数。  
    63         System.out.println("Math.copySign(1.2, -1.0):"   
    64             + Math.copySign(1.2, -1.0));  
    65         //符号函数;如果参数为 0,则返回 0;如果参数大于 0,  
    66         // 则返回 1.0;如果参数小于 0,则返回 -1.0。  
    67         System.out.println("Math.signum(2.3):" + Math.signum(2.3));   
    68         /*---------下面是大小相关的运算---------*/  
    69         //找出最大值  
    70         System.out.println("Math.max(2.3 , 4.5):" + Math.max(2.3 , 4.5));  
    71         //计算最小值   
    72         System.out.println("Math.min(1.2 , 3.4):" + Math.min(1.2 , 3.4));  
    73         //返回第一个参数和第二个参数之间与第一个参数相邻的浮点数。  
    74         System.out.println("Math.nextAfter(1.2, 1.0):"  
    75             + Math.nextAfter(1.2, 1.0));  
    76         //返回比目标数略大的浮点数  
    77         System.out.println("Math.nextUp(1.2 ):" + Math.nextUp(1.2 ));  
    78         //返回一个伪随机数,该值大于等于 0.0 且小于 1.0。  
    79         System.out.println("Math.random():" + Math.random());  
    80     }  
    81 }  
  • 相关阅读:
    2015腾讯暑期实习笔试题目
    二叉树的优点和缺点
    pandas对象保存到mysql出错提示“BLOB/TEXT column used in key specification without a key length”解决办法
    事务的隔离机制
    Flink Sink定制开发
    Presto实现定时从配置文件读取配置
    LDAP与Sentry API使用
    Presto压测报告
    PrestoSPI安全扩展
    项目重构总结
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4837410.html
Copyright © 2020-2023  润新知