从屌丝到架构师的飞越(API篇)-Math类

一、介绍

Java 的 Math 包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。

Math类位于Java.lang包中,包含用于执行基本数学运算的方法!Math类的所有执行方法都是静态方法,可以直接使用类名.方法名调用。

Math类是一个唯一(final)类或工具类,他提供了我们所需要的所有的数学方法及属性,比如:我们的笔记本电脑,他就是一个工具,我们拥有了他就可以直接去使用,我们可以使用他里面的office软件、聊天软件、听歌软件等,还有他本身的显示器,键盘,鼠标等。

二、知识点介绍

1、Math类中的常用方法

三、上课对应视频的说明文档

1、Math类中的常用方法

public static final Double E = 2.7182818284590452354

public static final Double PI = 3.14159265358979323846

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 static long floor(double x):传回不大于x的最大整数值 ,向下取整

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

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

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

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)的θ值

案例一:

public class Demo{

public static void main(String args[]){ 

/**

*Math.sqrt()//计算平方根

*Math.cbrt()//计算立方根

*Math.pow(a, b)//计算a的b次方

*Math.max( , );//计算最大值

*Math.min( , );//计算最小值

*/ 

System.out.println(Math.sqrt(16));  //4.0

System.out.println(Math.cbrt(8));    //2.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 

/**

* 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 

/**

* random 取得一个大于或者等于0.0小于不等于1.0的随机数

*/ 

System.out.println(Math.random());  //小于1大于0的double类型的数

System.out.println(Math.random()*2);//大于0小于1的double类型的数

System.out.println(Math.random()*2+1);//大于1小于2的double类型的数

/**

* rint 四舍五入,返回double值

* 注意.5的时候会取偶数    异常的尴尬=。=

*/ 

System.out.println(Math.rint(10.1));    //10.0 

System.out.println(Math.rint(10.7));    //11.0 

System.out.println(Math.rint(11.5));    //12.0 

System.out.println(Math.rint(10.5));    //10.0 

System.out.println(Math.rint(10.51));  //11.0 

System.out.println(Math.rint(-10.5));  //-10.0 

System.out.println(Math.rint(-11.5));  //-12.0 

System.out.println(Math.rint(-10.51));  //-11.0 

System.out.println(Math.rint(-10.6));  //-11.0 

System.out.println(Math.rint(-10.2));  //-10.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 

}

案例二:

public class Test{ 

public static void main(String[] args) { 

// TODO Auto-generated method stub 

System.out.println(Math.E);//比任何其他值都更接近 e(即自然对数的底数)的 double 值。 

System.out.println(Math.PI);//比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。 

/*

* 1.abs绝对值函数

* 对各种数据类型求绝对值

*/ 

System.out.println(Math.abs(-10));//输出10 

/*

* 2.三角函数与反三角函数

* cos求余弦

* sin求正弦

* tan求正切

* acos求反余弦

* asin求反正弦

* atan求反正切

* atan2(y,x)求向量(x,y)与x轴夹角

*/ 

System.out.println(Math.acos(-1.0));//输出圆周率3.14... 

System.out.println(Math.atan2(1.0, 1.0));//输出 π/4 的小数值 

/*

* 3.开根号

* cbrt(x)开立方

* sqrt(x)开平方

* hypot(x,y)求sqrt(x*x+y*y)在求两点间距离时有用sqrt((x1-x2)^2+(y1-y2)^2)

*/ 

System.out.println(Math.sqrt(4.0));//输出2.0 

System.out.println(Math.cbrt(8.0));//输出2.0 

System.out.println(Math.hypot(3.0, 4.0));//输出5.0 

/*

* 4.最值

* max(a,b)求最大值

* min(a,b)求最小值

*/ 

System.out.println(Math.max(1, 2));//输出2 

System.out.println(Math.min(1.9, -0.2));//输出-0.2 

/*

* 5.对数

* log(a) a的自然对数(底数是e)

* log10(a) a 的底数为10的对数

* log1p(a) a+1的自然对数

* 值得注意的是,前面其他函数都有重载,对数运算的函数只能传double型数据并返回double型数据

*/ 

System.out.println(Math.log(Math.E));//输出1.0 

System.out.println(Math.log10(10));//输出1.0 

System.out.println(Math.log1p(Math.E-1.0));//输出1.0 

/*

* 6.幂

* exp(x) 返回e^x的值

* expm1(x) 返回e^x - 1的值

* pow(x,y) 返回x^y的值

* 这里可用的数据类型也只有double型

*/ 

System.out.println(Math.exp(2));//输出E^2的值 

System.out.println(Math.pow(2.0, 3.0));//输出8.0 

/*

* 7.随机数

* random()返回[0.0,1.0)之间的double值

* 这个产生的随机数其实可以通过*x控制

* 比如(int)(random*100)后可以得到[0,100)之间的整数

*/ 

System.out.println((int)(Math.random()*100));//输出[0,100)间的随机数 

/*

* 8.转换

* toDegrees(a) 弧度换角度

* toRadians(a) 角度换弧度

*/ 

System.out.println(Math.toDegrees(Math.PI));//输出180.0 

System.out.println(Math.toRadians(180));//输出 π 的值 

/*

* 9.其他

*/ 

//copySign(x,y) 返回 用y的符号取代x的符号后新的x值 

System.out.println(Math.copySign(-1.0, 2.0));//输出1.0 

System.out.println(Math.copySign(2.0, -1.0));//输出-2.0 

//ceil(a) 返回大于a的第一个整数所对应的浮点数(值是整的,类型是浮点型) 

//可以通过强制转换将类型换成整型 

System.out.println(Math.ceil(1.3443));//输出2.0 

System.out.println((int)Math.ceil(1.3443));//输出2 

//floor(a) 返回小于a的第一个整数所对应的浮点数(值是整的,类型是浮点型) 

System.out.println(Math.floor(1.3443));//输出1.0 

//rint(a) 返回最接近a的整数的double值 

System.out.println(Math.rint(1.2));//输出1.0 

System.out.println(Math.rint(1.8));//输出2.0 

//nextAfter(a,b) 返回(a,b)或(b,a)间与a相邻的浮点数 b可以比a小 

System.out.println(Math.nextAfter(1.2, 2.7));//输出1.2000000000000002 

System.out.println(Math.nextAfter(1.2, -1));//输出1.1999999999999997   

//所以这里的b是控制条件 

//nextUp(a) 返回比a大一点点的浮点数 

System.out.println(Math.nextUp(1.2));//输出1.2000000000000002 

//nextDown(a) 返回比a小一点点的浮点数 

System.out.println(Math.nextDown(1.2));//输出1.1999999999999997   

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容