Math
来源:互联网 发布:怎样在淘宝网购物 编辑:程序博客网 时间:2024/06/05 06:06
参考: Android中Math类Math.floor()、Math.round()及Math.ceil()等方法的使用
android Math 用法大全
Java.lang.Math类
1、Math.floor():Math.floor()表示向下取整:数值变小
Math.floor(-1.1): -2.0Math.floor(-1.5): -2.0Math.floor(-1.6): -2.0Math.floor(0.1): 0.0Math.floor(0.5): 0.0Math.floor(0.6): 0.0Math.floor(1.1): 1.0Math.floor(1.5): 1.0Math.floor(1.6): 1.0
2、Math.round():四舍五入
Math.round(-1.1): -1Math.round(-1.5): -1Math.round(-1.6): -2Math.round(0.1): 0Math.round(0.5): 1Math.round(0.6): 1Math.round(1.1): 1Math.round(1.5): 2Math.round(1.6): 2
3、Math.ceil():Math.ceil()方法就表示向上取整,数值变大,
Math.ceil(-1.1): -1.0Math.ceil(-1.5): -1.0Math.ceil(-1.6): -1.0Math.ceil(0.1): 1.0Math.ceil(0.5): 1.0Math.ceil(0.6): 1.0Math.ceil(1.1): 2.0Math.ceil(1.5): 2.0Math.ceil(1.6): 2.0
4、Math.rint():求距离某数最近的整数(可能比某数大,也可能比它小)
Math.ring()返回double值最接近参数的值,并等于某个整数。
如果两个double值跟整数都同样接近,结果是整数值是偶数。特殊情况:
如果参数值已经等于某个整数,那么结果跟参数一样。
如果参数为NaN或无穷大,正零或负零,那么结果和参数一样。
Math.rint(-1.1): -1.0Math.rint(-1.5): -2.0//取偶数Math.rint(-1.6): -2.0Math.rint(0.1): 0.0Math.rint(0.5): 0.0//取偶数Math.rint(0.6): 1.0Math.rint(1.1): 1.0Math.rint(1.5): 2.0//取偶数Math.rint(1.6): 2.0
5、反正切
正切、余切图:
由图可得:正切tan:一三象限正,二四象限负
反正切、反余切图:
由图可得:反正切atan值域:(-90,90)
atan与atan2区别:
共同点:返回的都是弧度值
不同点:
atan(double x):参数为:直线的斜率即正切值
由于正切可以有两个角度的但它却只返回一个,因为atan的值域是从-90~90 也就是它只处理一四象限,所以一般不用它。
atan2(double y,double x):参数为:Y坐标、X坐标
返回值是此点与原点连线与x轴正方向的夹角,这样它就可以处理四个象限的任意情况了,它的值域相应的也就是-180~180了。常用的不是求过原点的直线的夹角 往往是求一个线段的夹角 这对于atan2就更是如鱼得水了
System.out.println(" Math.tan(Math.PI * 3 / 4)==" + (Math.tan(Math.PI * 3 / 4)));//-1.0000000000000002System.out.println(" Math.tan((-1) * Math.PI / 4)==" + (Math.tan((-1) * Math.PI / 4)));//-0.9999999999999999System.out.println(" Math.tan(Math.PI / 4)==" + (Math.tan(Math.PI / 4)));//0.9999999999999999System.out.println(" Math.tan((-1) * Math.PI * 3 / 4)==" + (Math.tan((-1) * Math.PI * 3 / 4)));//1.0000000000000002System.out.println(" Math.atan(1)==" + Math.toDegrees(Math.atan(1)));//45.0System.out.println(" Math.atan2(0,1)==" + Math.toDegrees(Math.atan2(1, 1)));//45.0System.out.println(" Math.atan2(1,0)==" + Math.toDegrees(Math.atan2(-1, -1)));//-135.0System.out.println(" Math.atan(-1)==" + Math.toDegrees(Math.atan(-1)));//-45.0System.out.println(" Math.atan2(-0,1)==" + Math.toDegrees(Math.atan2(1, -1)));//135.0System.out.println(" Math.atan2(-1,0)==" + Math.toDegrees(Math.atan2(-1, 1)));//-45.0
6、其他:
Math.PI 返回记录的圆周率Math.E 返回记录e的常量Math.abs 返回绝对值Math.sin 返回正弦函数 Math.asin 反正弦函数Math.cos 返回余弦函数 Math.acos 反余弦函数Math.tan 返回正切函数 Math.atan 反正切函数Math.toDegrees 返回弧度转化为角度 Math.toRadians 角度转化为弧度Math.random 返回[0,1)之间的一个随机数Math.IEEEremainder(x,y) 返x%y的值Math.max 求两数中最大Math.min 求两数中最小Math.sqrt(x):平方根Math.pow(x,y):x的y次方Math.exp 求e的任意次方Math.log10 以10为底的对数Math.log 自然对数Math.nextDown(X)比X小0.00001Math.nextUp(X)比X大0.00001Math.addExact(1, 2); 加法运算Math.subtractExact(3, 5);减法运算Math.multiplyExact(2, 4); 乘法运算Math.scalb(6, 3); 表示6 * (2的3次方)
示例:
参考:
Java中产生随机数的两个方法
使用java中的Math进行三角函数的计算
public class TestMath { public static void main(String[] args) { /** * 向下取整,返回double类型 */ System.out.println(" Math.floor(-1.1)==" + Math.floor(-1.1));//-2.0 System.out.println(" Math.floor(-1.5)==" + Math.floor(-1.5));//-2.0 System.out.println(" Math.floor(-1.6)==" + Math.floor(-1.6));//-2.0 System.out.println(" Math.floor(0.1)==" + Math.floor(0.1));//0.0 System.out.println(" Math.floor(0.5)==" + Math.floor(0.5));//0.0 System.out.println(" Math.floor(0.6)==" + Math.floor(0.6));//0.0 System.out.println(" Math.floor(1.1)==" + Math.floor(1.1));//1.0 System.out.println(" Math.floor(1.5)==" + Math.floor(1.5));//1.0 System.out.println(" Math.floor(1.6)==" + Math.floor(1.6));//1.0 /** * 四舍五入,返回long类型 */ System.out.println(" Math.round(-1.1)==" + Math.round(-1.1));//-1 System.out.println(" Math.round(-1.5)==" + Math.round(-1.5));//-1 System.out.println(" Math.round(-1.6)==" + Math.round(-1.6));//-2 System.out.println(" Math.round(0.1)==" + Math.round(0.1));//0 System.out.println(" Math.round(0.5)==" + Math.round(0.5));//1 System.out.println(" Math.round(0.6)==" + Math.round(0.6));//1 System.out.println(" Math.round(1.1)==" + Math.round(1.1));//1 System.out.println(" Math.round(1.5)==" + Math.round(1.5));//2 System.out.println(" Math.round(1.6)==" + Math.round(1.6));//2 /** * 向上取整,返回double类型 */ System.out.println(" Math.ceil(-1.1)==" + Math.ceil(-1.1));//-1.0 System.out.println(" Math.ceil(-1.5)==" + Math.ceil(-1.5));//-1.0 System.out.println(" Math.ceil(-1.6)==" + Math.ceil(-1.6));//-1.0 System.out.println(" Math.ceil(0.1)==" + Math.ceil(0.1));//1.0 System.out.println(" Math.ceil(0.5)==" + Math.ceil(0.5));//1.0 System.out.println(" Math.ceil(0.6)==" + Math.ceil(0.6));//1.0 System.out.println(" Math.ceil(1.1)==" + Math.ceil(1.1));//2.0 System.out.println(" Math.ceil(1.5)==" + Math.ceil(1.5));//2.0 System.out.println(" Math.ceil(1.6)==" + Math.ceil(1.6));//2.0 /** * Math.rint 最接近参数的double值,返回double类型 * 如果有2个数同样接近(尾数为5),则返回偶数的那个。 * 它有两个特殊的情况:1)如果参数本身是整数,则返回本身。2)如果不是数字或无穷大或正负0,则结果为其本身。 */ System.out.println(" Math.rint(-1.1)==" + Math.rint(-1.1));//-1.0 System.out.println(" Math.rint(-1.5)==" + Math.rint(-1.5));//-2.0//同样接近,返回偶数 System.out.println(" Math.rint(-1.6)==" + Math.rint(-1.6));//-2.0 System.out.println(" Math.rint(0.1)==" + Math.rint(0.1));//0.0 System.out.println(" Math.rint(0.5)==" + Math.rint(0.5));//0.0//同样接近,返回偶数 System.out.println(" Math.rint(0.6)==" + Math.rint(0.6));//1.0 System.out.println(" Math.rint(1.1)==" + Math.rint(1.1));//1.0 System.out.println(" Math.rint(1.5)==" + Math.rint(1.5));//2.0//同样接近,返回偶数 System.out.println(" Math.rint(1.6)==" + Math.rint(1.6));//2.0 /*----------------三角函数-----------------------*/ //radians ['reɪdjənz] 弧度; 毫弧度角; //toRadians:将角度转为弧度 //toDegrees: 将弧度转为角度 System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI / 2)); double a0 = Math.toRadians(90);//把数字90 转换成 90度 System.out.println("Math.sin(a0)==" + Math.sin(a0));//计算sin 90度//1.0 double b0 = Math.toRadians(30); System.out.println("Math.cos(b0)==" + Math.cos(b0));//0.8660254037844387 double c0 = Math.toRadians(20); System.out.println("Math.tan(c0)==" + Math.tan(c0));//0.36397023426620234 double pi = Math.PI; System.out.println("pi==" + pi);//3.141592653589793 //因为java中的Math.PI是个近似值, //所以通过这个常量计算的三角函数值也会出现一些偏差,数学中我们sin(pi / 6)=0.5 System.out.println("Math.sin(pi/6)==" + Math.sin(pi / 6));//0.49999999999999994 //sqrt : Square Root(平方根) double x = 9; double y = 25; System.out.println("Math.sqrt(" + x + ")==" + Math.sqrt(x));//3.0 System.out.println("Math.sqrt(" + y + ")==" + Math.sqrt(y));//5.0 //pow : x 的 y 次幂(次方) double x1 = 2; double y1 = 3; System.out.println("Math.sqrt(" + x1 + "," + y1 + ")==" + Math.pow(x1, y1));//5.0 /*----------------------------随机数------------------------*/ //生成[0,100)的随机数,Math.random()=[0,1) double random = Math.random(); System.out.println("random==" + random);//0.10884585712109973 System.out.println("random==" + random * 100);//10.884585712109974,[0,100) System.out.println("random==" + (int) (random * 100));//10,浮点转整型 //结论: //(int)(Math.random()*10)返回0到9的随机数。 //(int)(Math.random()*n) 返回0到n的随机数。 //(int)(Math.random()*100)+1 返回1到100之间的随机数, // 前面返回0到99之间的随机数,加1就成了1到100之间的随机数。 Random rand1 = new Random(); int num1 = rand1.nextInt(100); System.out.println("num1==" + num1);//随机[0,100) Random rand2 = new Random(100); int num2 = rand2.nextInt(); System.out.println("num2==" + num2);//固定:-1193959466 Random rand3 = new Random(); int num3 = rand3.nextInt(); System.out.println("num3==" + num3);//随机[) //结论: //Random random=new Random(); //random.nextInt();返回的是int型的范围的随机数 //random.nextInt(10);返回的是0到9范围的随机数 //所以random.nextInt(100)+1;就生成1到100之间的随机数 //random.nextInt(n)+m;就返回m到m+n-1之间的随机数 /*---------------求整型的范围----------------------*/ //2E9 : 表示2乘以10的9次方,就是2后边加9个零 System.out.println("int 范围==" + Math.pow((-2), 31) + " ~ " + (Math.pow((2), 31) - 1));//-2.147483648E9 ~ 2.147483647E9 System.out.println("int 范围==" + Integer.MIN_VALUE + " ~ " + Integer.MAX_VALUE);//-2147483648 ~ 2147483647 /*---------------浮点转整型、拆箱、装箱----------------------*/ //浮点型(float , double)转整型(int , long) : 去掉小数部分 int a = (int) 1.23456; float b = 1.23456f;//float需要带f标识 double c = 1.23456;//默认double long d = (int) 1.23456; System.out.println(" int==" + a);//1 System.out.println(" float==" + b);//1.23456 System.out.println(" double==" + c);//1.23456 System.out.println(" d==" + d);//1 //valueOf:返回包装类,可判断是否为null(对象) //parseFloat:返回基本数据类型,可判断是否等于某个值 //装箱,由基本数据类型构造出一个包装类的对象。 //拆箱,由一个包装类对象转换到相应的基本数据类型。 Float aFloat = Float.valueOf("1.23456");//装箱,等价于:new Float(1.23456); System.out.println(" aFloat==" + aFloat + " " + (aFloat != null));//1.23456 true float aFloat1 = Float.valueOf("1.23456");//拆箱,将Float自动转为float System.out.println(" aFloat1==" + aFloat1 + " " + (aFloat1 != 1.23456));//1.23456 true System.out.println(" aFloat1==" + aFloat1 + " " + (aFloat1 != 1.23456f));//1.23456 false float parseFloat = Float.parseFloat("1.23456"); System.out.println(" parseFloat==" + parseFloat);//1.23456 Boolean aBoolean = true; if (aBoolean) {//拆箱,将Float自动转为float System.out.println("==aBoolean=="); } boolean bool = true; if (bool) { System.out.println("==bool=="); } }}
阅读全文
0 0
- Math
- math
- Math
- Math
- Math
- Math
- math
- Math
- math
- Math
- Math
- Math
- Math
- Math
- Math
- Math
- Math
- Math
- 用OpenCV的GaussianBlur函数做高斯滤波
- Spark官网资料学习网址
- 学会Nginx,10倍提升你的Web应用性能!
- 马云:要是哭有用我每天哭
- Java工程师为何如此火爆?
- Math
- 访问网络的Json和图片
- 仿微信和QQ多图合并框架实现
- angularjs表格增强版增删改查排序敏感字和敏感字替换#(商品)
- Java字母加数字组合比较大小
- 底部栏加Fragment
- 快速排序
- Git提交代码到远程仓库
- effective C++ 学习笔记——4 设计与声明