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

目录

前言:

 MATH:

Math类中的常用方法:

总结:


前言:

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

 MATH:

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

Math类中的常用方法:

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

public class MathDemo {
    public static void main(String[] args) {
        int x = -5;
        double y = -2.75;

        int abs_x = Math.abs(x);
        double abs_y = Math.abs(y);

        System.out.println("abs(" + x + ") = " + abs_x);
        System.out.println("abs(" + y + ") = " + abs_y);
    }
}

输出:

abs(-5) = 5
abs(-2.75) = 2.75

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

public class MathDemo {
    public static void main(String[] args) {
        double x = 2.0;
        int y = 3;

        double result = Math.pow(x, y);

        System.out.println(x + "^" + y + " = " + result);
    }
}

输出:

2.0^3 = 8.0

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

public class MathDemo {
    public static void main(String[] args) {
        double x = 16.0;

        double result = Math.sqrt(x);

        System.out.println("sqrt(" + x + ") = " + result);
    }
}

输出:

sqrt(16.0) = 4.0

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

public class MathDemo {
    public static void main(String[] args) {
        double x = 45.0 * Math.PI / 180.0;

        double sin_x = Math.sin(x);
        double cos_x = Math.cos(x);
        double tan_x = Math.tan(x);

        System.out.println("sin(" + x + ") = " + sin_x);
        System.out.println("cos(" + x + ") = " + cos_x);
        System.out.println("tan(" + x + ") = " + tan_x);
    }
}

输出:

sin(0.7853981633974483) = 0.7071067811865475
cos(0.7853981633974483) = 0.7071067811865476
tan(0.7853981633974483) = 0.9999999999999999

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

public class MathDemo {
    public static void main(String[] args) {
        int x = 5;
        int y = 3;

        int max_xy = Math.max(x, y);
        int min_xy = Math.min(x, y);

        System.out.println("max(" + x + "," + y + ") = " + max_xy);
        System.out.println("min(" + x + "," + y + ") = " + min_xy);
    }
}

输出:

max(5,3) = 5
min(5,3) = 3

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

public class MathDemo {
    public static void main(String[] args) {
        double x = 2.75;

        double ceil_x = Math.ceil(x);
        double floor_x = Math.floor(x);
        long round_x = Math.round(x);

        System.out.println("ceil(" + x + ") = " + ceil_x);
        System.out.println("floor(" + x + ") = " + floor_x);
        System.out.println("round(" + x + ") = " + round_x);
    }
}

输出:

ceil(2.75) = 3.0
floor(2.75) = 2.0
round(2.75) = 3

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

public class MathDemo {
    public static void main(String[] args) {
        double x = 2.0;

        double result = Math.exp(x);

        System.out.println("exp(" + x + ") = " + result);
    }
}

输出:

exp(2.0) = 7.3890560989306495

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

public class MathDemo {
    public static void main(String[] args) {
        double x = 2.0;

        double result = Math.log(x);

        System.out.println("log(" + x + ") = " + result);
    }
}

输出:

log(2.0) = 0.6931471805599453

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

语法:public static double ceil(double a)

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

public class CeilDemo {
    public static void main(String[] args) {
        double d1 = 2.5;
        double d2 = -2.5;
        double d3 = 2.0;

        System.out.println(Math.ceil(d1));
        System.out.println(Math.ceil(d2));
        System.out.println(Math.ceil(d3));
    }
}

输出:

3.0
-2.0
2.0

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

语法:public static double floor(double a)

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

public class FloorDemo {
    public static void main(String[] args) {
        double d1 = 2.5;
        double d2 = -2.5;
        double d3 = 2.0;

        System.out.println(Math.floor(d1));
        System.out.println(Math.floor(d2));
        System.out.println(Math.floor(d3));
    }
}

输出:

2.0
-3.0
2.0

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

语法:public static long round(double a)

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

public class RoundDemo {
    public static void main(String[] args) {
        double d1 = 2.4;
        double d2 = 2.6;

        System.out.println(Math.round(d1));
        System.out.println(Math.round(d2));
    }
}

输出:

2
3

总结:

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

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

猜你喜欢

转载自blog.csdn.net/fckbb/article/details/131405597