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中文网其他相关文章!

JavaScript字符串替换方法详解及常见问题解答 本文将探讨两种在JavaScript中替换字符串字符的方法:在JavaScript代码内部替换和在网页HTML内部替换。 在JavaScript代码内部替换字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 该方法仅替换第一个匹配项。要替换所有匹配项,需使用正则表达式并添加全局标志g: str = str.replace(/fi

简单JavaScript函数用于检查日期是否有效。 function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2] '/' bits[1] '/' bits[0]); return !!(d && (d.getMonth() 1) == bits[1] && d.getDate() == Number(bits[0])); } //测试 var

本文探讨如何使用 jQuery 获取和设置 DOM 元素的内边距和外边距值,特别是元素外边距和内边距的具体位置。虽然可以使用 CSS 设置元素的内边距和外边距,但获取准确的值可能会比较棘手。 // 设置 $("div.header").css("margin","10px"); $("div.header").css("padding","10px"); 你可能会认为这段代码很

本文探讨了十个特殊的jQuery选项卡和手风琴。 选项卡和手风琴之间的关键区别在于其内容面板的显示和隐藏方式。让我们深入研究这十个示例。 相关文章:10个jQuery选项卡插件

发现十个杰出的jQuery插件,以提升您的网站的活力和视觉吸引力!这个精选的收藏品提供了不同的功能,从图像动画到交互式画廊。让我们探索这些强大的工具: 相关文章: 1

HTTP-Console是一个节点模块,可为您提供用于执行HTTP命令的命令行接口。不管您是否针对Web服务器,Web Serv

本教程向您展示了如何将自定义的Google搜索API集成到您的博客或网站中,提供了比标准WordPress主题搜索功能更精致的搜索体验。 令人惊讶的是简单!您将能够将搜索限制为Y

当div内容超出容器元素区域时,以下jQuery代码片段可用于添加滚动条。 (无演示,请直接复制到Firebug中) //D = document //W = window //$ = jQuery var contentArea = $(this), wintop = contentArea.scrollTop(), docheight = $(D).height(), winheight = $(W).height(), divheight = $('#c


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver Mac版
视觉化网页开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。