登录
首页 >  文章 >  前端

掌握 JavaScript 的数学对象:内置数学函数和属性的综合指南

来源:dev.to

时间:2024-09-12 21:06:42 379浏览 收藏

一分耕耘,一分收获!既然都打开这篇《掌握 JavaScript 的数学对象:内置数学函数和属性的综合指南》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新文章相关的内容,希望对大家都有所帮助!

掌握 JavaScript 的数学对象:内置数学函数和属性的综合指南

javascript 数学对象:概述

javascript math 对象是一个内置对象,提供数学函数和常量的集合。它不是构造函数,因此您无法创建它的实例;相反,它是通过其静态方法和属性直接使用的。

1.常数

math 对象包含几个对数学计算有用的常量:

  • math.e:自然对数的底数,约等于 2.718。
  • math.ln2:2的自然对数,约等于0.693。
  • math.ln10:10 的自然对数,约等于 2.303。
  • math.log2e:e 的以 2 为底的对数,约等于 1.442。
  • math.log10e:e 以 10 为底的对数,约等于 0.434。
  • math.pi:圆的周长与其直径的比值,约等于 3.14159。
  • math.sqrt1_2:1/2 的平方根,约等于 0.707。
  • math.sqrt2:2 的平方根,约等于 1.414。

2.方法

math 对象提供了多种执行数学运算的方法:

  • math.abs(x):返回x的绝对值。
  math.abs(-5); // 5
  • math.ceil(x):将 x 向上舍入到最接近的整数。
  math.ceil(4.2); // 5
  • math.floor(x):将 x 向下舍入到最接近的整数。
  math.floor(4.7); // 4
  • math.round(x):将 x 四舍五入到最接近的整数。
  math.round(4.5); // 5
  • math.max(...values):返回零个或多个数字中的最大值。
  math.max(1, 5, 3); // 5
  • math.min(...values):返回零个或多个数字中的最小值。
  math.min(1, 5, 3); // 1
  • math.random():返回 0(含)和 1(不包括)之间的伪随机数。
  math.random(); // e.g., 0.237
  • math.pow(base, exponent):返回底数的指数次幂。
  math.pow(2, 3); // 8
  • math.sqrt(x):返回 x 的平方根。
  math.sqrt(9); // 3
  • math.trunc(x):返回 x 的整数部分,删除任何小数位。
  math.trunc(4.9); // 4

3.用法示例

以下是一些如何使用 math 对象的实际示例:

  • 生成随机整数
  function getrandomint(min, max) {
    return math.floor(math.random() * (max - min + 1)) + min;
  }
  console.log(getrandomint(1, 10)); // e.g., 7
  • 计算斜边
  function calculatehypotenuse(a, b) {
    return math.sqrt(math.pow(a, 2) + math.pow(b, 2));
  }
  console.log(calculatehypotenuse(3, 4)); // 5

4.限制和注意事项

  • 精度问题:浮点运算可能会导致精度问题。例如,由于舍入误差,math.sqrt(2) * math.sqrt(2) 可能不完全等于 2。
  • 不是构造函数:math 对象没有构造函数功能。所有属性和方法都是静态的。

数学对象方法和属性


1. math.abs(x)

返回 x 的绝对值。

console.log(math.abs(-10)); // 10
console.log(math.abs(5.5)); // 5.5

2. math.acos(x)

返回 x 的反余弦(反余弦),以弧度表示。

console.log(math.acos(1)); // 0
console.log(math.acos(0)); // 1.5707963267948966 (π/2)

3. math.acosh(x)

返回 x 的双曲反余弦。

console.log(math.acosh(1)); // 0
console.log(math.acosh(2)); // 1.3169578969248166

4. math.asin(x)

返回 x 的反正弦(反正弦),以弧度表示。

console.log(math.asin(0)); // 0
console.log(math.asin(1)); // 1.5707963267948966 (π/2)

5. math.asinh(x)

返回 x 的双曲反正弦。

console.log(math.asinh(0)); // 0
console.log(math.asinh(1)); // 0.881373587019543

6. math.atan(x)

返回 x 的反正切(反正切),以弧度表示。

console.log(math.atan(1)); // 0.7853981633974483 (π/4)
console.log(math.atan(0)); // 0

7. math.atan2(y, x)

返回其参数商的反正切值,以弧度表示。

console.log(math.atan2(1, 1)); // 0.7853981633974483 (π/4)
console.log(math.atan2(-1, -1)); // -2.356194490192345 (-3π/4)

8. math.atanh(x)

返回 x 的双曲反正切值。

console.log(math.atanh(0)); // 0
console.log(math.atanh(0.5)); // 0.5493061443340549

9. math.cbrt(x)

返回 x 的立方根。

console.log(math.cbrt(27)); // 3
console.log(math.cbrt(-8)); // -2

10。 math.ceil(x)

将 x 向上舍入到最接近的整数。

console.log(math.ceil(4.2)); // 5
console.log(math.ceil(-4.7)); // -4

11。 math.clz32(x)

