我想对于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));    }}

执行结果如下图所示:

  • 减法
  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));    }}

执行结果如下图所示:

  • 乘法
  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));    }}

执行结果如下图所示:

  • 除法
  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));    }}

执行结果如下图所示:

  • 求余
  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));    }}

执行结果如下图所示:

  • 取反
  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));    }}

执行结果如下图所示:

  • 取两个数的最大数
  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));    }}

执行结果如下图所示:

  • 取两个数的最小数
  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));    }}

执行结果如下图所示:

  • 取绝对值:
  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));    }}

执行结果如下图所示:

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

执行结果如下图所示:

  • 自动加一
  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);        }    }}

执行结果如下图所示:

  • 自动减一
  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);        }    }}

执行结果如下图所示:

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

执行结果如下图所示:

  • 向下取整
  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));    }}

执行结果如下图所示:

  • 四舍五入
  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));    }}

执行结果如下图所示:

  • 判断一个数的正负(若返回值是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));    }}

执行结果如下图所示:

  • 开方
  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));    }}

执行结果如下图所示:

  • 开根
  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));    }}

执行结果如下图所示:

  • 求对数
  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));    }}

执行结果如下图所示:

  • 复制正负号
  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));    }}

执行结果如下图所示:

  • 求正弦、余弦、正切、反正弦、反余弦和反正切
  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));    }}

执行结果如下图所示:

  • 角度和弧度互转
  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));    }}

执行结果如下图所示:

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

执行结果如下图所示:

  • 返回比目标值略大或略小一点的浮点数
  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));    }}

执行结果如下图所示:

  • ​​第一个参数和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));    }}

执行结果如下图所示:

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

java简单通讯录的实现02person类_Java中Math类的简单介绍相关推荐

  1. java中math的方法_Java中Math类常用方法代码详解

    近期用到四舍五入想到以前整理了一点,就顺便重新整理好经常见到的一些四舍五入,后续遇到常用也会直接在这篇文章更新... public class Demo{ public static void mai ...

  2. java 中的class类_Java中Class类简介

    Java中Class类简介 1. 在面向对象的世界里,万事万物皆对象.(java语言中,静态的成员.普通数据类型除外) 类是不是对象呢?类是(哪个类的对象呢?)谁的对象呢? -- 类是对象,类是jav ...

  3. java自定义异常类_java中自定义异常类

    hello,大家好,今天跟大家分享一下java中如何自定义异常,以后如果有新的心得,再添加,欢迎前辈指导... 首先,上Api,看一下异常和错误的父类: 然后,现在假设我有个循环(i=0;i<1 ...

  4. java中string类_Java中String类浅谈

    1)String对象的初始化 由于String对象特别常用,所以在对String对象进行初始化时,Java提供了一种简化的特殊语法,格式如下:      String s = "abc&qu ...

  5. java中file类_Java中file类

    File类的概述:File更应该叫做一个路径,文件路径或者文件夹路径 ,路径分为绝对路径和相对路径,绝对路径是一个固定的路径,从盘符开始,相对路径相对于某个位置,在eclipse下是指当前项目下,在d ...

  6. java math 函数_Java中Math类常用函数总结

    Java中比较常用的几个数学公式的总结: //取整,返回小于目标函数的最大整数,如下将会返回-2 Math.floor(-1.8): //取整,返回发育目标数的最小整数 Math.ceil() //四 ...

  7. process java类_Java中Process类的用途是什么?

    Java.lang.Process是Object类的子类,可以描述由Runtime类的exec()方法启动的进程. 进程和对象控制进程并获取有关该进程的信息. Process类是抽象类,因此无法实例化 ...

  8. java中printwriter类_Java中printwriter类的用法

    printwriter类: java.io 类 PrintWriter java.lang.Object java.io.Writer java.io.PrintWriter 所有已实现的接口: Cl ...

  9. java 实现 string类_java 中String类的常用方法总结,带你玩转String类。

    String类: String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.String类对象创建后不能修改,StringBuffer & St ...

最新文章

  1. socket inet_pton
  2. vb6实现union数据结构_数据结构与算法——并查集(不相交集合)
  3. 动态配置流处理-BetterCloud如何使用Flink构建报警系统
  4. Deeplearnng.AI第四部分第一周、卷积神经网络
  5. 2018年最值得关注的15大技术趋势,区块链将得到更广泛的应用
  6. Ubuntu各大分支版本功能介绍及下载地址
  7. Flink并行度优先级_集群操作常用指令_运行组件_任务提交流程_数据流图变化过程
  8. 中国电子学会python等级考试一级
  9. jqxWidgets 常用代码
  10. python连续质数计算代码分析,素性测试的Miller-Rabin算法完全解析 (C语言实现、Python实现)...
  11. 网络安全学习--007--漏洞分析简介
  12. 机器学习--SVM支持向量机
  13. 程序员才是真正的段子手
  14. Hbase------regionServer
  15. amazon alexa_如何建立您的第一个Amazon Alexa技能
  16. 【计算机网络】——体系结构
  17. 访问和更新Orkut数据
  18. 服务网格峰会 Service Mesh Summit 2022 重启报名
  19. cv2,Image,Tensor图像shape中hw顺序
  20. 他们为什么能拿到11K的入职薪资?

热门文章

  1. 傅里叶变换(待总结)
  2. 【arduino】不做不死系列,用arduino玩CyberPi童芯派之helloworld点灯
  3. Linux vi/vim使用方法 总结
  4. Linux中expect实现自动登录
  5. Spring Cloud构建微服务架构:服务容错保护(Hystrix断路器)
  6. HTTP代理ip的这些误区你知道吗?
  7. 我要做 Android 之面笔试总结
  8. 深度解析(十五)哈夫曼树
  9. angular2 安装
  10. CentOS-6.5安装配置Tengine