Java数字处理类-- Math类--数学运算

        在Java中提供了一个执行数学基本运算的Math类,该类包括了常用的数学运算方法和常量,包括【三角函数方法】,【指数函数方法】,【取整函数方法】、【取最大值函数方法】、【取最小值函数方法】、【取平均值函数方法】、【对数函数方法】,【平方根函数方法】、【PI】、【E】等,这些方法都被定义为static形式,调用方便。

1. 调用方式:

        Math.数学方法/Math.PI/Math.E

2. 三角函数方法:返回值都是double型

        2.1 返回三角正弦:

                public static double sin(double a)

        2.2 返回三角余弦:

                public static double cos(double a)

        2.3 返回三角正切:

                public static double tan(double a)

        2.4 返回一个值的反正弦:

                public static double asin(double a)

        2.5 返回一个值的反余弦:

                public static double acos(double a)

        2.6 返回一个的反正切:

                public static double atan(double a)

        2.7 将角度转换为弧度:

                public static double toRadians(double angdeg)

        2.8 将弧度转换为角度:

                public static double toDegrees(double angrad

package MathInfo;

import java.lang.Math;

public class TrigonometricFunction {
    public static void main(String[] args) {
        System.out.println("90度正弦值:" + Math.sin(Math.PI / 2));  //取90度正弦值
        System.out.println("0度余弦值:" + Math.cos(0));   //取0度余弦值
        System.out.println("60度正切值:" + Math.tan(Math.PI / 3));  //取60度正切值
        System.out.println("2的平方根与2商的反正弦值:" + Math.asin(Math.sqrt(2) / 2));
        System.out.println("2的平方根与2商的反余弦值:" + Math.acos(Math.sqrt(2) / 2));
        System.out.println("1的反正切值:" + Math.atan(1)); //取1的反正切值
        System.out.println("120度的弧度值:" + Math.toRadians(120.0));  //取120度的弧度值
        System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI / 2));   //取π/2的角度值
    }
}

输出:
        90度正弦值:1.0
        0度余弦值:1.0
        60度正切值:1.7320508075688767
        2的平方根与2商的反正弦值:0.7853981633974484
        2的平方根与2商的反余弦值:0.7853981633974483
        1的反正切值:0.7853981633974483
        120度的弧度值:2.0943951023931953
        π/2的角度值:90.0

3. 指数函数方法

        3.1 获取e的a次方,即取e^a:

                public static double exp(double a) 

        3.2 获取自然对数,即lna的值:

                 public static double log(double a) 

        3.3 获取底数为10的对数:

                 public static double log10(double a) 

        3.4 获取a的平方根,其中a的值不能为负数:

                public static double sqrt(double a)    

        3.5 取a的立方根:

                public static double cbrt(double a)      

        3.6 获取a的b次方:

                public static double pow(double a, double b) 

package MathInfo;

public class ExponentFunction {
    public static void main(String[] args) {
        System.out.println("e的平方值:" + Math.exp(2));
        System.out.println("以e为底2的对数:" + Math.log(2));
        System.out.println("以10为底2的对数:" + Math.log10(2));
        System.out.println("取4的平方根:" + Math.sqrt(4));
        System.out.println("取8的立方根:" + Math.cbrt(8));
        System.out.println("取2的2次方:" + Math.pow(2, 2));
    }
}

输出:
        e的平方值:7.38905609893065
        以e为底2的对数:0.6931471805599453
        以10为底2的对数:0.3010299956639812
        取4的平方根:2.0
        取8的立方根:2.0
        取2的2次方:4.0

4. 取整函数方法

        4.1 返回大于等于参数的最小整数:

                public static double ceil(double a)

        4.2 返回小于等于参数的最大整数:

                public static double floor(double a)

        4.3 返回与参数最接近的整数,如果两个同为整数且同样接近,则取偶数:

                public static double rint(double a)

        4.4 将参数加上0.5后返回小于等于参数+0.5后最大的整数:

                public static int round(float a)

        4.5 将参数加上0.5后返回小于等于参数+0.5后最大的整数,然后强制转换为长整型:

                public static long round(double a)

package MathInfo;

public class IntFunction {
    public static void main(String[] args) {
        //返回第一个大于等于参数的整数
        System.out.println("使用ceil()方法取整:" + Math.ceil(5.2));
        //返回第一个小于等于参数的整数
        System.out.println("使用floor()方法取整:" + Math.floor(5.2));
        //返回与参数最接近的整数
        System.out.println("使用rint()方法取整:" + Math.rint(2.7));
        //返回与参数最接近的整数
        System.out.println("使用rint()方法取整:" + Math.rint(2.5));
        //将参数加上0.5后返回最接近的整数
        System.out.println("使用round()方法取整:" + Math.round(3.4f));
        //将参数加上0.5后返回最接近的整数,然后强制转换为长整型long
        System.out.println("使用round()方法取整:" + Math.round(2.4));
    }
}

输出:
        使用ceil()方法取整:6.0
        使用floor()方法取整:5.0
        使用rint()方法取整:3.0
        使用rint()方法取整:2.0
        使用round()方法取整:3
        使用round()方法取整:2

5. 取最大值、最小值、绝对值函数方法

        5.1 取a,b中的较大值,参数为整数

                public static double max(double a, double b)

        5.2 取a,b中的较小值,参数为整数

                public static int min(int a, int b)        

        5.3 取a,b中的较小值,参数为长整型

                public static long max(long a, long b)

        5.4 取a,b中的较小值,参数为浮点型

                public static float max(float a, float b)

        5.5 取a,b中的较小值,参数为双精度型

                public static double min(double a, double b)         

        5.6 取整型参数的绝对值

                public static int abs(int a) 

        5.7 取长整型参数的绝对值

                public static long abs(long a) 

        5.8 取浮点型参数的绝对值

                public static float abs(float a) 

        5.9 取双精度型参数的绝对值

                public static double abs(doulbe a) 

package MathInfo;

public class AnyFunction {
    public static void main(String[] args) {
        System.out.println("4和8的较大值是:" + Math.max(4, 8));
        System.out.println("4和8的较小值是:" + Math.min(4, 8));
        System.out.println("-7的绝对值是:" + Math.abs(-7));
    }
}

输出:
        4和8的较大值是:8
        4和8的较小值是:4
        -7的绝对值是:7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chuntian_tester

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值