欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

談?wù)凧avaScript類型系統(tǒng)之Math

 更新時(shí)間:2016年01月06日 16:03:32   投稿:mrr  
Math 對(duì)象并不像 Date 和 String 那樣是對(duì)象的類,因此沒(méi)有構(gòu)造函數(shù) Math(),像 Math.sin() 這樣的函數(shù)只是函數(shù),不是某個(gè)對(duì)象的方法。您無(wú)需創(chuàng)建它,通過(guò)把 Math 作為對(duì)象使用就可以調(diào)用其所有屬性和方法

開(kāi)門必讀

  math和其他對(duì)象不同,Math對(duì)象是一個(gè)靜態(tài)對(duì)象,而不是構(gòu)造函數(shù)。實(shí)際上,Math只是一個(gè)由Javascript設(shè)置的對(duì)象命名空間,用于存儲(chǔ)數(shù)學(xué)函數(shù)

屬性

Math.E 自然對(duì)數(shù)的底數(shù),即常量e的值(約等于2.718)
Math.PI 派的值(約等于3.14159)
console.log(Math.E);//2.718281828459045
console.log(Math.PI);//3.141592653589793
Math.LN2 2的自然對(duì)數(shù)(約等于0.693)
Math.LN10 10的自然對(duì)數(shù)(約等于2.302)
Math.LOG2E 以2為底e的對(duì)數(shù)(約等于1.414)
Math.LOG10E 以10為底e的對(duì)數(shù)(約等于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的平方根的倒數(shù)(約等于0.707)
console.log(Math.SQRT2);//1.4142135623730951
console.log(Math.SQRT1_2);//0.7071067811865476

方法

  這些方法都涉及到Number()隱式類型轉(zhuǎn)換;若超出方法范圍,將返回NaN

Math.min() 返回一組數(shù)字中的最小值
Math.max() 返回一組數(shù)字中的最大值
console.log(Math.min(1,2,3));//1
console.log(Math.max(1,2,3));//3
Math.ceil(num) 向上舍入為整數(shù)
Math.floor(num) 向下舍入為整數(shù)
Math.round(num) 四舍五入為整數(shù)
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的絕對(duì)值
Math.random() 返回大于等于0小于1的一個(gè)隨機(jī)數(shù)
console.log(Math.abs(-10));//10
console.log(Math.random());//0.741887615993619
Math.exp(num) 返回Math.E的num次冪
Math.log(num) 返回num的自然對(duì)數(shù)
Math.sqrt(num) 返回num的平方根(x必須是大于等于0的數(shù))
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之間的數(shù))
Math.acos(x) 返回x的反余弦值(x必須是-1到1之間的數(shù))
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

tips

[tips1]找到數(shù)組中的最大或最小值

var values = [1,2,3,4,5,6,7,8];
var max = Math.max.apply(Math,values);//8 

[tips2]從某個(gè)整數(shù)范圍內(nèi)隨機(jī)選擇一個(gè)值

value = Math.floor(Math.random()*可能值的總數(shù) + 第一個(gè)可能的值)

[tips3]通過(guò)最小值和最大值隨機(jī)選擇一個(gè)值

function selectFrom(lowerValue,upperValue){
var choices = upperValue - lowerValue + 1;
return Math.floor(Math.random()*choices + lowerValue);
}
var num = selectFrom(2,10);
console.log(num); 

Math 對(duì)象方法

方法 描述
abs(x) 返回?cái)?shù)的絕對(duì)值。
acos(x) 返回?cái)?shù)的反余弦值。
asin(x) 返回?cái)?shù)的反正弦值。
atan(x) 以介于 -PI/2 與 PI/2 弧度之間的數(shù)值來(lái)返回 x 的反正切值。
atan2(y,x) 返回從 x 軸到點(diǎn) (x,y) 的角度(介于 -PI/2 與 PI/2 弧度之間)。
ceil(x) 對(duì)數(shù)進(jìn)行上舍入。
cos(x) 返回?cái)?shù)的余弦。
exp(x) 返回 e 的指數(shù)。
floor(x) 對(duì)數(shù)進(jìn)行下舍入。
log(x) 返回?cái)?shù)的自然對(duì)數(shù)(底為e)。
max(x,y) 返回 x 和 y 中的最高值。
min(x,y) 返回 x 和 y 中的最低值。
pow(x,y) 返回 x 的 y 次冪。
random() 返回 0 ~ 1 之間的隨機(jī)數(shù)。
round(x) 把數(shù)四舍五入為最接近的整數(shù)。
sin(x) 返回?cái)?shù)的正弦。
sqrt(x) 返回?cái)?shù)的平方根。
tan(x) 返回角的正切。
toSource() 返回該對(duì)象的源代碼。
valueOf() 返回 Math 對(duì)象的原始值。

相關(guān)文章

最新評(píng)論