• Java Math类


    目录

    1、静态常量

    2、求最大值、最小值和绝对值

    3、求整运算

    4、三角函数运算

    5、指数运算

    6、生成随机数


    java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回值类型一般为double型。

    1、静态常量

    1. System.out.println("E 常量的值:" + Math.E);
    2. System.out.println("PI 常量的值:" + Math.PI);
    1. /**
    2. * The {@code double} value that is closer than any other to
    3. * e, the base of the natural logarithms.
    4. */
    5. public static final double E = 2.7182818284590452354;
    6. /**
    7. * The {@code double} value that is closer than any other to
    8. * pi, the ratio of the circumference of a circle to its
    9. * diameter.
    10. */
    11. public static final double PI = 3.14159265358979323846;

    2、求最大值、最小值和绝对值

    static int abs(int a)返回 a 的绝对值
    static long abs(long a)返回 a 的绝对值
    static float abs(float a)返回 a 的绝对值
    static double abs(double a)返回 a 的绝对值
    static int max(int x,int y)返回 x 和 y 中的最大值
    static double max(double x,double y)返回 x 和 y 中的最大值
    static long max(long x,long y)返回 x 和 y 中的最大值
    static float max(float x,float y)返回 x 和 y 中的最大值
    static int min(int x,int y)返回 x 和 y 中的最小值
    static long min(long x,long y)返回 x 和 y 中的最小值
    static double min(double x,double y)返回 x 和 y 中的最小值
    static float min(float x,float y)返回 x 和 y 中的最小值
    1. System.out.println("10 和 20 的较大值:" + Math.max(10, 20));
    2. System.out.println("15.6 和 15 的较小值:" + Math.min(15.6, 15));
    3. System.out.println("-12 的绝对值:" + Math.abs(-12))

     10和20的较大值:20

    15.6和15的较小值:15.0

    -12的绝对值:12

    3、求整运算

    方法说明
    static double ceil(double a)返回大于或等于 a 的最小整数
    static double floor(double a)返回小于或等于 a 的最大整数
    static double rint(double a)返回最接近 a 的整数值,如果有两个同样接近的整数,则结果取偶数
    static int round(float a)将参数加上 1/2 后返回与参数最近的整数(四舍五入)
    static long round(double a)将参数加上 1/2 后返回与参数最近的整数,然后强制转换为长整型(四舍五入)
    1. Scanner input = new Scanner(System.in);
    2. System.out.println("请输入一个数字:");
    3. double num = input.nextDouble();
    4. System.out.println("大于或等于 "+ num +" 的最小整数:" + Math.ceil(num));
    5. System.out.println("小于或等于 "+ num +" 的最大整数:" + Math.floor(num));
    6. System.out.println("将 "+ num +" 加上 0.5 之后最接近的整数:" + Math.round(num));
    7. System.out.println("最接近 "+num+" 的整数:" + Math.rint(num));

    请输入一个数字:

    99.01

    大于或等于 99.01 的最小整数:100.0

    小于或等于 99.01 的最大整数:99.0

    将 99.01 加上 0.5 之后最接近的整数:99

    最接近 99.01 的整数:99.0

    4、三角函数运算

    方法说明
    static double sin(double a)返回角的三角正弦值,参数以孤度为单位
    static double cos(double a)返回角的三角余弦值,参数以孤度为单位
    static double asin(double a)返回一个值的反正弦值,参数域在 [-1,1],值域在 [-PI/2,PI/2]
    static double acos(double a)返回一个值的反余弦值,参数域在 [-1,1],值域在 [0.0,PI]
    static double tan(double a)返回角的三角正切值,参数以弧度为单位
    static double atan(double a)返回一个值的反正切值,值域在 [-PI/2,PI/2]
    static double toDegrees(double angrad)将用孤度表示的角转换为近似相等的用角度表示的角
    staticdouble toRadians(double angdeg)将用角度表示的角转换为近似相等的用弧度表示的角
    1. System.out.println{"90 度的正弦值:" + Math.sin(Math.PI/2));
    2. System.out.println("0 度的余弦值:" + Math.cos(0));
    3. System.out.println("1 的反正切值:" + Math.atan(l));
    4. System.out.println("120 度的弧度值:" + Math.toRadians(120.0));

    90 度的正弦值:1.0

    0 的余弦值:1.0

    1 的反正切值:0.7853981633974483

    120 度的弧度值:2.0943951023931953

    5、指数运算

    方法说明
    static double exp(double a)返回 e 的 a 次幂
    static double pow(double a,double b)返回以 a 为底数,以 b 为指数的幂值
    static double sqrt(double a)返回 a 的平方根
    static double cbrt(double a)返回 a 的立方根
    static double log(double a)返回 a 的自然对数,即 lna 的值
    static double log10(double a)返回以 10 为底 a 的对数
    1. System.out.println("4 的立方值:" + Math.pow(4, 3));
    2. System.out.println("16 的平方根:" + Math.sqrt(16));
    3. System.out.println("10 为底 2 的对数:" + Math.log1O(2));

    4 的立方值:64.0

    16 的平方根:4.0

    10 为底 2 的对数:0.3010299956639812

    6、生成随机数

    random()      返回0.0到1.0的随机数
        System.out.println(Math.random());

    0.33647757258025535

    参考文章:Java Math类的常用方法 (biancheng.net) 

  • 相关阅读:
    Ros Nodelet 高速通信 单独节点编写
    【备考网络工程师】如何备考2023年网络工程师之错题集篇(1)
    【分布式任务调度】二、Elastic-Job详细介绍
    吉利笔试——编程代码题
    Java基础之《netty(2)—IO模型、BIO介绍、NIO介绍》
    Codeforces 353D 思维
    Java开发工具&安装MySql
    链表经典热门考题(下篇)
    杰理之OTA 升级【篇】
    【pytest官方文档】解读- 开发可pip安装的第三方插件
  • 原文地址:https://blog.csdn.net/qq_51409098/article/details/126490921