最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解JavaScript中Math內(nèi)置對象基本方法的使用

 更新時(shí)間:2022年04月29日 10:18:34   作者:程序猿布?xì)W  
Math?是javaScript的內(nèi)置對象,包含了部分?jǐn)?shù)學(xué)常數(shù)屬性和數(shù)學(xué)函數(shù)方法。本文將詳細(xì)講解Math基本方法的使用,感興趣的小伙伴可以學(xué)習(xí)一下

概念

Math 是javaScript的內(nèi)置對象,包含了部分?jǐn)?shù)學(xué)常數(shù)屬性和數(shù)學(xué)函數(shù)方法。

Math 不是一個(gè)函數(shù)對象,用戶Number類型進(jìn)行使用,不支持BigInt。

Math 的所有屬性與方法都是靜態(tài)的。

比如說當(dāng)我們使用圓周率的時(shí)候,寫法是 Math.PI

當(dāng)使用正余弦函數(shù)的寫法是 Math.sin(x),x 是要傳入的參數(shù)。

Math 的常量是使用 JavaScript 中的全精度浮點(diǎn)數(shù)來定義的。

math原生屬性

// 歐拉常數(shù),也是自然對數(shù)的底數(shù),約等于 2.718。
console.log("Math.E", Math.E);  //  Math.E 2.718281828459045
// 2 的自然對數(shù),約等于 0.693。
console.log("Math.LN2", Math.LN2);  //  Math.LN2 0.6931471805599453
// 10 的自然對數(shù),約等于 2.303。
console.log("Math.LN10", Math.LN10);  //  Math.LN10 2.302585092994046
// 以 2 為底的 E 的對數(shù),約等于 1.443。
console.log("Math.LOG2E", Math.LOG2E);  //  Math.LOG2E 1.4426950408889634
// 以 10 為底的 E 的對數(shù),約等于 0.434。
console.log("Math.LOG10E", Math.LOG10E);  //  Math.LOG10E 0.4342944819032518
// 圓周率,一個(gè)圓的周長和直徑之比,約等于 3.14159。
console.log("Math.PI", Math.PI);  //  Math.PI 3.141592653589793
// 計(jì)算圓周長
function calculateCircumference(radius) {
  return 2 * Math.PI * radius;
}
console.log("calculateCircumference(1)", calculateCircumference(1)); // calculateCircumference(1) 6.283185307179586
// 二分之一 ? 的平方根,同時(shí)也是 2 的平方根的倒數(shù)  1 2 ,約等于 0.707。
console.log("Math.SQRT1_2", Math.SQRT1_2);  //  Math.SQRT1_2 0.7071067811865476
// 2 的平方根,約等于 1.414。
console.log("Math.SQRT2", Math.SQRT2);  //  Math.SQRT2 1.4142135623730951

math常用方法

Math.abs()  // 指定數(shù)字 “x“ 的絕對值
Math.abs("-1"); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs("string"); // NaN
Math.abs(); // NaN

math在日常開發(fā)中的數(shù)字處理方法

// Math.round() 函數(shù)返回一個(gè)數(shù)字四舍五入后最接近的整數(shù)。
console.log(Math.round(20.49)); //20
console.log(Math.round(20.5)); //21
console.log(Math.round(-20.5)); //-20
console.log(Math.round(-20.51)); //-21


// Math.ceil() 返回大于或等于一個(gè)給定數(shù)字的最小整數(shù),向上取整。
console.log(Math.ceil(0.95));
// 1
console.log(Math.ceil(4));
// 4
console.log(Math.ceil(7.004));
// 8
console.log(Math.ceil(-7.004));
// -7

// Math.floor() 返回小于或等于一個(gè)給定數(shù)字的最大整數(shù), Math.floor()為向下取整。
Math.floor(45.95);
// 45
Math.floor(45.05);
// 45
Math.floor(4);
// 4
Math.floor(-45.05);
// -46
Math.floor(-45.95);
// -46

// Math.max() 返回一組數(shù)當(dāng)中的最大值
console.log(Math.max(1, 3, 2));
// 3
console.log(Math.max(-1, -3, -2));
// -1
const array1 = [1, -3, 2];
console.log(Math.max(...array1));
// 3


// Math.min() 返回零個(gè)或更多個(gè)數(shù)值的最小值。
console.log(Math.min()); // Infinity
console.log(Math.min(1, 2, 3, -4)); // -4

// 使用 Math.min() 裁剪值(Clipping a value)
function f(x) {
  if (x > 5) {
    return (x = 5);
  }
  return (x = 6);
}
var finalMin = Math.min(f(2), 2, 3, 4, 5, 30);
console.log("finalMin", finalMin);  // 2


// Math.sqrt() 返回一個(gè)數(shù)的平方根
function calcHypotenuse(a, b) {
  return Math.sqrt(a * a + b * b);
}
console.log(calcHypotenuse(3, 4));
// 5
console.log(calcHypotenuse(5, 12));
// 13
console.log(calcHypotenuse(0, 0));
// 0

使用Math.random()生成隨機(jī)數(shù)

/**
 *
 * Math.random() 函數(shù)返回一個(gè)浮點(diǎn)數(shù)
 * 偽隨機(jī)數(shù)在范圍從0到小于1,也就是說,從0(包括0)往上,但是不包括1(排除1),
 * 然后您可以縮放到所需的范圍。實(shí)現(xiàn)將初始種子選擇到隨機(jī)數(shù)生成算法;它不能被用戶選擇或重置。
 *
 * */

console.log(Math.random());

function getRandomNumber(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}

console.log(getRandomNumber(2, 100));

小結(jié)

  • 以上例子包含了math常用的方法和屬性的api
  • math在使用過程中,可以結(jié)合random以及max和min方法等,生成需要的隨機(jī)數(shù)
  • 通過round、floor、ceil,我們可以針對數(shù)字進(jìn)行進(jìn)一步地取值,得到符合要求的數(shù)字格式

Math更多方法請查閱文檔

Mathjs插件

文檔地址

mathjs的插件提供的方法比較全面,涵蓋了從代數(shù)計(jì)算到函數(shù)計(jì)算,貨幣運(yùn)算等方法,矩陣序列化等,更多方法可以查看官方文檔。

基礎(chǔ)使用方法:

npm install mathjs
import { sqrt } from 'mathjs'
console.log(sqrt(-4).toString()) // 2i

源碼地址

碼云

github

到此這篇關(guān)于詳解JavaScript中Math內(nèi)置對象基本方法的使用的文章就介紹到這了,更多相關(guān)JavaScript內(nèi)置對象Math內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

大埔县| 平邑县| 青岛市| 剑阁县| 鹤庆县| 延边| 家居| 海林市| 阳新县| 汉源县| 柳河县| 阳江市| 改则县| 沁源县| 库车县| 涞水县| 绥芬河市| 威远县| 敦煌市| 沭阳县| 福清市| 永宁县| 子洲县| 西吉县| 萨嘎县| 福州市| 乐安县| 长白| 安义县| 晋州市| 阿瓦提县| 连南| 衡阳市| 五华县| 昌乐县| 天门市| 绥化市| 将乐县| 金昌市| 毕节市| 东兴市|