阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Math类

99次阅读
没有评论

共计 2857 个字符,预计需要花费 8 分钟才能阅读完成。

1、Math 概述

Java 的 Math 类封装了很多与数学有关的属性和方法。

2、Math 常用方法

2.1、常用属性

public static final Double E = 2.7182818284590452354

public static final Double PI = 3.14159265358979323846

public class MathDemo01 {public static void main(String[] args) {System.out.println(Math.E); System.out.println(Math.PI); } }

运行结果:

Math 类

2.2、算术计算

public static long abs(double x):传回 x 的绝对值。X 也可 int long float

public static long pow(double x,double y):传回 x 的 y 次幂值

public static long sqrt(double x): 传回 x 开平方值

public static long max(double x,double y):传回 x、y 较大数

public static long min(double x,double y):传回 x、y 较小数

public class MathDemo02 {public static void main(String[] args) {/** *Math.sqrt()// 计算平方根 *Math.pow(a, b)// 计算 a 的 b 次方 *Math.max(,);// 计算最大值 *Math.min(,);// 计算最小值 */ System.out.println(Math.sqrt(16)); //4.0 System.out.println(Math.pow(3,2)); //9.0 System.out.println(Math.max(2.3,4.5));//4.5 System.out.println(Math.min(2.3,4.5));//2.3 /** * abs 求绝对值 */ System.out.println(Math.abs(-10.4)); //10.4 System.out.println(Math.abs(10.1)); //10.1 } }

运行结果:

Math 类

2.3、进位计算

public static long floor(double x):传回不大于 x 的最大整数值,向下取整

public static long ceil(double x):传回不小于 x 的最小整数值,向上取整

public static long round(double x): 传回 x 的四舍五入值

public class MathDemo03 {public static void main(String[] args) {//ceil 天花板的意思,就是返回大的值 System.out.println(Math.ceil(-10.1)); //-10.0 System.out.println(Math.ceil(10.7)); //11.0 System.out.println(Math.ceil(-0.7)); //-0.0 System.out.println(Math.ceil(0.0)); //0.0 System.out.println(Math.ceil(-0.0)); //-0.0 System.out.println(Math.ceil(-1.7)); //-1.0 // floor 地板的意思,就是返回小的值 System.out.println(Math.floor(-10.1)); //-11.0 System.out.println(Math.floor(10.7)); //10.0 System.out.println(Math.floor(-0.7)); //-1.0 System.out.println(Math.floor(0.0)); //0.0 System.out.println(Math.floor(-0.0)); //-0.0 // round 四舍五入,float 时返回 int 值,double 时返回 long 值 System.out.println(Math.round(10.1)); //10 System.out.println(Math.round(10.7)); //11 System.out.println(Math.round(10.5)); //11 System.out.println(Math.round(10.51)); //11 System.out.println(Math.round(-10.5)); //-10 System.out.println(Math.round(-10.51)); //-11 System.out.println(Math.round(-10.6)); //-11 System.out.println(Math.round(-10.2)); //-10 } }

运行结果:

Math 类

2.4、随机数

public static long random(): 传回随机数值, 产生一个 0 - 1 之间的随机数 ( 不包括 0 和 1)

public class MathDemo04 {public static void main(String[] args) {//random 取得一个大于或者等于 0.0 小于不等于 1.0 的随机数 System.out.println(Math.random()); // 小于 1 大于 0 的 double 类型的数 System.out.println(Math.random()*2);// 大于 0 小于 2 的 double 类型的数 System.out.println(Math.random()*2+1);// 大于 1 小于 3 的 double 类型的数 } }

运行结果:

Math 类

2.5、其他

public static long exp(double x):传回相当于 e^x

public static long log(double x):传回 x 的自然对数函数值

public static long rint(double x): 传回最接近 x 的整数值

public static long toDegrees(double angrad): 传回将 angrad 径度转换成角度

public static long toRadians(double angdeg): 传回将 angdeg 角度转换成径度

public static long sin(double x): 传回 x 径度的正弦函数值

public static long cos(double x):传回 x 径度的余弦函数值

public static long tan(double x): 传回 x 径度的正切函数值

public static long asin(double x):传回 x 值的反正弦函数值。

public static long acos(double x):传回 x 值的反余弦函数值。

public static long atan(double x):传回 x 值的反正切函数值。

public static long atan2(double x, double y):传回极坐标(polar)的 θ 值

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-06-06发表,共计2857字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中