返回 x 的 32 位二进制表示形式中前导零的数量。

console.log(math.clz32(1)); // 31
console.log(math.clz32(0x80000000)); // 0

12。数学.cos(x)

返回 x 的余弦(其中 x 的单位是弧度)。

console.log(math.cos(0)); // 1
console.log(math.cos(math.pi)); // -1

13。 math.cosh(x)

返回 x 的双曲余弦。

console.log(math.cosh(0)); // 1
console.log(math.cosh(1)); // 1.5430806348152437

14。数学.e

返回欧拉数,大约为 2.718。

console.log(math.e); // 2.718281828459045

15。 math.exp(x)

返回 e 的 x 次方的值。

console.log(math.exp(1)); // 2.718281828459045
console.log(math.exp(0)); // 1

16。 math.expm1(x)

返回 e 的 x 次方减去 1 的值。

console.log(math.expm1(1)); // 1.718281828459045
console.log(math.expm1(0)); // 0

17。 math.floor(x)

将 x 向下舍入到最接近的整数。

console.log(math.floor(4.7)); // 4
console.log(math.floor(-4.2)); // -5

18。 math.fround(x)

返回 x 的最接近的(32 位单精度)浮点表示形式。

console.log(math.fround(1.337)); // 1.336914
console.log(math.fround(1.5)); // 1.5

19。数学.ln2

返回 2 的自然对数,大约为 0.693。

console.log(math.ln2); // 0.6931471805599453

20。数学.ln10

返回 10 的自然对数,大约为 2.302。

console.log(math.ln10); // 2.302585092994046

21。 math.log(x)

返回 x 的自然对数(以 e 为底)。

console.log(math.log(math.e)); // 1
console.log(math.log(10)); // 2.302585092994046

22。 math.log10(x)

返回 x 以 10 为底的对数。

console.log(math.log10(10)); // 1
console.log(math.log10(100)); // 2

23。数学.log10e

返回 e 以 10 为底的对数,大约为 0.434。

console.log(math.log10e); // 0.4342944819032518

24。 math.log1p(x)

返回 1 + x 的自然对数。

console.log(math.log1p(1)); // 0.6931471805599453
console.log(math.log1p(0)); // 0

25。 math.log2(x)

返回 x 以 2 为底的对数。

console.log(math.log2(2)); // 1
console.log(math.log2(8)); // 3

26。数学.log2e

返回 e 以 2 为底的对数,大约为 1.442。

console.log(math.log2e); // 1.4426950408889634

27。 math.max(...值)

返回零个或多个数字中最大的一个。

console.log(math.max(1, 5, 3)); // 5
console.log(math.max(-1, -5, -3)); // -1

28。 math.min(...值)

返回零个或多个数字中最小的一个。

console.log(math.min(1, 5, 3)); // 1
console.log(math.min(-1, -5, -3)); // -5

29。数学.pi

返回 π 的值,大约为 3.14159。

console.log(math.pi); // 3.141592653589793

30。 math.pow(底数, 指数)

返回底数的指数次方的值。

console.log(math.pow(2, 3)); // 8
console.log(math.pow(5, 0)); // 1

31。 math.random()

返回 0(含)和 1(不包括)之间的伪随机数。

console.log(math.random()); // e.g., 0.237

32。 math.round(x)

将 x 舍入到最接近的整数。

console.log(math.round(4.5)); // 5
console.log(math.round(4.4)); // 4

33。 math.sign(x)

返回数字的符号,指示该数字是正数、负数还是零。

console.log(math.sign(-5)); // -1
console.log(math.sign(0)); // 0
console.log(math.sign(5)); // 1

34。数学.sin(x)

返回 x 的正弦值(其中 x 的单位是弧度)。

console.log(math.sin(0)); // 0
console.log(math.sin(math.pi / 2)); // 1

35。 math.sinh(x)

返回 x 的双曲正弦值。

console.log(math.sinh(0)); // 0
console.log(math.sinh(1)); // 1.1752011936438014

36。 math.sqrt(x)

返回 x 的平方根。

console.log(math.sqrt(9)); // 3
console.log(math.sqrt(16));

 // 4

37。数学.sqrt1_2

返回 1/2 的平方根,大约为 0.707。

console.log(math.sqrt1_2); // 0.7071067811865476

38。数学.sqrt2

返回 2 的平方根,大约为 1.414。

console.log(math.sqrt2); // 1.4142135623730951

39。 math.tan(x)

返回 x 的正切值(其中 x 的单位是弧度)。

console.log(math.tan(0)); // 0
console.log(math.tan(math.pi / 4)); // 1

40。 math.tanh(x)

返回 x 的双曲正切。

console.log(math.tanh(0)); // 0
console.log(math.tanh(1)); // 0.7615941559557649

41。 math.trunc(x)

通过删除任何小数位来返回数字的整数部分。

console.log(Math.trunc(4.9)); // 4
console.log(Math.trunc(-4.9)); // -4

今天关于《掌握 JavaScript 的数学对象:内置数学函数和属性的综合指南》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>