Math类
Math类
众多数学函数方法都被定义为static形式,使用以下方式调用
Math.数学方法
还有一些常用数学常量,如PI,调用如下
Math.PI
三角函数方法
| 方法 | 功能 |
|---|---|
| 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) | 将弧度转化为角度 |
注:角度和弧度的转换通常是不精确的,因为PI也是近似值
180°==PI
调用示例如下:
public static void main(String[] args) {
System.out.println("90°的正弦值:"+Math.sin(Math.PI/2));//取90°的sin
System.out.println("0°的余弦值:"+Math.cos(0));//取0°的cos
System.out.println("60°的正切值:"+Math.tan(Math.PI/3));//取60°的tan
//取2的平方根与2商的反正弦
//sqrt是取平方根
System.out.println("2的平方根与2商的反正弦:"+Math.asin(Math.sqrt(2)/2));
//取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("PI/2的角度值:"+Math.toDegrees(Math.PI/2));//取PI/2的角度值
}
指数函数方法
| 方法 | 功能 |
|---|---|
| public static double exp(double a) | 用于获取e的a次方, |
| public static double log(double a) | 用于取自然对数,即取Ina的值 |
| public static double log10(double a) | 用于取底数为10的a的对数 |
| public static double sqrt(double a) | 用于取a的平方根,a不能为负值 |
| public static double cbrt(double a) | 用于取a的立方根 |
| public static double pow(double a,double b) | 用于取a的b次方 |
示例如下
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));
}
取整函数方法
| 方法 | 功能 |
|---|---|
| public static double ceil(double a) | 返回大于等于参数的最小整数 |
| public static double floor(double a) | 返回小于等于参数的最大整数 |
| public static double rint(double a) | 返回与参数最接近的整数,若存在两个同样接近的整数,则结果取偶数 |
| public static double round(float a) | 将参数加上0.5后返回与参数最近的整数 |
| public static double round(double a) | 将参数加上0.5后返回与参数最近的整数,然后强制转换为长整型 |
示例如下
public static void main(String[] args) {
System.out.println("使用ceil方法取整:"+Math.ceil(5.2));//6.0
System.out.println("使用floor方法取整:"+Math.floor(2.5));//2.0
System.out.println("使用rint方法取整:"+Math.rint(2.7));//3.0
System.out.println("使用rint方法取整:"+Math.rint(2.5));//2.0,取偶
//只取整
System.out.println("使用round方法取整:"+Math.round(3.4f));//3
System.out.println("使用round方法取整:"+Math.round(2.5f));//3
//取整后强转为长整型
System.out.println("使用round方法取整:"+Math.round(2.5));//3
}
取最大值,最小值,绝对值函数方法
| 方法 | 功能 |
|---|---|
| public static double max(double a,double b) | 取a与b之间的最大值 |
| public static int min(int a,int b) | 取a与b之间的最小值,参数为整型 |
| public static long max(long a,long b) | 取a与b之间的最小值,参数为长整型 |
| public static float max(float a,float b) | 取a与b之间的最小值,参数为单精度浮点型 |
| public static double max(double a,double b) | 取a与b之间的最小值,参数为双精度浮点型 |
| public static int abs(int a) | 返回整型参数的绝对值 |
| public static long abs(long a) | 返回长整型参数的绝对值 |
| public static float abs(float a) | 返回单精度浮点型参数的绝对值 |
| public static double abs(double a) | 返回双精度浮点型参数的绝对值 |
注:以上min有的方法max也有,只不过表示的是对应参数的最大值
示例如下
public static void main(String[] args) {
System.out.println("4和10较大的为:"+Math.max(4,10));//10
System.out.println("4.8和5较小值为:"+Math.min(4.8,5));//4.8
System.out.println("-5的绝对值为:"+Math.abs(-5));//5
}

浙公网安备 33010602011771号