Math 数学对象

重要通知

基本概况

Math:数学对象

数学对象属性

Math.PI:一个π值,即180度,返回圆周率(3.1415926 - 3.1415927) | Math.PI/2 | Math.PI/4 Math.SQRT2:返回2的平方根值
Math.E Math.LN2 Math.LN10 Math.LOG2E Math.LOG10E Math.SQRT1_2 Math.SQRT2

数学对象方法

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';

Last Updated:
Contributors: 709992523