Java学习笔记——Math类

目录

​一、静态常量

二、三角函数

三、指数函数

四、取整

五、其他

六、代码 


Java中的Math类包含了基本的数学运算方法。下面将从以下几部分进行讲解。

一、静态常量

  • 常量 E:自然对数e,double数据类型
  • 常量PI:圆周率π,double数据类型

代码实例1

/* 静态常量 */
// 常量E
Object a = null;
a = Math.E;
System.out.println("E 常量的值: " + a);

// 常量PI
Object b = null;
b = Math.PI;
System.out.println("PI 常量的值: " + b);

输出结果1

Debug

debug后发现,常量E和常量PI均为double类型。

二、三角函数

  • toRadians():角度 --> 弧度
  • toDegrees():弧度 --> 弧度
  • 正切值sin()
  • 余弦值cos()
  • 反正弦值asin()
  • 反余弦值acos()
  • 正切值tan()
  • 反正切值atan() atan2()

代码实例2

/* 三角函数 */
// 角度 --> 弧度 toRadians()
double x = 45; // 45°  45° -->  PI / 4
System.out.println("45°转换为弧度:" + Math.toRadians(x));
System.out.println(Math.PI / 4);

double y = 180;
System.out.println("180°转换为弧度:" + Math.toRadians(y));
System.out.println(Math.PI);

// 弧度 --> 弧度 toDegrees()
double z = 0.7853981633974483; // PI / 4 -->  45°
System.out.println("0.7853981633974483转换为角度" + Math.toDegrees(z));

// 正弦函数sin()
double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.println("45° 的正弦值: " + Math.sin(radians));

// 余弦函数cos()
System.out.println("45° 的余弦值: " + Math.cos(radians));

// 反正弦值asin()
System.out.println("45° 的反正弦值: " + Math.asin(radians));

// 反余弦值acos()
System.out.println("45° 的反余弦值: " + Math.acos(radians));

// 正切值tan()
System.out.println("45° 的正切值: " + Math.tan(radians));

// 反正切值atan() atan2()
double m = 45;
double n = 30;
System.out.println("45° 的反正切值1: " + Math.atan(radians)); // atan()
System.out.println("反正弦值2: " + Math.atan2(m, n)); // atan2() 坐标系表示角的反正切值

输出结果2 

三、指数函数

  • exp():自然对数e的幂函数
  • pow():幂函数
  • sqrt():平方根
  • cbrt():立方根
  • log():ln函数
  • log 10():log_10函数

代码实例3

/* 指数函数 */
double p = 8;
double q = 3;

// exp()
System.out.println("e的6次幂: " + Math.exp(p)); // e^8

// pow()
System.out.println("8的3次幂: " + Math.pow(p, q)); // 8^3

// sqrt()
System.out.println("8的平方根: " + Math.sqrt(p));

// cbrt()
System.out.println("8的立方根: " + Math.cbrt(p)); // 2

// log()
System.out.println("ln(8): " + Math.log(p)); // ln(8)

// log10()
System.out.println("log10(8): " + Math.log10(p)); // log10(8)

输出结果3 

四、取整

  • ceil():>=的整数
  • floor():<=的整数
  • rint():最近的整数,0.5返回0
  • round():四舍五入的整数,0.5返回1

代码实例4

/* 取整 */
double d = 100.675;
double e = 100.500;

// >=的整数  ceil()
System.out.println("ceil(100.675): " + Math.ceil(d));

// <=的整数  floor()
System.out.println("floor(100.675): " + Math.floor(d));

// 最近的整数  rint()
System.out.println("rint(100.675): " + Math.rint(d));
System.out.println("rint(100.500): " + Math.rint(e));

// 四舍五入的整数  round()
System.out.println("round(100.675): " + Math.round(d));
System.out.println("round(100.500): " + Math.round(e));

输出结果4 

五、其他

  • min():最小值
  • max():最大值
  • random():随机产生一个数 random(),随机数范围为 0.0 =< Math.random < 1.0
  • abs():绝对值

代码实例6

/* 其他 */
// min() 最小
System.out.println("min(): " + Math.min(2, 10));

// max() 最大
System.out.println("max(): " + Math.max(2, 10));

// random() 返回一个随机数
System.out.println("random(): " + Math.random());

// abs() 绝对值
System.out.println("abs(): " + Math.abs(-5));

输出结果6

六、代码 

public class Day5 {
    public static void main(String[] args){
        /* 静态常量 */
        // 常量E
        Object a = null;
        a = Math.E;
        System.out.println("E 常量的值: " + a);

        // 常量PI
        Object b = null;
        b = Math.PI;
        System.out.println("PI 常量的值: " + b);

        /* 三角函数 */
        // 角度 --> 弧度 toRadians()
        double x = 45; // 45°  45° -->  PI / 4
        System.out.println("45°转换为弧度:" + Math.toRadians(x));
        System.out.println(Math.PI / 4);

        double y = 180;
        System.out.println("180°转换为弧度:" + Math.toRadians(y));
        System.out.println(Math.PI);

        // 弧度 --> 弧度 toDegrees()
        double z = 0.7853981633974483; // PI / 4 -->  45°
        System.out.println("0.7853981633974483转换为角度" + Math.toDegrees(z));

        // 正弦函数sin()
        double degrees = 45.0;
        double radians = Math.toRadians(degrees);
        System.out.println("45° 的正弦值: " + Math.sin(radians));

        // 余弦函数cos()
        System.out.println("45° 的余弦值: " + Math.cos(radians));

        // 反正弦值asin()
        System.out.println("45° 的反正弦值: " + Math.asin(radians));

        // 反余弦值acos()
        System.out.println("45° 的反余弦值: " + Math.acos(radians));

        // 正切值tan()
        System.out.println("45° 的正切值: " + Math.tan(radians));

        // 反正切值atan() atan2()
        double m = 45;
        double n = 30;
        System.out.println("45° 的反正切值1: " + Math.atan(radians)); // atan()
        System.out.println("反正弦值2: " + Math.atan2(m, n)); // atan2() 坐标系表示角的反正切值

        /* 指数函数 */
        double p = 8;
        double q = 3;

        // exp()
        System.out.println("e的6次幂: " + Math.exp(p)); // e^8

        // pow()
        System.out.println("8的3次幂: " + Math.pow(p, q)); // 8^3

        // sqrt()
        System.out.println("8的平方根: " + Math.sqrt(p));

        // cbrt()
        System.out.println("8的立方根: " + Math.cbrt(p)); // 2

        // log()
        System.out.println("ln(8): " + Math.log(p)); // ln(8)

        // log10()
        System.out.println("log10(8): " + Math.log10(p)); // log10(8)

        /* 取整 */
        double d = 100.675;
        double e = 100.500;

        // >=的整数  ceil()
        System.out.println("ceil(100.675): " + Math.ceil(d));

        // <=的整数  floor()
        System.out.println("floor(100.675): " + Math.floor(d));

        // 最近的整数  rint()
        System.out.println("rint(100.675): " + Math.rint(d));
        System.out.println("rint(100.500): " + Math.rint(e));

        // 四舍五入的整数  round()
        System.out.println("round(100.675): " + Math.round(d));
        System.out.println("round(100.500): " + Math.round(e));

        /* 其他 */
        // min() 最小
        System.out.println("min(): " + Math.min(2, 10));

        // max() 最大
        System.out.println("max(): " + Math.max(2, 10));

        // random() 返回一个随机数
        System.out.println("random(): " + Math.random());

        // abs() 绝对值
        System.out.println("abs(): " + Math.abs(-5));
    }
}
  • 13
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值