javamath库有哪些函数(Java中Math类的简单介绍)
javamath库有哪些函数(Java中Math类的简单介绍)public class MathTest { public static void main(String[] args) { // 除法 System.out.println("6 / 4 = " Math.floorDiv(6 4)); System.out.println("7L / 2 = " Math.floorDiv(7L 2)); System.out.println("7L / 5L = " Math.floorDiv(7L 5L)); } }执行结果如下图所示:除法的示例代码如下所示:public class MathTest { public static void main(String[] args) {
我想对于Math类大家一定很熟悉了,是Java提供的一个用来进行简单数学运算的工具类。对于Math类来说,常用的方法有:
- 加法
- public static int addExact(int x int y);求两个int类型整数的和
- public static long addExact(long x long y):求两个long类型整型数的和
加法的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 加法
System.out.println("3 4 = " Math.addExact(3 4));
System.out.println("5L 4L = " Math.addExact(5L 4L));
}
}
执行结果如下图所示:
- 减法
- public static int subtractExact(int x int y):求两个int类型整数的差
- public static long subtractExact(long x long y):求两个long类型整数的差
减法的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 减法
System.out.println("6 - 4 = " Math.subtractExact(6 4));
System.out.println("7L - 2L = " Math.subtractExact(7L 2L));
}
}
执行结果如下图所示:
- 乘法
- public static int multiplyExact(int x int y):求两个int类型整数的积
- public static long multiplyExact(long x int y):求一个long类型整型数和一个int类型整数的积
- public static long multiplyExact(long x long y):求两个long类型整型数的积
乘法的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 乘法
System.out.println("6 * 4 = " Math.multiplyExact(6 4));
System.out.println("7L * 2 = " Math.multiplyExact(7L 2));
System.out.println("7L * 5L = " Math.multiplyExact(7L 5L));
}
}
执行结果如下图所示:
- 除法
- public static int floorDiv(int x int y):求两个int类型整数的相除的结果
- public static long floorDiv(long x int y):求一个long类型整型数除以一个int类型整数的结果
- public static long floorDiv(long x long y):求两个long类型整型数相除的结果
除法的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 除法
System.out.println("6 / 4 = " Math.floorDiv(6 4));
System.out.println("7L / 2 = " Math.floorDiv(7L 2));
System.out.println("7L / 5L = " Math.floorDiv(7L 5L));
}
}
执行结果如下图所示:
- 求余
- public static int floorMod(int x int y):求两个int类型整数的取模
- public static int floorMod(long x int y):求一个long类型整型数对另一个int类型整数的取模
- public static long floorMod(long x long y):求两个long类型整型数的取模
求余的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 求余
System.out.println("6 % 4 = " Math.floorMod(6 4));
System.out.println("7L % 2 = " Math.floorMod(7L 2));
System.out.println("7L % 5L = " Math.floorMod(7L 5L));
}
}
执行结果如下图所示:
- 取反
- public static int negateExact(int a):对一个int类型的整数进行取反
- public static long negateExact(long a):对一个long类型的整数进行取反
取反的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 取反
System.out.println("3取反的结果为:" Math.negateExact(3));
System.out.println("-5L取反的结果为:" Math.negateExact(-5L));
}
}
执行结果如下图所示:
- 取两个数的最大数
- public static int max(int a int b):取两个int类型整数的最大数
- public static long max(long a long b):取两个long类型整数的最大数
- public static float max(float a float b):取两个float类型浮点数的最大数
- public static double max(double a double b):取两个double类型浮点数的最大数
取两个数的最大数的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 取两个数的最大数
System.out.println("3和4的最大数为:" Math.max(3 4));
System.out.println("9L和4L的最大数为:" Math.max(9L 4L));
System.out.println("9.3F和14.2F的最大数为:" Math.max(9.3F 14.2F));
System.out.println("9.3和4.2的最大数为:" Math.max(9.3 4.2));
}
}
执行结果如下图所示:
- 取两个数的最小数
- public static int min(int a int b):取两个int类型整数的最小数
- public static long min(long a long b):取两个long类型整数的最小数
- public static float min(float a float b):取两个float类型浮点数的最小数
- public static double min(double a double b):取两个double类型浮点数的最小数
取两个数的最小数为:
public class MathTest {
public static void main(String[] args) {
// 取两个数的最小数
System.out.println("3和4的最小数为:" Math.min(3 4));
System.out.println("9L和4L的最小数为:" Math.min(9L 4L));
System.out.println("9.3F和14.2F的最小数为:" Math.min(9.3F 14.2F));
System.out.println("9.3和4.2的最小数为:" Math.min(9.3 4.2));
}
}
执行结果如下图所示:
- 取绝对值:
- public static int abs(int a):取一个int类型整数的绝对值
- public static long abs(long a):取一个long类型整数的绝对值
- public static float abs(float a):取一个float类型浮点数的绝对值
- public static double abs(double a):取一个double类型浮点数的绝对值
取绝对值的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 取绝对值
System.out.println("3的绝对值为:" Math.abs(3));
System.out.println("-3L的绝对值为:" Math.abs(-3L));
System.out.println("-3.5F的绝对值为:" Math.abs(-3.5F));
System.out.println("9.3的绝对值为:" Math.abs(9.3));
}
}
执行结果如下图所示:
- 取随机数
public static double random():获取一个0-1之间的随机小数。
取随机数的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 取绝对值
for (int i = 0; i < 5; i ) {
System.out.println("第" (i 1) "次获取的随机数为:" Math.random());
}
}
}
执行结果如下图所示:
- 自动加一
- public static int incrementExact(int a):对一个int类型整数自动加1
- public static long incrementExact(long a):对一个long类型整数自动加1
自动加1的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 取绝对值
int intValue = 10;
long longValue = 20;
for (int i = 0; i < 5; i ) {
intValue = Math.incrementExact(intValue);
System.out.println("第" (i 1) "次加1后的值为:" intValue);
longValue = Math.incrementExact(longValue);
System.out.println("第" (i 1) "次加1后的值为:" longValue);
}
}
}
执行结果如下图所示:
- 自动减一
- public static int decrementExact(int a):对一个int类型整数自动减1
- public static long decrementExact(long a):对一个long类型整数自动减1
自动减一的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 取绝对值
int intValue = 10;
long longValue = 20;
for (int i = 0; i < 5; i ) {
intValue = Math.decrementExact(intValue);
System.out.println("第" (i 1) "次减1后的值为:" intValue);
longValue = Math.decrementExact(longValue);
System.out.println("第" (i 1) "次减1后的值为:" longValue);
}
}
}
执行结果如下图所示:
- 向上取整
- public static double ceil(double a):将一个浮点数向上取整(需要说明的是,由于float可以自动转换为double,所以该方法可以满足所有浮点数的向上取整的需求。当然,大多数时候,浮点数还是习惯于用double表示。)
向上取整的示例如下所示:
public class MathTest {
public static void main(String[] args) {
// 向上取整
System.out.println("3.0向上取整结果为:" Math.ceil(3.0));
System.out.println("3.2向上取整结果为:" Math.ceil(3.2));
System.out.println("3.5向上取整结果为:" Math.ceil(3.5));
}
}
执行结果如下图所示:
- 向下取整
- public static double floor(double a):将一个浮点数向下取整。
向下取整的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 向上取整
System.out.println("3.0向下取整结果为:" Math.floor(3.0));
System.out.println("3.2向下取整结果为:" Math.floor(3.2));
System.out.println("3.6向下取整结果为:" Math.floor(3.6));
}
}
执行结果如下图所示:
- 四舍五入
- public static int round(float a):将一个float类型的浮点数进行四舍五入。
- public static long round(double a):将一个double类型的浮点数进行四舍五入。
- public static double rint(double a):将一个double类型的浮点数进行四舍五入。无需说明的是,当该浮点数距两侧的整数差值相等的时候,会优先向偶数靠拢。
四舍五入的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 四舍五入
System.out.println("3.0四舍五入结果为:" Math.round(3.0));
System.out.println("3.2四舍五入结果为:" Math.round(3.2F));
System.out.println("3.6四舍五入结果为:" Math.round(3.6));
}
}
执行结果如下图所示:
- 判断一个数的正负(若返回值是1.0则为正数,若返回值是-1.0为负数)
- public static double signum(double d):判断double类型浮点数是正数还是负数
- public static float signum(float f):判断float类型的浮点数是正数还是负数。
判断一个数的正负的代码示例如下所示:
public class MathTest {
public static void main(String[] args) {
// 判断数的正负
System.out.println("3是否为正数:" (Math.signum(3) > 0));
System.out.println("3是否为负数:" (Math.signum(3) < 0));
System.out.println("-3是否为正数:" (Math.signum(-3) > 0));
System.out.println("-3是否为负数:" (Math.signum(-3) < 0));
}
}
执行结果如下图所示:
- 开方
- public static double pow(double a double b):求a的b次方的值
- public static double exp(double a):求e的a次方的值
- public static double expm1(double x):求e的x次方加1的值
开方的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 开方
System.out.println("3的4次方为:" Math.pow(3 4));
System.out.println("2的5次方为:" Math.pow(2 5));
System.out.println("e的2次方为:" Math.exp(2));
System.out.println("e的2次方减1为:" Math.expm1(2));
}
}
执行结果如下图所示:
- 开根
- public static double sqrt(double a):求double类型浮点数的平方根
- public static double cbrt(double a):求double类型浮点数的立方根
开根的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 开根
System.out.println("4的平方根方为:" Math.sqrt(4));
System.out.println("9的平方根方为:" Math.sqrt(9));
System.out.println("8的立方根方为:" Math.cbrt(8));
System.out.println("27的立方根方为:" Math.cbrt(27));
}
}
执行结果如下图所示:
- 求对数
- public static double log(double a):求以e为底数求double类型浮点数的对数。
- public static double log10(double a):求以10为底数求double类型浮点数的对数
- public static double log1p(double x):求以e为底传入double类型浮点数加1之后的对数。
求对数的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 求对数
System.out.println("e以e为底的对数为:" Math.log(Math.E));
System.out.println("100以10为底的对数为:" Math.log10(100));
System.out.println("10加1之后以e为底的对数为:" Math.log1p(10));
System.out.println("e - 1加1之后以e为底的对数为:" Math.log1p(Math.E - 1));
}
}
执行结果如下图所示:
- 复制正负号
- public static double copySign(double magnitude double sign):根据第一个double类型浮点数的绝对值加第二个double类型浮点数的正负得出最终结果。
- public static float copySign(float magnitude float sign):根据第一个float类型浮点数的绝对值加第二个float类型浮点数的正负得出最终结果。
复制正负号的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 以第一个参数的绝对值为值,以第二个参数的符号位符号
System.out.println("(4.0F -1.0F)执行copySign方法得到的结果为:" Math.copySign(4.0F -1.0F));
System.out.println("(-4.0F -1.0F)执行copySign方法得到的结果为:" Math.copySign(-4.0F -1.0F));
System.out.println("(4.0 1.0)执行copySign方法得到的结果为:" Math.copySign(4.0 1.0));
System.out.println("(-4.0 1.0)执行copySign方法得到的结果为:" Math.copySign(-4.0 1.0));
}
}
执行结果如下图所示:
- 求正弦、余弦、正切、反正弦、反余弦和反正切
- 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 sinh(double x) :求双曲正弦
- public static double cosh(double x) :求双曲余弦
- public static double tanh(double x):求双曲正切
求正弦、余弦、正切、反正弦、反余弦、反正切、双曲正弦、双曲余弦、双曲正切的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 正弦
System.out.println(Math.sin(Math.PI / 4));
// 余弦
System.out.println(Math.cos(Math.PI / 4));
// 正切
System.out.println(Math.tan(Math.PI / 4));
// 反正弦
System.out.println(Math.asin(Math.PI / 4));
// 反余弦
System.out.println(Math.acos(Math.PI / 4));
// 反正切
System.out.println(Math.atan(Math.PI / 4));
// 双曲正弦
System.out.println(Math.sinh(Math.PI / 4));
// 双曲余弦
System.out.println(Math.cosh(Math.PI / 4));
// 双曲正切
System.out.println(Math.tanh(Math.PI / 4));
}
}
执行结果如下图所示:
- 角度和弧度互转
- public static double toDegrees(double angrad):将弧度转为角度
- public static double toRadians(double angdeg):将角度转弧度
角度和弧度互转的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 角度转弧度
System.out.println(Math.toRadians(45));
// 弧度转角度
System.out.println(Math.toDegrees(Math.PI / 2));
}
}
执行结果如下图所示:
- 求直角三角形的斜边
public static double hypot(double x double y):求直角三角形的斜边,即对x的平方加y的平方的和开根
求直角三角形的斜边的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 若两直角边为3和4,则斜边为5
System.out.println(Math.hypot(3 4));
}
}
执行结果如下图所示:
- 求前两个数相乘后与第三个数相加的和
public static float fma(float a float b float c):结果相当与a * b c;
求前两个数相乘后与第三个数相加的和的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 求前两个数相乘后与第三个数相加的和
System.out.println("3 * 4 5 = " Math.fma(3 4 5));
}
}
执行结果如下图所示:
- 返回比目标值略大或略小一点的浮点数
- public static double nextUp(double d) :返回一个比double类型浮点数略大一点的double类型的浮点数
- public static float nextUp(float f):返回一个比float类型浮点数略大一点的float类型的浮点数
- public static double nextDown(double d):返回一个比double类型浮点数略小一点的double类型的浮点数
- public static float nextDown(float f):返回一个比float类型浮点数略小一点的float类型的浮点数
- public static double nextAfter(double start double direction):返回一个在两个double类型浮点数间比第一个double类型浮点数略大一点的浮点数。
- public static float nextAfter(float start double direction):返回一个在两个float类型浮点数间比第一个float类型浮点数略小一点的浮点数
返回比目标值略大或略小一点的浮点数的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
// 求前两个数相乘后与第三个数相加的和
System.out.println("比1.0F略大的小数为:" Math.nextUp(1.0F));
System.out.println("比1.2略大的小数为:" Math.nextUp(1.2));
System.out.println("比1.0F略小的小数为:" Math.nextDown(1.0F));
System.out.println("比1.2F略小的小数为:" Math.nextDown(1.2));
System.out.println("在1.1F和1.2F之间靠近1.1F的小数为:" Math.nextAfter(1.1F 1.2F));
System.out.println("在1.5和1.6之间靠近1.5的小数为:" Math.nextAfter(1.5 1.6));
}
}
执行结果如下图所示:
- 第一个参数和2的第二个参数次方的积。
- public static float scalb(float f int scaleFactor):表示二进制第scaleFactor位的值为f时对应的十进制的值。
- public static double scalb(double d int scaleFactor):表示二进制第scaleFactor位的值为d时对应的十进制的值。
计算二进制的某一位对应的值的示例代码如下所示:
public class MathTest {
public static void main(String[] args) {
//
System.out.println("3 * 2的4次方的结果为:" Math.scalb(3 4));
System.out.println("4 * 2的4次方的结果为:" Math.scalb(4 4));
}
}
执行结果如下图所示:
除此以外,还有两个特殊的常量值:Math.PI和Math.E。