本文目錄一覽:
- 1、js中的math對象有哪些常用的方法,其用法和作用是什麼
- 2、number-precision 實現js高精度運算 源碼
- 3、JavaScript Math.random()問題
- 4、JS中Math[this
- 5、js Math 對象的方法
- 6、js中Math的幾個函數
js中的math對象有哪些常用的方法,其用法和作用是什麼
abs(x)
返回數的絕對值。
acos(x)
返回數的反餘弦值。
asin(x)
返回數的反正弦值。
atan(x)
以介於 -PI/2 與 PI/2 弧度之間的數值來返回 x 的反正切值。
atan2(y,x)
返回從 x 軸到點 (x,y) 的角度(介於 -PI/2 與 PI/2 弧度之間)。
ceil(x)
對數進行上舍入。
cos(x)
返回數的餘弦。
exp(x)
返回 e 的指數。
floor(x)
對數進行下舍入。
log(x)
返回數的自然對數(底為e)。
max(x,y)
返回 x 和 y 中的最高值。
min(x,y)
返回 x 和 y 中的最低值。
pow(x,y)
返回 x 的 y 次冪。
random()
返回 0 ~ 1 之間的隨機數。
round(x)
把數四捨五入為最接近的整數。
sin(x)
返回數的正弦。
sqrt(x)
返回數的平方根。
tan(x)
返回角的正切。
toSource()
返回該對象的源代碼。
valueOf()
返回 Math 對象的原始值。
作用和用法?數學函數需要什麼用什麼啊。
number-precision 實現js高精度運算 源碼
type numType = number | string;
/**
* @desc 解決浮動運算問題,避免小數點後產生多位數和計算精度損失。
* 問題示例:2.3 + 2.4 = 4.699999999999999,1.0 – 0.9 = 0.09999999999999998
*/
/**
* 把錯誤的數據轉正
* strip(0.09999999999999998)=0.1
*/
function strip(num: numType, precision = 15): number {
return +parseFloat(Number(num).toPrecision(precision));
}
/**
* Return digits length of a number
* @param {*number} num Input number
*/
function digitLength(num: numType): number {
// Get digit length of e
const eSplit = num.toString().split(/[eE]/);
const len = (eSplit[0].split(‘.’)[1] || ”).length – +(eSplit[1] || 0);
return len 0 ? len : 0;
}
/**
* 把小數轉成整數,支持科學計數法。如果是小數則放大成整數
* @param {*number} num 輸入數
*/
function float2Fixed(num: numType): number {
if (num.toString().indexOf(‘e’) === -1) {
return Number(num.toString().replace(‘.’, ”));
}
const dLen = digitLength(num);
return dLen 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num);
}
/**
* 檢測數字是否越界,如果越界給出提示
* @param {*number} num 輸入數
*/
function checkBoundary(num: number) {
if (_boundaryCheckingState) {
if (num Number.MAX_SAFE_INTEGER || num Number.MIN_SAFE_INTEGER) {
console.warn(`${num} is beyond boundary when transfer to integer, the results may not be accurate`);
}
}
}
/**
* 精確乘法
*/
function times(num1: numType, num2: numType, …others: numType[]): number {
if (others.length 0) {
return times(times(num1, num2), others[0], …others.slice(1));
}
const num1Changed = float2Fixed(num1);
const num2Changed = float2Fixed(num2);
const baseNum = digitLength(num1) + digitLength(num2);
const leftValue = num1Changed * num2Changed;
checkBoundary(leftValue);
return leftValue / Math.pow(10, baseNum);
}
/**
* 精確加法
*/
function plus(num1: numType, num2: numType, …others: numType[]): number {
if (others.length 0) {
return plus(plus(num1, num2), others[0], …others.slice(1));
}
const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
}
/**
* 精確減法
*/
function minus(num1: numType, num2: numType, …others: numType[]): number {
if (others.length 0) {
return minus(minus(num1, num2), others[0], …others.slice(1));
}
const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
return (times(num1, baseNum) – times(num2, baseNum)) / baseNum;
}
/**
* 精確除法
*/
function divide(num1: numType, num2: numType, …others: numType[]): number {
if (others.length 0) {
return divide(divide(num1, num2), others[0], …others.slice(1));
}
const num1Changed = float2Fixed(num1);
const num2Changed = float2Fixed(num2);
checkBoundary(num1Changed);
checkBoundary(num2Changed);
// fix: 類似 10 ** -4 為 0.00009999999999999999,strip 修正
return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) – digitLength(num1))));
}
/**
* 四捨五入
*/
function round(num: numType, ratio: number): number {
const base = Math.pow(10, ratio);
return divide(Math.round(times(num, base)), base);
}
let _boundaryCheckingState = true;
/**
* 是否進行邊界檢查,默認開啟
* @param flag 標記開關,true 為開啟,false 為關閉,默認為 true
*/
// 這裡可以設置邊界檢查(默認是true)
function enableBoundaryChecking(flag = true) {
_boundaryCheckingState = flag;
}
// 輸出上面的方法
export { strip, plus, minus, times, divide, round, digitLength, float2Fixed, enableBoundaryChecking };
export default {
strip,
plus,
minus,
times,
divide,
round,
digitLength,
float2Fixed,
enableBoundaryChecking,
};
JavaScript Math.random()問題
那是因為javascript把min_num作為了默認字符串處理了。
只需要把var num=Math.random()*(max_num-min_num)+min_num;改一下就可以了
var num=Math.random()*(max_num-min_num)+min_num*1;
強制把min_num作為數字處理
JS中Math[this
在javascript中對象獲取屬性的方式有兩種,一種是使用運算符「.」跟上屬性名,一種是使用[‘屬性名’]的方式。比如:
window.location 與window[‘location’]都可以訪問到window對象的location屬性。
點運算符(成員運算符)使用方便,但是在需要動態獲取對象屬性時,也就是說,我要獲取的屬性它的名稱本身也是通過傳參確定時,就需要使用方括號的方式,你現在就屬於這種情況。如果你有過反射方面的編程經驗就不難理解了。
js Math 對象的方法
1.丟棄小數部分,保留整數部分
parseInt(5/2)
2.向上取整,有小數就整數部分加1
Math.ceil(5/2)
3,四捨五入.
Math.round(5/2)
4,向下取整
Math.floor(5/2)
Math
對象方法
FF:
Firefox,
IE:
Internet
Explorer
方法
描述
FF
IE
abs(x)
返回數的絕對值。
1
3
acos(x)
返回數的反餘弦值。
1
3
asin(x)
返回數的反正弦值。
1
3
atan(x)
以介於
-PI/2
與
PI/2
弧度之間的數值來返回
x
的反正切值。
1
3
atan2(y,x)
返回從
x
軸到點
(x,y)
的角度(介於
-PI/2
與
PI/2
弧度之間)。
1
3
ceil(x)
對數進行上舍入。
1
3
cos(x)
返回數的餘弦。
1
3
exp(x)
返回
e
的指數。
1
3
floor(x)
對數進行下舍入。
1
3
log(x)
返回數的自然對數(底為e)。
1
3
max(x,y)
返回
x
和
y
中的最高值。
1
3
min(x,y)
返回
x
和
y
中的最低值。
1
3
pow(x,y)
返回
x
的
y
次冪。
1
3
random()
返回
~
1
之間的隨機數。
1
3
round(x)
把數四捨五入為最接近的整數。
1
3
sin(x)
返回數的正弦。
1
3
sqrt(x)
返回數的平方根。
1
3
tan(x)
返回角的正切。
1
3
toSource()
返回該對象的源代碼。
1
–
valueOf()
返回
Math
對象的原始值。
1
4
js中Math的幾個函數
console.log(Object.getOwnPropertyNames(Math).filter((function (name) {
return typeof Math[name] === ‘function’;
})).length); //有35個函數
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/256990.html