• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

从零开始学习JAVA | 第二十篇常见API Math

武飞扬头像
我是一盘牛肉
帮助1

学新通

目录

前言:

 MATH:

Math类中的常用方法:

总结:


前言:

                本篇往后我们会详细介绍一些常用的API,今天我们介绍的是Math的常用方法。但是其实我们不需要记住所有的方法,在日常工作中自己学会查询API文档就可以了。

 MATH:

Math类是Java中提供的一个标准类,它包含了许多常用的数学运算和常量,使用这些API可以方便地进行数学计算。

Math类中的常用方法:

1. abs函数:返回一个数的绝对值。它有两个版本,一个用于整数,另一个用于浮点数。

  1.  
    public class MathDemo {
  2.  
        public static void main(String[] args) {
  3.  
            int x = -5;
  4.  
            double y = -2.75;
  5.  
     
  6.  
            int abs_x = Math.abs(x);
  7.  
            double abs_y = Math.abs(y);
  8.  
     
  9.  
            System.out.println("abs(" x ") = " abs_x);
  10.  
            System.out.println("abs(" y ") = " abs_y);
  11.  
        }
  12.  
    }

输出:

  1.  
    abs(-5) = 5
  2.  
    abs(-2.75) = 2.75

2. pow函数:返回一个数的指定幂。它有两个参数,第一个是底数,第二个是指数。

  1.  
    public class MathDemo {
  2.  
        public static void main(String[] args) {
  3.  
            double x = 2.0;
  4.  
            int y = 3;
  5.  
     
  6.  
            double result = Math.pow(x, y);
  7.  
     
  8.  
            System.out.println(x "^" y " = " result);
  9.  
        }
  10.  
    }

输出:

2.0^3 = 8.0

3. sqrt函数:返回一个数的平方根。

  1.  
    public class MathDemo {
  2.  
        public static void main(String[] args) {
  3.  
            double x = 16.0;
  4.  
     
  5.  
            double result = Math.sqrt(x);
  6.  
     
  7.  
            System.out.println("sqrt(" x ") = " result);
  8.  
        }
  9.  
    }

输出:

sqrt(16.0) = 4.0

4. sin,cos,tan函数:分别返回一个角的正弦、余弦和正切值。

  1.  
    public class MathDemo {
  2.  
        public static void main(String[] args) {
  3.  
            double x = 45.0 * Math.PI / 180.0;
  4.  
     
  5.  
            double sin_x = Math.sin(x);
  6.  
            double cos_x = Math.cos(x);
  7.  
            double tan_x = Math.tan(x);
  8.  
     
  9.  
            System.out.println("sin(" x ") = " sin_x);
  10.  
            System.out.println("cos(" x ") = " cos_x);
  11.  
            System.out.println("tan(" x ") = " tan_x);
  12.  
        }
  13.  
    }

输出:

  1.  
    sin(0.7853981633974483) = 0.7071067811865475
  2.  
    cos(0.7853981633974483) = 0.7071067811865476
  3.  
    tan(0.7853981633974483) = 0.9999999999999999

5. max,min函数:分别返回两个数的最大值和最小值。

  1.  
    public class MathDemo {
  2.  
        public static void main(String[] args) {
  3.  
            int x = 5;
  4.  
            int y = 3;
  5.  
     
  6.  
            int max_xy = Math.max(x, y);
  7.  
            int min_xy = Math.min(x, y);
  8.  
     
  9.  
            System.out.println("max(" x "," y ") = " max_xy);
  10.  
            System.out.println("min(" x "," y ") = " min_xy);
  11.  
        }
  12.  
    }

输出:

  1.  
    max(5,3) = 5
  2.  
    min(5,3) = 3

6. ceil,floor,round函数:分别向上取整、向下取整和四舍五入。

  1.  
    public class MathDemo {
  2.  
        public static void main(String[] args) {
  3.  
            double x = 2.75;
  4.  
     
  5.  
            double ceil_x = Math.ceil(x);
  6.  
            double floor_x = Math.floor(x);
  7.  
            long round_x = Math.round(x);
  8.  
     
  9.  
            System.out.println("ceil(" x ") = " ceil_x);
  10.  
            System.out.println("floor(" x ") = " floor_x);
  11.  
            System.out.println("round(" x ") = " round_x);
  12.  
        }
  13.  
    }

