Skip to content

Math 数学对象 ​

重要通知

。

基本概况 ​

静态属性 ​

  • Math.E:欧拉数,即自然对数的底数,约等于 2.718。
  • Math.LN10:10 的自然对数,约等于 2.303。
  • Math.LN2:2 的自然对数,约等于 0.693。
  • Math.LOG10E:以 10 为底的 E 的对数,约等于 0.434。
  • Math.LOG2E:以 2 为底的 E 的对数,约等于 1.443。
  • Math.PI:一个圆的周长和直径之比,约等于 3.14159,一个π值,即180度,返回圆周率(3.1415926 - 3.1415927),常见Math.PI/2 | Math.PI/4。
  • Math.SQRT1_2:½(二分之一)的平方根,约等于 0.707。
  • Math.SQRT2:返回2的平方根值,约等于 1.414。

静态方法 ​

Math.abs() 返回一个数的绝对值。

Math.acos() 返回一个数的反余弦值。

Math.acosh() 返回一个数的反双曲余弦值。

Math.asin() 返回一个数的反正弦值。

Math.asinh() 返回一个数的反双曲正弦值。

Math.atan() 返回一个数的反正切值。

Math.atan2() 返回其两个参数之商的反正切值。

Math.atanh() 返回一个数的反双曲正切值。

Math.cbrt() 返回一个数的立方根。

Math.ceil() 返回大于等于一个数的最小整数。

Math.clz32() 返回一个 32 位整数的前导零的数量。

Math.cos() 返回一个数的余弦值。

Math.cosh() 返回一个数的双曲余弦值。

Math.exp() 返回 ex,其中 x 为参数,e 是欧拉常数(2.718...,自然对数的底数)。

Math.expm1() 返回 exp(x) - 1 的值。

Math.floor() 返回小于等于一个数的最大整数。

Math.f16round() 返回最接近一个数的半精度浮点型表示。

Math.fround() 返回最接近一个数的单精度浮点型表示。

Math.hypot() 返回其所有参数平方和的平方根。

Math.imul() 返回 32 位整数乘法的结果。

Math.log() 返回一个数的自然对数(㏒e,即 ㏑)。

Math.log10() 返回一个数以 10 为底数的对数。

Math.log1p() 返回 1 + x 的和的 x 的自然对数(㏒e,即 ㏑)。

Math.log2()} 返回一个数以 2 为底数的对数。

Math.max() 返回零到多个数值中最大值。

Math.min() 返回零到多个数值中最小值。

Math.pow() 返回 x 的 y 次幂(也就是 xy)。

Math.random() 返回一个 0 到 1 之间的伪随机数。

Math.round() 返回四舍五入后的整数。

Math.sign() 返回一个数的符号,表示它是正数、负数还是 0。

Math.sin() 返回一个数的正弦值。

Math.sinh() 返回一个数的双曲正弦值。

Math.sqrt() 返回一个数的平方根。

Math.sumPrecise() 返回传入的可迭代数字集合的和,同时避免中间结果出现浮点精度损失。

Math.tan() 返回一个数的正切值。

Math.tanh() 返回一个数的双曲正切值。

Math.trunc() 返回一个数的整数部分,直接去除其小数点及之后的部分。

数学对象方法 ​

Math.ceil(number):返回大于或等于number的最小整数 [向上求整(包含5)] Math.floor(number):返回小于或等于number的最大整数 [向下求整(不包含5)]

console.log(Math.ceil(1.5)); // 输出:2
console.log(Math.floor(1.5)); // 输出:1

Math.exp(number):返回e的number次幂 Math.expm1(x) // 计算 e 的 x 次方减 1 的结果,即 Math.exp(x) - 1

Math.log(number):返回number的自然对数
Math.pow(x,y):返回x的y次幂

四舍五入 ​

Math.round(number):返回number整数(四舍五入处理)

最大值与最小值 ​

Math.max(num1,num2,...,numn):返回num1,num2,...,numn中的最大值
Math.min(num1,num2,...,numn):返回num1,num2,...,numn中的最小值

随机数 ​

