
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 对象的实际示例:
立即学习“Java免费学习笔记(深入)”;
生成随机整数
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)); // 10console.log(math.abs(5.5)); // 5.5
2. math.acos(x)
返回 x 的反余弦(反余弦),以弧度表示。
console.log(math.acos(1)); // 0console.log(math.acos(0)); // 1.5707963267948966 (π/2)
3. math.acosh(x)
返回 x 的双曲反余弦。
console.log(math.acosh(1)); // 0console.log(math.acosh(2)); // 1.3169578969248166
4. math.asin(x)
返回 x 的反正弦(反正弦),以弧度表示。
console.log(math.asin(0)); // 0console.log(math.asin(1)); // 1.5707963267948966 (π/2)
5. math.asinh(x)
返回 x 的双曲反正弦。
console.log(math.asinh(0)); // 0console.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)); // 0console.log(math.atanh(0.5)); // 0.5493061443340549
9. math.cbrt(x)
返回 x 的立方根。
console.log(math.cbrt(27)); // 3console.log(math.cbrt(-8)); // -2
10。 math.ceil(x)
将 x 向上舍入到最接近的整数。
console.log(math.ceil(4.2)); // 5console.log(math.ceil(-4.7)); // -4
11。 math.clz32(x)
返回 x 的 32 位二进制表示形式中前导零的数量。
console.log(math.clz32(1)); // 31console.log(math.clz32(0x80000000)); // 0
12。数学.cos(x)
返回 x 的余弦(其中 x 的单位是弧度)。
console.log(math.cos(0)); // 1console.log(math.cos(math.pi)); // -1
13。 math.cosh(x)
返回 x 的双曲余弦。
console.log(math.cosh(0)); // 1console.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.718281828459045console.log(math.exp(0)); // 1
16。 math.expm1(x)
返回 e 的 x 次方减去 1 的值。
console.log(math.expm1(1)); // 1.718281828459045console.log(math.expm1(0)); // 0
17。 math.floor(x)
将 x 向下舍入到最接近的整数。
console.log(math.floor(4.7)); // 4console.log(math.floor(-4.2)); // -5
18。 math.fround(x)
返回 x 的最接近的(32 位单精度)浮点表示形式。
console.log(math.fround(1.337)); // 1.336914console.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)); // 1console.log(math.log(10)); // 2.302585092994046
22。 math.log10(x)
返回 x 以 10 为底的对数。
console.log(math.log10(10)); // 1console.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.6931471805599453console.log(math.log1p(0)); // 0
25。 math.log2(x)
返回 x 以 2 为底的对数。
console.log(math.log2(2)); // 1console.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)); // 5console.log(math.max(-1, -5, -3)); // -1
28。 math.min(…值)
返回零个或多个数字中最小的一个。
console.log(math.min(1, 5, 3)); // 1console.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)); // 8console.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)); // 5console.log(math.round(4.4)); // 4
33。 math.sign(x)
返回数字的符号,指示该数字是正数、负数还是零。
console.log(math.sign(-5)); // -1console.log(math.sign(0)); // 0console.log(math.sign(5)); // 1
34。数学.sin(x)
返回 x 的正弦值(其中 x 的单位是弧度)。
console.log(math.sin(0)); // 0console.log(math.sin(math.pi / 2)); // 1
35。 math.sinh(x)
返回 x 的双曲正弦值。
console.log(math.sinh(0)); // 0console.log(math.sinh(1)); // 1.1752011936438014
36。 math.sqrt(x)
返回 x 的平方根。
console.log(math.sqrt(9)); // 3console.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)); // 0console.log(math.tan(math.pi / 4)); // 1
40。 math.tanh(x)
返回 x 的双曲正切。
console.log(math.tanh(0)); // 0console.log(math.tanh(1)); // 0.7615941559557649
41。 math.trunc(x)
通过删除任何小数位来返回数字的整数部分。
console.log(Math.trunc(4.9)); // 4console.log(Math.trunc(-4.9)); // -4
以上就是掌握 JavaScript 的数学对象:内置数学函数和属性的综合指南的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1491259.html
微信扫一扫
支付宝扫一扫