JDK源码阅读-------自学笔记(十五)(java.lang.Math数学类)

Math类简介

  • 用于常见的数学方法
  • 如果需要更加强大的数学运算能力,计算高等数学中的相关内容,可以使用apache commons下面的Math类库

常用方法及实战

  • abs 绝对值
    实例:
    1     //绝对值
    2     System.out.println(Math.abs(-45));
    View Code
  • acos,asin,atan,cos,sin,tan 三角函数
    实例:
     1     // 计算30°的正弦值
     2     System.out.println("30°的正弦值:" + Math.sin(Math.PI / 6));
     3 
     4     // 计算30°的余弦值
     5     System.out.println("30°的余弦值:" + Math.cos(Math.PI / 6));
     6 
     7     // 计算30°的正切值
     8     System.out.println("30°的正切值:" + Math.tan(Math.PI / 6));
     9 
    10     // 计算0.5的反正弦
    11     System.out.println("0.5的反正弦值:" + Math.asin(0.5));
    12 
    13     // 计算0.866的反余弦
    14     System.out.println("0.866的反余弦值:" + Math.acos(0.866));
    15 
    16     // 计算0.5774的反正切
    17     System.out.println("0.5774的反正切值:" + Math.atan(0.5774));
    View Code
  • sqrt 平方根
    实例:
    1    
    2     // 开方
    3     System.out.println(Math.sqrt(64));
    View Code
  • pow(double a, double b) a的b次幂
    实例:
    1     // a的b次幂
    2     System.out.println(Math.pow(5, 2));
    3     System.out.println(Math.pow(2, 5));
    View Code
  • max(double a, double b) 取大值
    实例:
    1     // 比较两个数的大小输出大的那个
    2     System.out.println("两个数比大小"+Math.max(102.123, 180.456));
    View Code
  • min(double a, double b) 取小值
    实例:
    1     // 比较两个数的大小输出小的那个
    2     System.out.println("两个数比大小"+Math.min(102.123, 180.456));
    View Code
  • ceil(double a) 大于a的最小整数
    实例:
    1      // 取整
    2     System.out.println(Math.ceil(3.5));
    View Code
  • floor(double a) 小于a的最大整数
    实例:
    1    // 取整
    2    System.out.println(Math.floor(3.5));
    View Code
  • random() 返回 0.0 到 1.0 的随机数
    实例:
    1     //随机数[0,1) 0-1之间
    2     System.out.println(Math.random());
    View Code
  • long round(double a) double型的数据a转换为long型(四舍五入)
    实例:
    1     //取近似值
    2     System.out.println(Math.round(3.5));
    3     System.out.println(Math.round(3.1));
    View Code
  • toDegrees(double angrad) 弧度->角度 用于将参数转化为角度
    实例:
    1     double x = 45.0;
    2     double y = 30.0;
    3 
    4     System.out.println("X的角度是:" + Math.toDegrees(x));
    5     System.out.println("Y的角度是:" + Math.toDegrees(y));
    View Code
  • toRadians(double angdeg) 角度->弧度 用于将参数转化为弧度
    实例:
    1     double x = 45.0;
    2     double y = 30.0;
    3 
    4     System.out.println("X的弧度是:" + Math.toRadians(x));
    5     System.out.println("Y的弧度是:" + Math.toRadians(y));
    View Code
  • 常用的常量
    实例:
    1        //Math类中常用的常量 
    2        System.out.println(Math.PI); 
    3        System.out.println(Math.E);
    View Code


原文地址:https://www.cnblogs.com/liuyangfirst/p/12900459.html