输出:

  1.  
    ceil(2.75) = 3.0
  2.  
    floor(2.75) = 2.0
  3.  
    round(2.75) = 3

7. exp函数:返回常数e的指数函数。

  1.  
    public class MathDemo {
  2.  
        public static void main(String[] args) {
  3.  
            double x = 2.0;
  4.  
     
  5.  
            double result = Math.exp(x);
  6.  
     
  7.  
            System.out.println("exp(" x ") = " result);
  8.  
        }
  9.  
    }

输出:

exp(2.0) = 7.3890560989306495

8. log函数:返回一个数的自然对数。

  1.  
    public class MathDemo {
  2.  
        public static void main(String[] args) {
  3.  
            double x = 2.0;
  4.  
     
  5.  
            double result = Math.log(x);
  6.  
     
  7.  
            System.out.println("log(" x ") = " result);
  8.  
        }
  9.  
    }

输出:

log(2.0) = 0.6931471805599453

9. ceil函数:它是将一个数字向上取整,返回结果最小的整数,可以理解为“取天花板”操作。

语法:public static double ceil(double a)

举例:如果输入的参数是2.5,则输出的结果为3.0。如果输入的参数是-2.5,则输出的结果为-2.0。

  1.  
    public class CeilDemo {
  2.  
        public static void main(String[] args) {
  3.  
            double d1 = 2.5;
  4.  
            double d2 = -2.5;
  5.  
            double d3 = 2.0;
  6.  
     
  7.  
            System.out.println(Math.ceil(d1));
  8.  
            System.out.println(Math.ceil(d2));
  9.  
            System.out.println(Math.ceil(d3));
  10.  
        }
  11.  
    }

输出:

  1.  
    3.0
  2.  
    -2.0
  3.  
    2.0

10. floor函数:与ceil函数相对,它是将一个数字向下取整,返回结果最大的整数,可以理解为“取地板”操作。

语法:public static double floor(double a)

举例:如果输入的参数是2.5,则输出的结果为2.0。如果输入的参数是-2.5,则输出的结果为-3.0。

  1.  
    public class FloorDemo {
  2.  
        public static void main(String[] args) {
  3.  
            double d1 = 2.5;
  4.  
            double d2 = -2.5;
  5.  
            double d3 = 2.0;
  6.  
     
  7.  
            System.out.println(Math.floor(d1));
  8.  
            System.out.println(Math.floor(d2));
  9.  
            System.out.println(Math.floor(d3));
  10.  
        }
  11.  
    }

输出:

  1.  
    2.0
  2.  
    -3.0
  3.  
    2.0

11. round函数:它是对一个数字进行四舍五入,返回结果最接近原始数字的整数, 如果距离两侧的整数一样,则返回距离较大的那个整数(例如,对1.5进行四舍五入,输出的结果是2.0)。

语法:public static long round(double a)

举例:如果输入的参数是2.4,则输出的结果是2。如果输入的参数是2.6,则输出的结果是3。

  1.  
    public class RoundDemo {
  2.  
        public static void main(String[] args) {
  3.  
            double d1 = 2.4;
  4.  
            double d2 = 2.6;
  5.  
     
  6.  
            System.out.println(Math.round(d1));
  7.  
            System.out.println(Math.round(d2));
  8.  
        }
  9.  
    }

输出:

  1.  
    2
  2.  
    3

总结:

        本篇我们详细的介绍了math类中的几个常见的方法,各位只需要大概知道就好了,在实际生活中我们如果有需要可以查阅API文档。

如果我的内容对你有帮助,请点赞,评论,收藏创作不易,大家的支持就是我坚持下去的动力!

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggakjj
系列文章
更多 icon
同类精品
更多 icon
继续加载