Math.random():返回 0 ~ 1 之间的随机数,包含 0 不包含 1。 0<= Math.random() < 1 | Math.floor(Math.random()*10):返回值:1 - 9

const value = 20.23;
console.log(Math.ceil(value));   // 21
console.log(Math.round(value));  // 20
console.log(Math.floor(value));  // 20
console.log(Math.random());

Math.ceil(Math.random()*10);     // 获取从 1 到 10 的随机整数,取 0 的概率极小。
Math.round(Math.random());       // 可均衡获取 0 到 1 的随机整数。
Math.floor(Math.random()*10);    // 可均衡获取 0 到 9 的随机整数。
Math.round(Math.random()*10);    // 基本均衡获取 0 到 10 的随机整数,其中获取最小值 0 和最大值 10 的几率少一半。

三角函数 ​

Math.abs(number):返回number的绝对值

Math.acos(number):返回number的反余弦值,number参数的取值范围为-1.0—1.0
Math.asin(number):返回number的反正弦值,number参数的取值范围为-1.0—1.0
Math.atan(number):返回number的反正切值
Math.atan2(x,y):返回number的由X轴到坐标(y,x)的角度

Math.tan(number):返回number正切值
Math.cos(number):返回number的余弦值 Math.sin(number):返回number正弦值

Math.sqrt(number):返回number平方根,number必须大于或等于0,否则返回NaN

Math.acosh(x) // 用于计算反双曲余弦 Math.asinh(x) // 用于计算反双曲正弦 Math.atanh(x) // 用于计算反双曲正切

Math.cbrt(x) // 用于计算一个数的立方根 Math.clz32(x) // 用于返回数字的32 位无符号整数形式的前导0的个数 Math.cosh(x) // 用于计算双曲余弦

Math.fround(x) // 用于获取数字的32位单精度浮点数形式 Math.hypot(x) // 用于计算所有参数的平方和的平方根

Math.imul(x) // 两个数以 32 位带符号整数形式相乘的结果,返回的也是一个 32 位的带符号整数

Math.log1p(x) // 计算1 + x 的自然对数,即 Math.log(1 + x) Math.log2(x) // 计算 2 为底的 x 的对数 Math.log10(x) // 计算 10 为底的 x 的对数

Math.sinh(x) // 用于计算双曲正弦

Math.tanh(x) // 用于计算双曲正切 Math.trunc(x) // 用于返回数字的整数部分

console.info(Math.tan(Math.PI/4)); // 0.9999999999999999,JS精度问题

截取小数点位数 ​

(number).toFixed(n):截取小数位数(四舍五入)

API接口 ​

Math.Symbol(Symbol.toStringTag)(x)

​

​

​

Math.js ​

Math.js 是一个广泛的 JavaScript 和 Node.js 数学库。它具有支持符号计算的灵活表达式解析器,带有大量内置函数和常量,并提供了一个集成的解决方案来处理不同的数据类型,如数字、大数、复数、分数、单位和矩阵。功能强大且易于使用。

big.js ​

一个用于任意精度十进制算术的小型快速 JavaScript 库。

mathlab ​

类似于 javascript 中的数学实验室,专注于矩阵操作,旨在成为 JavaScript 中最好的矩阵实验室。

代码示例 ​

import { 
  Complex, Sparse, abs, acos, add, and, asin, atan, atan2, band, bnot, bor, bxor, ceil, clone, cos, det, diag, dim,
  div, dot, eig, epsilon, eq, exp, floor, geq, getBlock, getDiag, gt, identity, inv, leq, linspace, log, lshift, lt,
  mod, mul, neg, negtranspose, neq, norm2, norm2Squared, not, or, pointwise, pointwise2, pow, random, reciprocal, rep,
  round, rrshift, rshift, same, setBlock, sin, sqrt, sub, tan, tensor, transpose, fft, ifft 
} from 'mathlab';

glMatrix ​

glMatrix是一个注重速度和高性能的向量和矩阵数学库。

import { glMatrix, mat2, mat2d, mat3, mat4, quat, quat2, vec2, vec3, vec4 } from 'glMatrix';

​

Released under the MIT License.