java double 取绝对值_Java中Math类的简单介绍

我想对于Math类大家一定很熟悉了,是Java提供的一个用来进行简单数学运算的工具类。对于Math类来说,常用的方法有:

  • 加法
  1. public static int addExact(int x, int y);求两个int类型整数的和
  2. 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));    }}

执行结果如下图所示:

f7480938fc737ae4b725eed447d05fb2.png
  • 减法
  1. public static int subtractExact(int x, int y):求两个int类型整数的差
  2. 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));    }}

执行结果如下图所示:

99b623ca5b9d589bf28653e46b3c2e71.png
  • 乘法
  1. public static int multiplyExact(int x, int y):求两个int类型整数的积
  2. public static long multiplyExact(long x, int y):求一个long类型整型数和一个int类型整数的积
  3. 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));    }}

执行结果如下图所示:

84781e1c7ee37fe99e409db3aea4c9c4.png
  • 除法
  1. public static int floorDiv(int x, int y):求两个int类型整数的相除的结果
  2. public static long floorDiv(long x, int y):求一个long类型整型数除以一个int类型整数的结果
  3. 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));    }}

执行结果如下图所示:

11b4da4bc76ecb37ec9e045422227fa0.png
  • 求余
  1. public static int floorMod(int x, int y):求两个int类型整数的取模
  2. public static int floorMod(long x, int y):求一个long类型整型数对另一个int类型整数的取模
  3. 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));    }}

执行结果如下图所示:

6ca3aa4a802b16891caa159ba1155381.png
  • 取反
  1. public static int negateExact(int a):对一个int类型的整数进行取反
  2. 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));    }}

执行结果如下图所示:

70a35f232503dd88d8f19dcaaf52d276.png
  • 取两个数的最大数
  1. public static int max(int a, int b):取两个int类型整数的最大数
  2. public static long max(long a, long b):取两个long类型整数的最大数
  3. public static float max(float a, float b):取两个float类型浮点数的最大数
  4. 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));    }}

执行结果如下图所示:

8b4563300c08474261ce85b0d974c422.png
  • 取两个数的最小数
  1. public static int min(int a, int b):取两个int类型整数的最小数
  2. public static long min(long a, long b):取两个long类型整数的最小数
  3. public static float min(float a, float b):取两个float类型浮点数的最小数
  4. 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));    }}

执行结果如下图所示:

26a67f9e98e5a0942096f3eb35487290.png
  • 取绝对值:
  1. public static int abs(int a):取一个int类型整数的绝对值
  2. public static long abs(long a):取一个long类型整数的绝对值
  3. public static float abs(float a):取一个float类型浮点数的绝对值
  4. 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));    }}

执行结果如下图所示:

7bd44f7b558493044ff588cd9d9e4ee5.png
  • 取随机数
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());        }    }}

执行结果如下图所示:

411bcad5634dc3851eed5e2da255e212.png
  • 自动加一
  1. public static int incrementExact(int a):对一个int类型整数自动加1
  2. 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);        }    }}

执行结果如下图所示:

12302a1cc89479500083a9359421bfd2.png
  • 自动减一
  1. public static int decrementExact(int a):对一个int类型整数自动减1
  2. 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);        }    }}

执行结果如下图所示:

e2c7d42be9940ac56607a90457cd3ba3.png
  • 向上取整
  1. 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));    }}

执行结果如下图所示:

304a615717558546f620b6fd2480c4c9.png
  • 向下取整
  1. 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));    }}

执行结果如下图所示:

1ff67c5566f51b42715e4697a2abf978.png
  • 四舍五入
  1. public static int round(float a):将一个float类型的浮点数进行四舍五入。
  2. public static long round(double a):将一个double类型的浮点数进行四舍五入。
  3. 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));    }}

执行结果如下图所示:

96996df4b035390524c5df9079b3812e.png
  • 判断一个数的正负(若返回值是1.0则为正数,若返回值是-1.0为负数)
  1. public static double signum(double d):判断double类型浮点数是正数还是负数
  2. 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));    }}

执行结果如下图所示:

6d66950d3af43a160827ac13a6b5e255.png
  • 开方
  1. public static double pow(double a, double b):求a的b次方的值
  2. public static double exp(double a):求e的a次方的值
  3. 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));    }}

执行结果如下图所示:

35c12e945ac29c5b2565e5851be467d3.png
  • 开根
  1. public static double sqrt(double a):求double类型浮点数的平方根
  2. 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));    }}

执行结果如下图所示:

83fed060b11416a4876451882ac512ac.png
  • 求对数
  1. public static double log(double a):求以e为底数求double类型浮点数的对数。
  2. public static double log10(double a):求以10为底数求double类型浮点数的对数
  3. 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));    }}

执行结果如下图所示:

7d510a9f5ddc18679b32ebf20ee9065a.png
  • 复制正负号
  1. public static double copySign(double magnitude, double sign):根据第一个double类型浮点数的绝对值加第二个double类型浮点数的正负得出最终结果。
  2. 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));    }}

执行结果如下图所示:

a75b809a007616c2e829174ccd6980fb.png
  • 求正弦、余弦、正切、反正弦、反余弦和反正切
  1. public static double sin(double a):求正弦
  2. public static double cos(double a):求余弦
  3. public static double tan(double a):求正切
  4. public static double asin(double a):求反正弦
  5. public static double acos(double a):求反余弦
  6. public static double atan(double a):求反正切
  7. public static double sinh(double x) :求双曲正弦
  8. public static double cosh(double x) :求双曲余弦
  9. 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));    }}

执行结果如下图所示:

64192c7e63d27c35e1793110023cdf63.png
  • 角度和弧度互转
  1. public static double toDegrees(double angrad):将弧度转为角度
  2. 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));    }}

执行结果如下图所示:

4c4b9056906b29ba96c2f846d86bc528.png
  • 求直角三角形的斜边
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));    }}

执行结果如下图所示:

aba46cb8f18e4389812f82105718be79.png

  • 求前两个数相乘后与第三个数相加的和
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));    }}

执行结果如下图所示:

9bc6e9d6a3dc200aa7faa35d0c92fbc4.png
  • 返回比目标值略大或略小一点的浮点数
  1. public static double nextUp(double d) :返回一个比double类型浮点数略大一点的double类型的浮点数
  2. public static float nextUp(float f):返回一个比float类型浮点数略大一点的float类型的浮点数
  3. public static double nextDown(double d):返回一个比double类型浮点数略小一点的double类型的浮点数
  4. public static float nextDown(float f):返回一个比float类型浮点数略小一点的float类型的浮点数
  5. public static double nextAfter(double start, double direction):返回一个在两个double类型浮点数间比第一个double类型浮点数略大一点的浮点数。
  6. 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));    }}

执行结果如下图所示:

a7fd4ef150808c16521400b4e47a3748.png
  • ​​第一个参数和2的第二个参数次方的积。
  1. public static float scalb(float f, int scaleFactor):表示二进制第scaleFactor位的值为f时对应的十进制的值。
  2. 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));    }}

执行结果如下图所示:

b0a53b5d932b69a0fafc34be02d4d39d.png

除此以外,还有两个特殊的常量值:Math.PI和Math.E。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值