Java学习之旅(三七):数学运算

在 Java 语言中提供了一个执行数学基本运算的 Math 类,该类包括常用的数学运算方法,如三角函数方法、指数函数方法、对数函数方法、平方根函数方法等一些常用的数学函数,除此之外还提供了一些常用的数学常量,如 PI、E 等。

Math 类

在 Math 类中提供了众多数学函数方法,主要包括三角函数方法、指数函数方法、取整函数方法、取最大值、取最小值以及平均值函数方法。这些方法都被定义为 static 形式,所以在程序中应用时其实是比较渐变的,可直接使用 Math.方法() 去调用。至于常量,也可使用此种方式调用:

public class MathDemo {

    public static void main(String[] args) {

        double PI = Math.PI;
        System.out.println("常量 PI = " + PI);
        double E = Math.E;
        System.out.println("常量 E = " + E);

    }

}

三角函数方法

在 Math 类中包含的三角函数方法主要有以下这些:

  • public static double sin(double a):返回角的三角正弦
  • public static double cos(double a):返回角的三角余弦
  • public static double tan(double a) :返回角的三角正切
  • public static double asin(double a):返回角的反正弦
  • public static double acos(double a):返回角的反余弦
  • public static double atan(double a):返回角的反正切
  • public static double toRadians(double angdeg):将角度转换为弧度
  • public static double toDegrees(double angrad):将弧度转换为角度

以上每个方法的参数和返回值都是 double 型的。将这些方法的参数的值设置为 double 型是有一定道理的,参数以弧度代替角度来实现,其中 1° 等于 Π / 180 弧度,所以 180° 可以使用 Π 弧度来表示。除了可以获取角的正弦、余弦、正切、反正弦、反余弦、反正切之外,Math 类还提供了角度和弧度相互转换的方法 toRadians() 和 toDegrees() 方法。但是需要注意的是,角度与弧度的转换通常是不精确的。

public class MathDemo {

    public static void main(String[] args) {

        System.out.println("sin(90°) = " + Math.sin(Math.PI / 2));
        System.out.println("cos(0°) = " + Math.cos(0));
        System.out.println("tan(60°) = " + Math.tan(Math.PI / 3));
        System.out.println("2的平方根与2的商的反正弦值 = " + Math.asin(Math.sqrt(2) / 2));  //即45°
        System.out.println("2的平方根与2的商的反余弦值 = " + Math.acos(Math.sqrt(2) / 2));  //即45°
        System.out.println("1的反正切值 = " + Math.atan(1)); //即45°
        System.out.println("120°的弧度值 = " + Math.toRadians(120));
        System.out.println("Π / 2 的角度值 = " + Math.toDegrees(Math.PI / 2));

    }

}

指数函数方法

Math 类中与指数相关的函数如下:

  • public static double exp(double a):获取 e 的 a 次方
  • public static double log(double a):获取自然对数,即以 lna 的值
  • public static double log10(double a):用于取底数为10的对数
  • public static double sqrt(double a):开平方
  • public static double cbrt(double a):开立方
  • public static double pow(double a, double b):取 a 的 b 次方
public class MathDemo {

    public static void main(String[] args) {

        System.out.println("e的平方 = " + Math.exp(2));
        System.out.println("ln2 = " + 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("3的立方 = " + Math.pow(3,3));

    }

}

取整函数方法

在具体问题中,取整操作的使用也很普遍。通常有以下几种取整的方法:

  • public static double ceil(double a):返回大于等于参数的最小整数
  • public static double floor(double a):返回小于等于参数的最大整数
  • public static double rint(double a):返回与参数最接近的整数,如果两个同为整数且同样接近,则结果取偶数
  • public static int round(float a):将参数加上0.5之后返回与参数最接近的整数
  • public static long round(double a):将参数加上0.5之后返回与参数最接近的整数,然后强制转化为长整型
public class MathDemo {

    public static void main(String[] args) {

        System.out.println("大于等于6.5的最小整数为:" + Math.ceil(6.5));
        System.out.println("小于等于6.5的最大整数为:" + Math.floor(6.5));
        System.out.println("与6.5最接近的整数为:" + Math.rint(6.5));    // 6.0,因为6.0与7.0同样接近于6.5,所以取偶数6.0
        System.out.println("6.5加上0.5之后最接近的整数为:" + Math.round(6.5));
        System.out.println("6.5加上0.5之后最接近的整数为:" + Math.round(6.5));

    }

}

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

在程序中最常用的方法就是取最大值、最小值、绝对值等 操作,在 Math 中主要有以下这几种方法:

  • public static double max(double a, double b):取 a 与 b 之间的最大值
  • public static int min(int a, int b):取 a 与 b 之间的最小值,参数为整型
  • public static long min(long a, long b):取 a 与 b 之间的最小值,参数为长整型
  • public static float min(float a, float b):取 a 与 b 之间的最小值,参数为浮点型
  • public static double min(double a, double b):取 a 与 b 之间的最小值,参数为双精度类型
  • public static int abs(int a):返回整型类型参数的绝对值
  • public static long abs(long s):返回长整型类型参数的绝对值
  • public static float abs(float a):返回浮点型类型参数的绝对值
  • public static double abs(double a):返回双精度类型参数的绝对值

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值