Math类中常用函数

//闲来无事重新学习一下java中的Math类的函数,顺便总结一下常用的函数,不多说直接上代码和注释以及结果截图

public class MathDemo{
	public static void main(String[] args){
		//PI E属于Math的属性
		System.out.println("PI: "+Math.PI);
		System.out.println("E: "+Math.E);
		System.out.println("<--------------------------------------->");
		//abs求绝对值的函数    可以有多种重载,整型浮点型都可以,这里随便举两个例子。
		System.out.println("Math.abs(3.2): "+Math.abs(3.2));
		System.out.println("Math.abs(-3.2): "+Math.abs(-3.2));
		System.out.println("<--------------------------------------->");
		//ceil可以认为是求在坐标轴上,这个浮点数向右最近的整数。
		System.out.println("Math.ceil(3.2): "+Math.ceil(3.2));
		System.out.println("Math.ceil(-2.1): "+Math.ceil(-2.1));
		System.out.println("Math.ceil(3.9): "+Math.ceil(3.9));
		System.out.println("<--------------------------------------->");
		//floor舍弃小数部分,可以认为是求坐标轴上向左取整的数
		System.out.println("Math.floor(3.9): "+Math.floor(3.9));
		System.out.println("Math.floor(3.2): "+Math.floor(3.2));
		System.out.println("Math.floor(-2.1): "+Math.floor(-2.1));
		System.out.println("<--------------------------------------->");
		//round正常的四舍五入
		System.out.println("Math.round(3.2): "+Math.round(3.2));
		System.out.println("Math.round(3.5): "+Math.round(3.5));
		System.out.println("<--------------------------------------->");
		//max min求两个数的最大最小,有多种重载
		System.out.println("Math.max(1,6): "+Math.max(1,6));
		System.out.println("Math.min(1,6): "+Math.min(1,6));
		System.out.println("<--------------------------------------->");
		//random求一个随机数,不过范围只能在0.0~1.0之间,想要其他形式的随机数找java.util.Random 
		System.out.println("Math.random(): "+Math.random());
		System.out.println("<--------------------------------------->");
		//sqrt()求平方根
		System.out.println("Math.sqrt(4): "+Math.sqrt(4));
		System.out.println("Math.sqrt(-4): "+Math.sqrt(-4));
		System.out.println("<--------------------------------------->");
		//pow()求x的y次幂
		System.out.println("Math.pow(2,3): "+Math.pow(2,3));
		System.out.println("<--------------------------------------->");
		//sin(),cos();三角函数,这里只举两个例子,所有三角函数在Math中都有
		System.out.println("Math.sin(0): "+Math.sin(0));
		System.out.println("Math.cos(0): "+Math.cos(0));
		System.out.println("<--------------------------------------->");
	}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello689

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值