首页 > web前端 > js教程 > 正文

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

WBOY
发布: 2024-09-08 20:34:08
原创
885 人浏览过

Mastering 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)

Returns the hyperbolic cosine of x.

console.log(Math.cosh(0)); // 1
console.log(Math.cosh(1)); // 1.5430806348152437
登录后复制

14. Math.E

Returns Euler's number, approximately 2.718.

console.log(Math.E); // 2.718281828459045
登录后复制

15. Math.exp(x)

Returns the value of e raised to the power of x.

console.log(Math.exp(1)); // 2.718281828459045
console.log(Math.exp(0)); // 1
登录后复制

16. Math.expm1(x)

Returns the value of e raised to the power of x, minus 1.

console.log(Math.expm1(1)); // 1.718281828459045
console.log(Math.expm1(0)); // 0
登录后复制

17. Math.floor(x)

Rounds x downwards to the nearest integer.

console.log(Math.floor(4.7)); // 4
console.log(Math.floor(-4.2)); // -5
登录后复制

18. Math.fround(x)

Returns the nearest (32-bit single precision) float representation of x.

console.log(Math.fround(1.337)); // 1.336914
console.log(Math.fround(1.5)); // 1.5
登录后复制

19. Math.LN2

Returns the natural logarithm of 2, approximately 0.693.

console.log(Math.LN2); // 0.6931471805599453
登录后复制

20. Math.LN10

Returns the natural logarithm of 10, approximately 2.302.

console.log(Math.LN10); // 2.302585092994046
登录后复制

21. Math.log(x)

Returns the natural logarithm (base e) of x.

console.log(Math.log(Math.E)); // 1
console.log(Math.log(10)); // 2.302585092994046
登录后复制

22. Math.log10(x)

Returns the base-10 logarithm of x.

console.log(Math.log10(10)); // 1
console.log(Math.log10(100)); // 2
登录后复制

23. Math.LOG10E

Returns the base-10 logarithm of e, approximately 0.434.

console.log(Math.LOG10E); // 0.4342944819032518
登录后复制

24. Math.log1p(x)

Returns the natural logarithm of 1 + x.

console.log(Math.log1p(1)); // 0.6931471805599453
console.log(Math.log1p(0)); // 0
登录后复制

25. Math.log2(x)

Returns the base-2 logarithm of x.

console.log(Math.log2(2)); // 1
console.log(Math.log2(8)); // 3
登录后复制

26. Math.LOG2E

Returns the base-2 logarithm of e, approximately 1.442.

console.log(Math.LOG2E); // 1.4426950408889634
登录后复制

27. Math.max(...values)

Returns the largest of zero or more numbers.

console.log(Math.max(1, 5, 3)); // 5
console.log(Math.max(-1, -5, -3)); // -1
登录后复制

28. Math.min(...values)

Returns the smallest of zero or more numbers.

console.log(Math.min(1, 5, 3)); // 1
console.log(Math.min(-1, -5, -3)); // -5
登录后复制

29. Math.PI

Returns the value of π, approximately 3.14159.

console.log(Math.PI); // 3.141592653589793
登录后复制

30. Math.pow(base, exponent)

Returns the value of base raised to the power of exponent.

console.log(Math.pow(2, 3)); // 8
console.log(Math.pow(5, 0)); // 1
登录后复制

31. Math.random()

Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).

console.log(Math.random()); // e.g., 0.237
登录后复制

32. Math.round(x)

Rounds x to the nearest integer.

console.log(Math.round(4.5)); // 5
console.log(Math.round(4.4)); // 4
登录后复制

33. Math.sign(x)

Returns the sign of a number, indicating whether the number is positive, negative, or zero.

console.log(Math.sign(-5)); // -1
console.log(Math.sign(0)); // 0
console.log(Math.sign(5)); // 1
登录后复制

34. Math.sin(x)

Returns the sine of x (where x is in radians).

console.log(Math.sin(0)); // 0
console.log(Math.sin(Math.PI / 2)); // 1
登录后复制

35. Math.sinh(x)

Returns the hyperbolic sine of x.

console.log(Math.sinh(0)); // 0
console.log(Math.sinh(1)); // 1.1752011936438014
登录后复制

36. Math.sqrt(x)

Returns the square root of x.

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

 // 4
登录后复制

37. Math.SQRT1_2

Returns the square root of 1/2, approximately 0.707.

console.log(Math.SQRT1_2); // 0.7071067811865476
登录后复制

38. Math.SQRT2

Returns the square root of 2, approximately 1.414.

console.log(Math.SQRT2); // 1.4142135623730951
登录后复制

39. Math.tan(x)

Returns the tangent of x (where x is in radians).

console.log(Math.tan(0)); // 0
console.log(Math.tan(Math.PI / 4)); // 1
登录后复制

40. Math.tanh(x)

Returns the hyperbolic tangent of x.

console.log(Math.tanh(0)); // 0
console.log(Math.tanh(1)); // 0.7615941559557649
登录后复制

41. Math.trunc(x)

Returns the integer part of a number by removing any fractional digits.

console.log(Math.trunc(4.9)); // 4
console.log(Math.trunc(-4.9)); // -4
登录后复制

以上是掌握 JavaScript 的数学对象:内置数学函数和属性的综合指南的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!