源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

谈谈JavaScript类型系统之Math

  • 时间:2022-06-19 16:07 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:谈谈JavaScript类型系统之Math
[b]开门必读[/b]   math和其他对象不同,Math对象是一个静态对象,而不是构造函数。实际上,Math只是一个由Javascript设置的对象命名空间,用于存储数学函数 [b]属性 [/b] Math.E 自然对数的底数,即常量e的值(约等于2.718) Math.PI 派的值(约等于3.14159) console.log(Math.E);//2.718281828459045 console.log(Math.PI);//3.141592653589793 Math.LN2 2的自然对数(约等于0.693) Math.LN10 10的自然对数(约等于2.302) Math.LOG2E 以2为底e的对数(约等于1.414) Math.LOG10E 以10为底e的对数(约等于0.434) console.log(Math.LN2);//0.6931471805599453 console.log(Math.LN10);//2.302585092994046 console.log(Math.LOG2E);//1.4426950408889634 console.log(Math.LOG10E);//0.4342944819032518 Math.SQRT2 2的平方根(约等于1.414) Math.SQRT1_2 1/2的平方根,即2的平方根的倒数(约等于0.707) console.log(Math.SQRT2);//1.4142135623730951 console.log(Math.SQRT1_2);//0.7071067811865476 [b]方法[/b]   这些方法都涉及到Number()隐式类型转换;若超出方法范围,将返回NaN Math.min() 返回一组数字中的最小值 Math.max() 返回一组数字中的最大值 console.log(Math.min(1,2,3));//1 console.log(Math.max(1,2,3));//3 Math.ceil(num) 向上舍入为整数 Math.floor(num) 向下舍入为整数 Math.round(num) 四舍五入为整数 console.log(Math.ceil(12.6));//13 console.log(Math.floor(12.6));//12 console.log(Math.round(12.6));//13 Math.abs(num) 返回num的绝对值 Math.random() 返回大于等于0小于1的一个随机数 console.log(Math.abs(-10));//10 console.log(Math.random());//0.741887615993619 Math.exp(num) 返回Math.E的num次幂 Math.log(num) 返回num的自然对数 Math.sqrt(num) 返回num的平方根(x必须是大于等于0的数) Math.pow(num,power) 返回num的power次幂 console.log(Math.exp(0));//1 console.log(Math.log(10));//2.302585092994046 console.log(Math.sqrt(100));//10 console.log(Math.pow(10,2));//100 Math.sin(x) 返回x的正弦值 Math.cos(x) 返回x的余弦值 Math.tan(x) 返回x的正切值 Math.asin(x) 返回x的反正弦值(x必须是-1到1之间的数) Math.acos(x) 返回x的反余弦值(x必须是-1到1之间的数) Math.atan(x) 返回x的反正切值 Math.atan2(y,x) 返回y/x的反正切值 console.log(Math.sin(30*Math.PI/180));//0.49999999999999994 console.log(Math.cos(60*Math.PI/180));//0.5000000000000001 console.log(Math.tan(45*Math.PI/180));//0.9999999999999999 console.log(Math.asin(1)*180/Math.PI);//90 console.log(Math.acos(1)*180/Math.PI);//0 console.log(Math.atan(1)*180/Math.PI);//45 console.log(Math.atan2(1,1)*180/Math.PI);//45 [b]tips[/b] [tips1]找到数组中的最大或最小值
var values = [1,2,3,4,5,6,7,8];
var max = Math.max.apply(Math,values);//8 
[tips2]从某个整数范围内随机选择一个值
value = Math.floor(Math.random()*可能值的总数 + 第一个可能的值)
[tips3]通过最小值和最大值随机选择一个值
function selectFrom(lowerValue,upperValue){
var choices = upperValue - lowerValue + 1;
return Math.floor(Math.random()*choices + lowerValue);
}
var num = selectFrom(2,10);
console.log(num); 
[h1]Math 对象方法[/h1]
方法 描述
[url=http://www.w3school.com.cn/jsref/jsref_abs.asp]abs(x)[/url] 返回数的绝对值。
[url=http://www.w3school.com.cn/jsref/jsref_acos.asp]acos(x)[/url] 返回数的反余弦值。
[url=http://www.w3school.com.cn/jsref/jsref_asin.asp]asin(x)[/url] 返回数的反正弦值。
[url=http://www.w3school.com.cn/jsref/jsref_atan.asp]atan(x)[/url] 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
[url=http://www.w3school.com.cn/jsref/jsref_atan2.asp]atan2(y,x)[/url] 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
[url=http://www.w3school.com.cn/jsref/jsref_ceil.asp]ceil(x)[/url] 对数进行上舍入。
[url=http://www.w3school.com.cn/jsref/jsref_cos.asp]cos(x)[/url] 返回数的余弦。
[url=http://www.w3school.com.cn/jsref/jsref_exp.asp]exp(x)[/url] 返回 e 的指数。
[url=http://www.w3school.com.cn/jsref/jsref_floor.asp]floor(x)[/url] 对数进行下舍入。
[url=http://www.w3school.com.cn/jsref/jsref_log.asp]log(x)[/url] 返回数的自然对数(底为e)。
[url=http://www.w3school.com.cn/jsref/jsref_max.asp]max(x,y)[/url] 返回 x 和 y 中的最高值。
[url=http://www.w3school.com.cn/jsref/jsref_min.asp]min(x,y)[/url] 返回 x 和 y 中的最低值。
[url=http://www.w3school.com.cn/jsref/jsref_pow.asp]pow(x,y)[/url] 返回 x 的 y 次幂。
[url=http://www.w3school.com.cn/jsref/jsref_random.asp]random()[/url] 返回 0 ~ 1 之间的随机数。
[url=http://www.w3school.com.cn/jsref/jsref_round.asp]round(x)[/url] 把数四舍五入为最接近的整数。
[url=http://www.w3school.com.cn/jsref/jsref_sin.asp]sin(x)[/url] 返回数的正弦。
[url=http://www.w3school.com.cn/jsref/jsref_sqrt.asp]sqrt(x)[/url] 返回数的平方根。
[url=http://www.w3school.com.cn/jsref/jsref_tan.asp]tan(x)[/url] 返回角的正切。
[url=http://www.w3school.com.cn/jsref/jsref_tosource_math.asp]toSource()[/url] 返回该对象的源代码。
[url=http://www.w3school.com.cn/jsref/jsref_valueof_math.asp]valueOf()[/url] 返回 Math 对象的原始值。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部