Math
是一個內置對象,它擁有一些數學常數屬性和數學函數方法,Math
用於Number
類型,其不支持BigInt
。javascript
Math
不是一個函數對象,也就是說Math
不是一個構造器,Math
的全部屬性與方法都是靜態的,例如引用圓周率的寫法是Math.PI
,Math
的常量是使用JavaScript
中的全精度浮點數來定義的,須要注意的是,不少Math
的函數都有一個精度,並且這個精度在不一樣實現中也是不相同的,這意味着不一樣的瀏覽器會給出不一樣的結果,甚至在不一樣的系統或架構下,相同的Js
引擎也會給出不一樣的結果,另外三角函數sin()
、cos()
、tan()
、asin()
、acos()
、atan()
和atan2()
返回的值是弧度而非角度。若要轉換,弧度除以Math.PI / 180
便可轉換爲角度,同理角度乘以這個數則能轉換爲弧度。java
console.log(Math.PI); // 3.141592653589793 console.log(Math.abs(-1)); // 1 console.log(Math.pow(2, 3)); // 8
Math.E
: 歐拉常數,也是天然對數的底數,約等於2.718
。Math.LN2
: 2
的天然對數,約等於0.693
。Math.LN10
: 10
的天然對數,約等於2.303
。Math.LOG2E
: 以2
爲底的E
的對數,約等於1.443
。Math.LOG10E
: 以10
爲底的E
的對數,約等於0.434
。Math.PI
: 圓周率,一個圓的周長和直徑之比,約等於3.14159
。Math.SQRT1_2
: ½
的平方根,同時也是2
的平方根的倒數,約等於0.707
。Math.SQRT2
: 2
的平方根,約等於1.414
。Math.abs(x)
Math.abs(x)
函數返回指定數字x
的絕對值。git
console.log(Math.abs(-1)); // 1
Math.acos(x)
Math.acos()
返回一個數的反餘弦值。
∀x∊[-1;1], Math.acos(x) = arccos(x) = the unique y∊[0;π] such that cos(y) = x
github
console.log(Math.acos(-1)); // 3.141592653589793
Math.acosh(x)
Math.acosh()
函數返回一個數的反雙曲餘弦值。
∀x≥1, Math.acosh(x) = arcosh(x) = the unique y≥0 such that cosh(y) = x
算法
console.log(Math.acosh(1)); // 0
Math.asin(x)
Math.asin()
方法返回一個數值的反正弦。
∀x∊[-1;1], Math.asin(x) = arcsin(x) = the unique y∊[- π/2 ; π/2 ] such that sin(y) = x
數組
console.log(Math.asin(0)); // 0
Math.asinh(x)
Math.asinh()
返回一個數值的反雙曲正弦值。
Math.asinh(x) = arsinh(x) = the unique y such that sinh(y) = x
瀏覽器
console.log(Math.asinh(0)); // 0
Math.atan(x)
Math.atan()
函數返回一個數值的反正切。
Math.atan(x) = arctan(x) = the unique y∊[- π/2 ; π/2 ] such that tan(y) = x
架構
console.log(Math.atan(0)); // 0
Math.atanh(x)
Math.atanh()
函數返回一個數值反雙曲正切值。
∀x∊(-1,1), Math.atanh(x) = arctanh(x) = the unique y such that tanh(y) = x
app
console.log(Math.atanh(0)); // 0
Math.atan2(y, x)
Math.atan2()
返回從原點(0,0)
到(x,y)
點的線段與x軸正方向之間的平面角度(弧度值),也就是Math.atan2(y,x)
。dom
console.log(Math.atan2(15, 90)); // 0.16514867741462683
Math.cbrt(x)
Math.cbrt()
函數返回任意數字的立方根。
console.log(Math.cbrt(27)); // 3
Math.ceil(x)
Math.ceil()
函數返回大於或等於一個給定數字的最小整數,即向上取整。
console.log(Math.ceil(6.6)); // 7
Math.clz32(x)
Math.clz32()
函數返回一個數字在轉換成32
無符號整形數字的二進制形式後,開頭的0
的個數, 好比1000000
轉換成32
位無符號整形數字的二進制形式後是00000000000011110100001001000000
,開頭的0
的個數是12
個,則Math.clz32(1000000)
返回12
。
console.log(Math.clz32(10)); // 28
Math.cos(x)
Math.cos()
函數返回一個數值的餘弦值。
console.log(Math.clz32(10)); // 28
Math.cosh(x)
Math.cosh()
函數返回數值的雙曲餘弦函數。
console.log(Math.cosh(0)); // 1
Math.exp(x)
Math.exp()
函數返回e^x
,x
表示參數,e
是天然對數的底數約2.718281828459045
。
console.log(Math.exp(2)); // 7.38905609893065
Math.expm1(x)
Math.exp()
函數返回e^x -1
,x
表示參數,e
是天然對數的底數約2.718281828459045
。
console.log(Math.expm1(2)); // 6.38905609893065
Math.floor()
返回小於或等於一個給定數字的最大整數,即向下取整。
console.log(Math.floor(6.6)); // 6
Math.fround(doubleFloat)
Math.fround()
能夠將任意的數字轉換爲離它最近的單精度浮點數形式的數字。JavaScript
內部使用64
位的雙浮點數字,支持很高的精度。可是有時須要用32
位浮點數字,好比從一個Float32Array
讀取值時,這時會產生混亂,檢查一個64
位浮點數和一個32
位浮點數是否相等會失敗,即便二個數字幾乎如出一轍,要解決這個問題,可使用Math.fround()
來將64
位的浮點數轉換爲32
位浮點數,在內部JavaScript
繼續把這個數字做爲64
位浮點數看待,僅僅是在尾數部分的第23
位執行了舍入到偶」的操做,並將後續的尾數位設置爲0
,若是數字超出32
位浮點數的範圍,則返回Infinity
或-Infinity
。
// 數字1.5能夠在二進制數字系統中精確表示,32位和64位的值相同 console.log(Math.fround(1.5) === 1.5); // true // 數字6.6卻沒法在二進制數字系統中精確表示,因此32位和64位的值是不一樣的 console.log(Math.fround(6.6) === 6.6); // false // 在某些精度不高的場合下,能夠經過將二個浮點數轉換成32位浮點數進行比較,以解決64位浮點數比較結果不正確的問題 console.log(0.1 + 0.2 === 0.3); //false var equal = (v1, v2) => Math.fround(v1) === Math.fround(v2); console.log(equal(0.1 + 0.2, 0.3)); // true
Math.hypot([value1[,value2, ...]])
Math.hypot()
函數返回全部參數的平方和的平方根。本函數比Math.sqrt()
更簡單也更快,只須要調用Math.hypot(v1, v2)
或Math.hypot(v1, v2, v3, v4, ...)
,其還避免了幅值過大的問題,Js
中最大的雙精度浮點數是Number.MAX_VALUE = 1.797...e+308
,若是計算的數字比約1e154
大,計算其平方值會返回Infinity
,使計算的的結果出現問題。
console.log(Math.hypot(3, 4)); // 5
Math.imul(a, b)
Math.imul()
函數將兩個參數分別轉換爲32
位整數,相乘後返回32
位結果,相似C
語言的32
位整數相乘。
console.log(Math.imul(0xffffffff, 1)); // -1
Math.log(x)
Math.log()
函數返回一個數的天然對數。
∀x>0, Math.log(x) = ln(x) = the unique y such that e^y = x
console.log(Math.log(Math.E)); // 1
Math.log10(x)
Math.log10()
函數返回一個數字以10
爲底的對數。
console.log(Math.log10(100)); // 2
Math.log1p(x)
Math.log1p()
函數返回一個數字加1
後的天然對數, 既log(x+1)
。
console.log(Math.log1p(Math.E-1)); // 1
Math.log2(x)
Math.log2()
函數返回一個數字以2
爲底的對數。
console.log(Math.log2(8)); // 3
Math.max(value1[,value2, ...])
Math.max()
函數返回一組數中的最大值。
console.log(Math.max(1, 2, 3)); // 3 // 在數組中應用 console.log(Math.max.apply(null, [1, 2, 3])); // 3 // 利用了apply的傳參特性 console.log(Math.max(...[1, 2, 3])); // 3 // 利用了 ES6 Spread 操做符
Math.min([value1[,value2, ...]])
Math.min()
返回零個或更多個數值的最小值。
console.log(Math.min(1, 2, 3)); // 1 // 在數組中應用 console.log(Math.min.apply(null, [1, 2, 3])); // 1 // 利用了apply的傳參特性 console.log(Math.min(...[1, 2, 3])); // 1 // 利用了 ES6 Spread 操做符
Math.pow(base, exponent)
Math.pow()
函數返回基數base
的指數exponent
次冪,即base^exponent
。
console.log(Math.pow(2, 3)); // 8
Math.random()
Math.random()
函數返回一個浮點數,僞隨機數在範圍從0
到小於1
,也就是說從0
(包括0
)往上,可是不包括1
,而後能夠縮放到所需的範圍,實現將初始種子選擇到隨機數生成算法,其不能被用戶選擇或重置。
console.log(Math.random()); // 0.20022678953392647 function randomInt(min=0, max=1) { // 生成隨機整數 return min + ~~((max-min)*Math.random()); // min <= random < max } randomInt(1, 9); // 5
Math.round(x)
Math.round()
函數返回一個數字四捨五入後最接近的整數。
console.log(Math.round(0.5)); // 1
Math.sign(x)
Math.sign()
函數返回一個數字的符號, 指示數字是正數,負數仍是零。此函數共有5
種返回值, 分別是1, -1, 0, -0, NaN
表明的各是正數,負數,正零,負零,NaN
。
console.log(Math.sign(0.5)); // 1
Math.sin(x)
Math.sin()
函數返回一個數值的正弦值。
console.log(Math.sin(Math.PI / 2)); // 1
Math.sinh(x)
Math.sinh()
函數返回一個數字的雙曲正弦值。
console.log(Math.sinh(0)); // 0
Math.sqrt(x)
Math.sqrt()
函數返回一個數的平方根。
∀x≥0, Math.sqrt(x) = x = the unique y≥0 such that y^2 = x
console.log(Math.sqrt(9)); // 3
Math.tan(x)
Math.tan()
方法返回一個數值的正切值。
console.log(Math.tan(0)); // 0
Math.tanh(x)
Math.tanh()
函數將會返回一個數的雙曲正切函數值。
console.log(Math.tanh(0)); // 0
Math.trunc(value)
Math.trunc()
方法會將數字的小數部分去掉,只保留整數部分。
console.log(Math.trunc(1.1)); // 1
https://github.com/WindrunnerMax/EveryDay
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math