Java 中 Math 类常用的方法有: Math.ceil() 、 Math.floor() 、 Math.round() 、 Math.abs() 、 Math.sqrt() 、 Math.pow() ; Java 中 Math 类中定义的特殊数据有: Math.PI( 圆周率 ) 、 Math.E( 自然数 ) ; Java 的 1.6 中文版 API 下载地址为: Java 的 1.6 中文版 API 下载地址 package Test_02; import java.util.Random; public class T03_Math { public static void main(String[] args) { System.out.println(Math.ceil(3.2)); // 返回值: 4.0 , 返回最小的(最接近负无穷大) double 值,该值大于等于参数,并等于某个整数。 System.out.println(Math.floor(3.2)); // 返回值: 3.0 ,返回最大的(最接近正无穷大) double 值,该值小于等于参数,并等于某个整数。 System.out.println(Math.round(3.2)); // 返回值: 3 ,返回最接近参数的 int 。 ( 四舍五入 ) System.out.println(Math.round(3.8)); // 返回值: 4 ,返回最接近参数的 int 。 ( 四舍五入 ) System.out.println("--------------------------------"); System.out.println(Math.abs(-45)); // 返回值: 45 ,求取绝对值 System.out.println(Math.sqrt(64)); // 返回值: 8.0 ,返回正确舍入的 double 值的正平方根。 System.out.println(Math.pow(5, 2)); // 返回值: 25.0 , 返回第一个参数的第二个参数次幂的值。 System.out.println(Math.pow(2, 5)); // 返回值: 32.0 , System.out.println("--------------------------------"); System.out.println(Math.PI); // 返回值: 3.141592653589793 ,返回圆周率Π System.out.println(Math.E); // 返回值: 2.718281828459045 ,返回比任何其他值都更接近 e (即自然对数的底数)的 double 值。 System.out.println(Math.exp(2)); // 返回值: 7.38905609893065 ,返回 返回欧拉数 e 的 double 次幂的值。 System.out.println("--------------------------------"); } } 注意:仔细体味下段代码中内含的“奇妙 ”,品味 Random 类于 Math 类在处理随机数中的异同。 package Test_03; import java.util.Random; public class T03_MathAndRandom { public static void main(String[] args) { // 调用 Math 类中方法返回 0.0 —— 1.0 之间均匀分布的 double 值。 double d = Math.random(); System.out.println(d); System.out.println("--------------------------------"); int a1 = (int)(10 * Math.random()); System.out.println(a1); int a2 = 20 + (int)(10 * Math.random()); System.out.println(a2); System.out.println("--------------------------------"); // 调用 Random 类中方法返回 0.0 —— 1.0 之间均匀分布的 double 值。 Random ran = new Random(); double d2 = ran.nextDouble(); System.out.println(d2); int a3 = ran.nextInt(10); System.out.println(a3); int a4 = 20 + ran.nextInt(10); System.out.println(a4); } } public class Demo{ public static void main(String args[]){ /** */ 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 /** */ System.out.println(Math.abs(-10.4)); //10.4 System.out.println(Math.abs(10.1)); //10.1 /** */ 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 /** */ 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 的随机数 [0,1) */ 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 类型的数 /** */ 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 加 0.50 向下取整, 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 } } /** */ System.out.println("------>" + Math.E);//2.718281828459045 System.out.println("------>" + Math.PI);//3.141592653589793 /** */ System.out.println("------>" + Math.abs(-3));//3 /** * atan2(y,x) 求向量 (x,y) 与 x 轴夹角 */ System.out.println("------>" + Math.acos(1)); System.out.println("------>" + Math.acos(-1)); /** */ System.out.println("------>" + Math.cbrt(-1));//-1.0 System.out.println("------>" + Math.cbrt(1));//1.0 System.out.println("------>" + Math.cbrt(0.5));//0.7937005259840998 System.out.println("------>" + Math.cbrt(5));//1.709975946676697 /** */ System.out.println("------>" + Math.sqrt(4.0));//2.0 /** * Math.hypot(x,y) 求 sqrt(x*x+y*y) 在求两点间距离时有用 sqrt((x1-x2)^2+(y1-y2)^2) */ System.out.println("------>" + Math.hypot(3.0, 4.0));//5.0 /** */ System.out.println("------1>" + Math.ceil(7.2));//8.0 System.out.println("------2>" + Math.ceil(7.5));//8.0 System.out.println("------3>" + Math.ceil(7.6));//8.0 System.out.println("------4>" + Math.ceil(-7.2));//-7.0 System.out.println("------5>" + Math.ceil(-7.5));//-7.0 System.out.println("------6>" + Math.ceil(-7.6));//-7.0 /** */ System.out.println("------1>" + Math.floor(7.2));//7.0 System.out.println("------2>" + Math.floor(7.5));//7.0 System.out.println("------3>" + Math.floor(7.6));//7.0 System.out.println("------4>" + Math.floor(-7.2));//-8.0 System.out.println("------5>" + Math.floor(-7.5));//-8.0 System.out.println("------6>" + Math.floor(-7.6));//-8.0 /** * Math.cosh() 返回 double 值的双曲线余弦。 x 的双曲线余弦的定义是 (ex + e-x)/2 ,其中 e 是欧拉数 */ System.out.println("------>" + Math.cosh(1));//1.543080634815244 System.out.println("------>" + Math.cosh(0));//1.0 /** */ System.out.println("------>" + Math.exp(2));//7.38905609893065 System.out.println("------>" + Math.expm1(2));//6.38905609893065 System.out.println("------>" + Math.pow(2.0, 3.0));//8.0 /** * Math.log(a) a 的自然对数 ( 底数是 e) * Math.log10(a) a 的底数为 10 的对数 * 值得注意的是,前面其他函数都有重载,对数运算的函数只能传 double 型数据并返回 double 型数据 */ System.out.println("------1>" + Math.log(Math.E));//1.0 System.out.println("------2>" + Math.log10(10));//1.0 System.out.println("------3>" + Math.log1p(Math.E - 1.0));//1.0 /** */ System.out.println("------1>" + Math.max(1, 2));//2 System.out.println("------2>" + Math.min(1, -2));//-2 /** * Math.nextAfter() 返回与第二个参数方向相邻的第一个参数的浮点数。 */ System.out.println("------1>" + Math.nextAfter(-1, 2));//-0.99999994 System.out.println("------2>" + Math.nextAfter(1, 2));//1.0000001 /** * Math.nextUp() 返回与正无穷大方向相邻的 d 的浮点值。 */ System.out.println("------>" + Math.nextUp(1));//1.0000001 System.out.println("------>" + Math.nextUp(-1));//-0.99999994 /** * Math.Random() 函数能够返回带正号的 double 值,该值大于等于 0.0 且小于 1.0 ,即取值范围是 [0.0,1.0) 的左闭右开区间, * 返回值是一个伪随机选择的数,在该范围内(近似)均匀分布 */ System.out.println("------>" + Math.random());// 取值范围是 [0.0,1.0) 的随机数 /** * Math.rint(x) : x 取整为它最接近的整数,如果 x 与两个整数的距离相等,则返回其中为偶数的那一个。 */ System.out.println("------>" + Math.rint(3.5));//4.0 System.out.println("------>" + Math.rint(4.5));//4.0 System.out.println("------>" + Math.rint(3.1));//3.0 System.out.println("------>" + Math.rint(4.1));//4.0 System.out.println("------>" + Math.rint(3.7));//4.0 System.out.println("------>" + Math.rint(4.7));//5.0 /** * Math.round(x) :返回 Math.floor(x+0.5) ,即“四舍五入”值。 */ System.out.println("------>" + Math.round(3.5));//4 System.out.println("------>" + Math.round(4.5));//5 System.out.println("------>" + Math.round(3.1));//3 System.out.println("------>" + Math.round(4.7));//5 /** * Math.scalb(double d, int scaleFactor), 返回 f × 2scaleFactor ,其舍入方式如同将一个正确舍入的浮点值乘以 float * 返回 d 和正无穷大之间与 d 相邻的浮点值 tatic double nextUp(double d) */ System.out.println("------1>" + Math.scalb(1.5d, 6));//96 /** * Math.scalb(float f, int scaleFactor) * 返回 f 和正无穷大之间与 f 相邻的浮点值 tatic float nextUp(float f) * 返回第一个参数和第二个参数之间与第一个参数相邻的浮点数 */ System.out.println("------2>" + Math.scalb(1.5f, 6));//96 /** * Math.signum 方法返回指定 int 值的符号函数(如果指定值为负,则返回 -1 ;如果指定值为零,则返回 0 ;如果指定值为正,则返回 1 )。 */ System.out.println("------1>" + Math.signum(10));//1.0 System.out.println("------2>" + Math.signum(-10));//-1.0 System.out.println("------3>" + Math.signum(0));//0.0 /** * Math.toDegrees() 将弧度转换角度 */ System.out.println("------3>" + Math.toDegrees(1.57));//89.95437383553926 /** * Math.toRadians() 将角度转换为弧度 */ System.out.println("------3>" + Math.toRadians(90));//1.5707963267948966 /** * Math.ulp() * 如果要理解什么是 ulp(unit in the last place or unit of least precision (ULP)) * 先要了解在外汇返佣 http://www.kaifx.cn/ 计算机中保存的数和我们在数学上认为的数是不一样的; * 比方说 2.0 和 3.0 之间有多少个数,在数学中是无限的,但是在计算机中是有限的, * 因为计算机需要用一堆字节来表示 double 或者 float, 但是因为计算机表示不了无限的数(因为没有无限内存)。 * * 所以就有了 ulp ,假设在 float 2.0 和 3.0 之间有 8,388,609 个数,那么在 2.0 和 3.0 之间的数的 ulp 就是 8,388,609/1.0 约等于 0.0000001 。 * * 你如果想知道某一个具体的 double 或 float 的先一个或者上一个数字是什么可以使用函数 * public static double nextAfter(float start, float direction) * public static double nextAfter(double start, double direction) */ /** */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { /** * Math.addExact() 求和,如果结果溢出取值范围,则引发异常。 */ System.out.println("------>" + Math.addExact(1, 2));//3 /** * Math.substractExact() 方法返回两个参数之差,结果溢出时抛出 ArithmeticException */ System.out.println("------>" + Math.subtractExact(100, 50));//50 /** * Math.incrementExact() 方法 返回参数值加一,结果溢出时抛出 ArithmeticException */ System.out.println("------>" + Math.incrementExact(100));//101 /** * Math.decrementExact() 方法 返回参数值减一,结果溢出时抛出 ArithmeticException */ System.out.println("------>" + Math.decrementExact(100));//99 /** * Math.multiplyExact() 方法 返回两个参数之积,结果溢出时抛出 ArithmeticException */ System.out.println("------>" + Math.multiplyExact(100, 5));//500 /** * Math.negateExact() 方法 改变参数符号,结果溢出时抛出 ArithmeticException */ System.out.println("------>" + Math.negateExact(100));//-100 /** * Math.floorDiv(1,2) 第一个参数除以第二参数,然后针对结果执行 floor 操作,返回小于或等于商的整数: */ System.out.println("------>" + Math.floorDiv(7, 3));//2 System.out.println("------>" + Math.floorDiv(-7, 3));//-3 /** * Math.floorMod() * 1 、如果参数的符号相同,则 floorMod 和%运算符的结果是相同的。 * 2 、如果参数的符号不同,则结果与%运算符不同。 */ // 如果参数的符号相同,则 floorMod 和%运算符的结果是相同的。 System.out.println("------1>" + Math.floorMod(4, 3));//1 System.out.println("------2>" + (4 % 3));//1 System.out.println("------3>" + Math.floorMod(4, -3));//-2 System.out.println("------4>" + (4 % -3));//1 System.out.println("------5>" + Math.floorMod(-4, 3));//2 System.out.println("------6>" + (-4 % 3));//-1 System.out.println("------7>" + Math.floorMod(-4, -3));//-1 System.out.println("------8>" + (-4 % -3));//-1 /** * Math.toIntExact(),long 转 int */ System.out.println("------1>" + Math.toIntExact(1)); /** * Math.nextDown() 返回与负无穷大方向相邻的 f 的浮点值。 */ System.out.println("------>" + Math.nextDown(1));//0.99999994 System.out.println("------>" + Math.nextDown(-1));//-1.0000001 } 更多技术资讯可关注:itheimaGZ获取 |