-
Math
2015-12-21 23:21:55Mathjava.math.Math类常用的常量和方法:
Math.PI 记录的圆周率 Math.E记录e的常量 Math.abs 求绝对值 Math.sin 正弦函数 Math.asin 反正弦函数 Math.cos 余弦函数 Math.acos 反余弦函数 Math.tan 正切函数 Math.atan 反正切函数 Math.atan2 商的反正切函数 Math.toDegrees 弧度转化为角度 Math.toRadians 角度转化为弧度 Math.ceil 得到不小于某数的最大整数 Math.floor 得到不大于某数的最大整数
例如:Math.floor(12.7) =12.0
Math.ceil(12.7) =13.0
ceil()是天花板,即向上取整。floor是地板,向下取整。round是四舍五入。Math.IEEEremainder 求余 Math.max 求两数中最大 Math.min 求两数中最小 Math.sqrt 求开方 Math.pow 求某数的任意次方, 抛出ArithmeticException处理溢出异常 Math.sqrt(x):平方根 Math.pow(x,y):x的y次方
Math.exp 求e的任意次方 Math.log10 以10为底的对数 Math.log 自然对数 Math.rint 求距离某数最近的整数(可能比某数大,也可能比它小) Math.round 同上,返回int型或者long型(上一个函数返回double型) Math.random 返回0,1之间的一个随机数
java.math.BigInteger(大整数):
BigInteger bi1=new BigInteger("1234567890123456890"); BigInteger bi2=BigInteger.valueOf(123L); bi1=bi1.add(bi2);//b1+b2 bi1=bi1.multiply(bi2);//b1*b2 bi1=bi1.subtract(bi2);//b1-b2 bi1=bi1.divide(bi2);// b1/b2 java.math.BigDecimal(大浮点数): BigDecimal bd = new BigDecimal("3.1415926"); bd = bd.setScale(2,BigDecimal.ROUND_DOWN);//取3.1415926小数点后面二位
-
math
2016-06-08 12:15:31函数名 ...math.pi 3.1415926535898 abs 取绝对值 math.abs(-2012) 2012 ceil 向上取整 math.ceil(9.1) 10 floor 向下取整 math.floor(9.9)函数名 描述 示例 结果 pi 圆周率 math.pi 3.1415926535898 abs 取绝对值 math.abs(-2012) 2012 ceil 向上取整 math.ceil(9.1) 10 floor 向下取整 math.floor(9.9) 9 max 取参数最大值 math.max(2,4,6,8) 8 min 取参数最小值 math.min(2,4,6,8) 2 pow 计算x的y次幂 math.pow(2,16) 65536 sqrt 开平方 math.sqrt(65536) 256 mod 取模 math.mod(65535,2) 1 modf 取整数和小数部分 math.modf(20.12) 20 0.12 randomseed 设随机数种子 math.randomseed(os.time()) random 取随机数 math.random(5,90) 5~90 rad 角度转弧度 math.rad(180) 3.1415926535898 deg 弧度转角度 math.deg(math.pi) 180 exp e的x次方 math.exp(4) 54.598150033144 log 计算x的自然对数 math.log(54.598150033144) 4 log10 计算10为底,x的对数 math.log10(1000) 3 frexp 将参数拆成x * (2 ^ y)的形式 math.frexp(160) 0.625 8 ldexp 计算x * (2 ^ y) math.ldexp(0.625,8) 160 sin 正弦 math.sin(math.rad(30)) 0.5 cos 余弦 math.cos(math.rad(60)) 0.5 tan 正切 math.tan(math.rad(45)) 1 asin 反正弦 math.deg(math.asin(0.5)) 30 acos 反余弦 math.deg(math.acos(0.5)) 60 atan 反正切 math.deg(math.atan(1)) 45 函数名 描述 示例 结果 pi 圆周率 math.pi 3.1415926535898 abs 取绝对值 math.abs(-2012) 2012 ceil 向上取整 math.ceil(9.1) 10 floor 向下取整 math.floor(9.9) 9 max 取参数最大值 math.max(2,4,6,8) 8 min 取参数最小值 math.min(2,4,6,8) 2 pow 计算x的y次幂 math.pow(2,16) 65536 sqrt 开平方 math.sqrt(65536) 256 mod 取模 math.mod(65535,2) 1 modf 取整数和小数部分 math.modf(20.12) 20 0.12 randomseed 设随机数种子 math.randomseed(os.time()) random 取随机数 math.random(5,90) 5~90 rad 角度转弧度 math.rad(180) 3.1415926535898 deg 弧度转角度 math.deg(math.pi) 180 exp e的x次方 math.exp(4) 54.598150033144 log 计算x的自然对数 math.log(54.598150033144) 4 log10 计算10为底,x的对数 math.log10(1000) 3 frexp 将参数拆成x * (2 ^ y)的形式 math.frexp(160) 0.625 8 ldexp 计算x * (2 ^ y) math.ldexp(0.625,8) 160 sin 正弦 math.sin(math.rad(30)) 0.5 cos 余弦 math.cos(math.rad(60)) 0.5 tan 正切 math.tan(math.rad(45)) 1 asin 反正弦 math.deg(math.asin(0.5)) 30 acos 反余弦 math.deg(math.acos(0.5)) 60 atan 反正切 math.deg(math.atan(1)) 45
print("floor",math.floor(1.2)) print("floor",math.floor(-1.2)) print("random",math.random()) print("random",math.random(1,10)) print("min",math.min(5,10))
结果为:floor 1
floor -2
random 0~1直接的一个随机小数
random 1~10之间的一个随机整数
-
math.pow()函数用法
2018-07-15 23:22:56Math.pow(底数,几次方) 如:int a=3; int b=3; int c=Math.pow(a,b); 就是3的三次方是多少; c最终为27; 基础用法:用math.pow()实现数组的交错求和 int ant=0; a+=b[i]*math.pow(-1,ant); //实...Math.pow(底数,几次方)
如:int a=3;
int b=3;
int c=(int)Math.pow(a,b);
就是3的三次方是多少;c最终为27;
基础用法:用math.pow()实现数组的交错求和
int ant=0;
a+=b[i]*math.pow(-1,ant); //实现b数组的交错求和并放在a中
结果a=b[0]+b[1]-b[2]+b[3]-b[4]....
可能导致错误的情况:
如果底数 x 为负数并且指数 y 不是整数,将会导致 domain error 错误。
如果底数 x 和指数 y 都是 0,可能会导致 domain error 错误,也可能没有;这跟库的实现有关。
如果底数 x 是 0,指数 y 是负数,可能会导致 domain error 或 pole error 错误,也可能没有;这跟库的实现有关。如果返回值太大或者太小,将会导致 range error 错误。
------------------------------------------------------------------------------------------
这里 int c=(int)Math.pow(a,b) 中添加了一个(int),这是强制类型转换(cast),
之所以要用是因为Math.pow(a,b) 的计算结果返回是double类型,double类型转换为int类型就需要用到。
我们可以看看如果不加(int) ,程序是会提示你去 Cast to 'int'
-
Math类常用方法大全
2018-06-12 15:18:051.简介Java的Math类封装了很多与数学有关的属性和方法。2.举例说明代码:private void mathMethod(){ /** * Math.sqrt()//计算平方根 * Math.cbrt()//计算立方根 * Math.hypot(x,y)//计算 (x的平方+y的平方)的...一.简介
Java的Math类封装了很多与数学有关的属性和方法。
二.举例说明
代码
private void mathMethod(){ // Math.sqrt()//计算平方根 Math.cbrt()//计算立方根 Math.hypot(x,y)//计算 (x的平方+y的平方)的平方根 Log.d("TAG","Math.sqrt(16)----:"+Math.sqrt(16));//4.0 Log.d("TAG","Math.cbrt(8)----:"+Math.cbrt(8));//2.0 Log.d("TAG","Math.hypot(3,4)----:"+Math.hypot(3,4));//5.0 // Math.pow(a,b)//计算a的b次方 Math.exp(x)//计算e^x的值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.pow(3,2)----:"+Math.pow(3,2));//9.0 Log.d("TAG","Math.exp(3)----:"+Math.exp(3));//20.085536923187668 //Math.max();//计算最大值 Math.min();//计算最小值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.max(2.3,4.5)----:"+Math.max(7,15));//15 Log.d("TAG","Math.min(2.3,4.5)----:"+Math.min(2.3,4.5));//2.3 //Math.abs求绝对值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.abs(-10.4)----:"+Math.abs(-10.4));//10.4 Log.d("TAG","Math.abs(10.1)----:"+Math.abs(10.1));//10.1 //Math.ceil天花板的意思,就是返回大的值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.ceil(-10.1)----:"+Math.ceil(-10.1));//-10.0 Log.d("TAG","Math.ceil(10.7)----:"+Math.ceil(10.7));//11.0 Log.d("TAG","Math.ceil(-0.7)----:"+Math.ceil(-0.7));//-0.0 Log.d("TAG","Math.ceil(0.0)----:"+Math.ceil(0.0));//0.0 Log.d("TAG","Math.ceil(-0.0)----:"+Math.ceil(-0.0));//-0.0 Log.d("TAG","Math.ceil(-1.7)----:"+Math.ceil(-1.7));//-1.0 //Math.floor地板的意思,就是返回小的值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.floor(-10.1)----:"+Math.floor(-10.1));//-11.0 Log.d("TAG","Math.floor(10.7)----:"+Math.floor(10.7));//10.0 Log.d("TAG","Math.floor(-0.7)----:"+Math.floor(-0.7));//-1.0 Log.d("TAG","Math.floor(0.0)----:"+Math.floor(0.0));//0.0 Log.d("TAG","Math.floor(-0.0)----:"+Math.floor(-0.0));//-0.0 //Math.random 取得一个大于或者等于0.0小于不等于1.0的随机数[0,1) Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.random()----:"+Math.random());//输出[0,1)间的随机数 0.8979626325354049 Log.d("TAG","Math.random()*100----:"+Math.random()*100);//输出[0,100)间的随机数 32.783762836248144 // Math.rint 四舍五入 返回double值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.rint(10.1)----:"+Math.rint(10.1));//10.0 Log.d("TAG","Math.rint(10.7)----:"+Math.rint(10.7));//11.0 Log.d("TAG","Math.rint(-10.5)----:"+Math.rint(-10.5));//-10.0 Log.d("TAG","Math.rint(-10.51)----:"+Math.rint(-10.51));//-11.0 Log.d("TAG","Math.rint(-10.2)----:"+Math.rint(-10.2));//-10.0 Log.d("TAG","Math.rint(9)----:"+Math.rint(9));//9.0 //Math.round 四舍五入 float时返回int值,double时返回long值 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.round(10.1)----:"+Math.round(10.1));//10 Log.d("TAG","Math.round(10.7)----:"+Math.round(10.7));//11 Log.d("TAG","Math.round(-10.5)----:"+Math.round(-10.5));//-10 Log.d("TAG","Math.round(-10.51)----:"+Math.round(-10.51));//-11 Log.d("TAG","Math.round(-10.2)----:"+Math.round(-10.2));//-10 Log.d("TAG","Math.round(9)----:"+Math.round(9));//9 //Math.nextUp(a) 返回比a大一点点的浮点数 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.nextUp(1.2)----:"+Math.nextUp(1.2));//1.2000000000000002 //Math.nextDown(a) 返回比a小一点点的浮点数 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.nextDown(1.2)----:"+Math.nextDown(1.2));//1.1999999999999997 //Math.nextAfter(a,b) 返回(a,b)或(b,a)间与a相邻的浮点数 b可以比a小 Log.d("TAG","------------------------------------------"); Log.d("TAG","Math.nextAfter(1.2, 2.7)----:"+Math.nextAfter(1.2, 2.7));//1.2000000000000002 Log.d("TAG","Math.nextAfter(1.2, -1)----:"+Math.nextAfter(1.2, -1));//1.1999999999999997 }
结果
Math.sqrt(16)----:4.0 Math.cbrt(8)----:2.0 Math.hypot(3,4)----:5.0 ------------------------------------------ Math.pow(3,2)----:9.0 Math.exp(3)----:20.085536923187668 ------------------------------------------ Math.max(2.3,4.5)----:15 Math.min(2.3,4.5)----:2.3 ------------------------------------------ Math.abs(-10.4)----:10.4 Math.abs(10.1)----:10.1 ------------------------------------------ Math.ceil(-10.1)----:-10.0 Math.ceil(10.7)----:11.0 Math.ceil(-0.7)----:-0.0 Math.ceil(0.0)----:0.0 Math.ceil(-0.0)----:-0.0 Math.ceil(-1.7)----:-1.0 ------------------------------------------ Math.floor(-10.1)----:-11.0 Math.floor(10.7)----:10.0 Math.floor(-0.7)----:-1.0 Math.floor(0.0)----:0.0 Math.floor(-0.0)----:-0.0 ------------------------------------------ Math.random()----:0.8979626325354049 Math.random()*100----:32.783762836248144 ------------------------------------------ Math.rint(10.1)----:10.0 Math.rint(10.7)----:11.0 Math.rint(-10.5)----:-10.0 Math.rint(-10.51)----:-11.0 Math.rint(-10.2)----:-10.0 Math.rint(9)----:9.0 ------------------------------------------ Math.round(10.1)----:10 Math.round(10.7)----:11 Math.round(-10.5)----:-10 Math.round(-10.51)----:-11 Math.round(-10.2)----:-10 Math.round(9)----:9 ------------------------------------------ Math.nextUp(1.2)----:1.2000000000000002 Math.nextDown(1.2)----:1.1999999999999997 Math.nextAfter(1.2, 2.7)----:1.2000000000000002 Math.nextAfter(1.2, -1)----:1.1999999999999997
-
Math.round(),Math.ceil(),Math.floor()的区别
2019-07-23 11:49:421、Math.round() “四舍五入”,该函数返回的是一个四舍五入后的的整数 double d = 3.1415926; double d2 = 18.58; double d3 = -15.23; double d4 = -16.85; long round1 = Math.round(d); //... -
Java中Math函数的使用
2018-04-08 11:49:40Java中Math函数的使用 说到Java中的Math函数,大家肯定不陌生,但是在真正使用的时候却犯了难,那么多方法,我们到底需要使用哪个呢? 为此,我特地研究了一些Math常用函数的使用,以方便大家使用。 算术计算 ... -
Mathpix教程
2019-11-29 15:46:38Mathpix教程 -
分享Math的几个方法:Math.round()、Math.ceil()、Math.floor()和Math.abs()
2021-01-05 11:28:54Math.round() 就是数学中的四舍五入,举例: System.out.println("Math.round(1.2)="+Math.round(1.2)); System.out.println("Math.round(1.5)="+Math.round(1.5)); System.out.println("Math.round(1.7)="+Math.... -
python math库
2021-02-26 12:35:20math库常用函数及举例: 注意:使用math库前,用import导入该库 import math 取大于等于x的最小的整数值,如果x是一个整数,则返回x math.ceil(4.12) 5 把y的正负号加到x前面,可以使用0 math.... -
Math的几个方法Math.round()、Math.ceil()、Math.floor()和Math.abs()记录一下
2020-03-15 13:05:06Math.round() 就是数学中的四舍五入,举例: System.out.println("Math.round(1.2)="+Math.round(1.2)); System.out.println("Math.round(1.5)="+Math.round(1.5)); System.out.println("Math.round(1.7)="+... -
【Java】Math.round(),Math.ceil(),Math.floor()的区别
2020-08-24 09:22:10Math.round() “四舍五入”, double d = 3.1415926; double d2 = 18.58; double d3 = -15.23; double d4 = -16.85; long round1 = Math.round(d); // 结果 3 long round2 = Math.round(d2); // 结果 19 long... -
Math中的常用方法
2021-02-27 13:38:15package educoder; public class MathTest{ ... *Math.sqrt()//计算平方根 *Math.cbrt()//计算立方根 *Math.pow(a, b)//计算a的b次方 *Math.max( , );//计算最大值 *Math.min( , );//计算最小值 -
JavaScript Math对象方法 之 Math.abs() Math.ceil() Math.floor();
2019-05-05 16:22:00Math 对象用于执行数学任务。 Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math()。 1,Math.abs():方法可返回一个数的绝对值。 语法:Math.abs(x):如果 x 不是数字返回 NaN,如果 x 为 ... -
C#取整函数Math.Round、Math.Ceiling和Math.Floor
2018-12-26 19:17:441.Math.Round:四舍六入五取偶 引用内容 Math.Round(0.0) //0 Math.Round(0.1) //0 Math.Round(0.2) //0 Math.Round(0.3) //0 Math.Round(0.4) //0 Math.Round(0.5) //0 Math.Round(0.6) //1 Math.Round(0.7) //1 ... -
go --不要滥用math.Min和math.Math
2019-12-02 14:28:46比较大小我们经常使用,但非常奇怪的是:go的math.Max()和math.Min()函数只接收float64类型的?为什么? 外国一个博主给了非常棒的分析: ... ... -
Math.floor,Math.ceil,Math.rint,Math.round用法详解
2015-10-06 22:14:21Math.floor(), Math.ceil() ,Math.round(), Math.rint(), -
python math类
2020-11-08 11:13:07import math #弧度、角度的转换 print(math.degrees(math.pi)) print(math.radians(180)) #三角函数(正弦、反正弦、余弦) print(math.sin(math.pi/3)) print(math.cos(math.pi/2-math.pi/3)) print(math.tan(math.... -
mathpix替代品终于找到了
2019-10-04 19:16:44mathpix免费用完50次后,可以用此方法 -
JavaScript Math round
2019-09-20 15:59:36JavaScript Math round var round = Math.round(5.5); console.log(round);//6 -
Math对象和ES6Math对象的扩展
2018-11-20 21:59:451.Math的静态属性 Math.E:常数e。 Math.LN2:2 的自然对数。 Math.LN10:10 的自然对数。 Math.LOG2E:以 2 为底的e的对数。 Math.LOG10E:以 10 为底的e的对数。 Math.PI:常数π。 Math.... -
Python math包 math.ceil函数
2018-01-16 11:38:57import.math !!!!! 不能直接访问,需要导入math模块,然后使用math静态对象调用此函数。 Python数字ceil()方法返回x的最大值,即大于等于x的最小整数。 参数 x - 这是一个数字表达式。 返回值 此方法... -
最好用的数学神器Mathpix Snip-公式神器,只要截图就能识别公式,手写的公式都能识别
2019-04-09 18:53:05公式神器 Mathpix Snip -
tf.math
2019-07-26 17:39:46目录 一、函数列表 二、重要的API 1、tf.floor 2、tf.log 3、tf.reduce_mean ...6、tf.math.top_k ...7、tf.math.argmax ...8、tf.math.greater_equal ...9、tf.math.pow ...10、tf.math.multiply ...11、tf.math.sq... -
Math.random(),Math.round(),Math.ceil(),Math.floor()的区别
2019-01-23 00:32:061.Math.random():得到0-1之间的随机数 2.Math.round():四舍五入 根据“round”的字面意思“附近、周围”,可以猜测该函数是求一个附近的整数,看下面几个例子就明白。 小数点后第一位<5 正数:Math... -
C#中Math函数简介
2020-10-09 17:06:33C#中Math函数介绍 Math.abs() 计算绝对值 Math.acos() 计算反余弦值 Math.asin() 计算反正弦值 Math.atan() 计算反正切值 Math.atan2() 计算从x 坐标轴到点的角度 Math.ceil() 将数字向上舍入为最接近的整数 Math.... -
import math
2017-07-11 13:37:05函数:可以被调用,它执行某种行为并且返回一个值,...import math x=1 y=math.sqrt callable(x) False callable(y) True使用def来定义函数# coding=gbk def goto(name): return 'mk'+name+'ok'; print(goto('tffff'))