ECMAScript 中所有的数学常量和函数(完整表格+数学公式)

79 阅读4分钟

原文:www.shenlu.me/blog/ecmasc…

ECMAScript 中的数学全局对象

在 ECMAScript 中,有许多内置对象可以直接用作数学常量和函数。

值属性 (Value Properties)

值属性描述符号表示数值
Infinitypositive Infinity++\inftyInfinity (+Infinity)
NaNNot a NumberNaNNaNNaN

函数属性 (Function Properties)

函数属性描述返回类型
isFinite(number)determine whether a number is a finite number or not (Number.isFinite(Number(number)))boolean
isNaN(number)determine whether a value is NaN or not (Number(number) !== Number(number))boolean
parseFloat(string)parse an argument to a floating point numbernumber (float)
parseInt(string, radix)parse an argument to an integer of the specified radixnumber (integer)

构造函数属性(Constructor Properties)

  • BigInt: 一个内置对象,其构造函数返回一个 bigint 基元(或 BigInt),用于表示大于 253−12^{53} - 1253−1 的整数(Number.MAX_SAFE_INTEGER),这是 JavaScript 使用数值基元(或数值类型)能表示的最大数字。
  • Numbers: 一个原始包装对象,用于表示和操作数字,其类型是 双精度 64 位二进制格式 IEEE 754

BigInt

函数属性描述返回类型
asIntN(bits, bigint)clamp a BigInt value to a signed integer valuebigint
asUintN(bits, bigint)clamp a BigInt value to an unsigned integer valuebigint

Numbers

值属性描述符号表示数值
EPSILONthe magnitude of the difference between 1 and the smallest value greater than 1, which is approximately 2522^{-52}ε\varepsilon2.220446049250313e-16
NaNthe value identicial to the global NaNNaNNaNNaN
MAX_SAFE_INTEGERthe maximum safe integer in JavaScript25312^{53}-19007199254740991
MAX_VALUEthe largest positive representable number2102412^{1024}-11.7976931348623157e+308
MIN_SAFE_INTEGERthe minimum safe integer in JavaScript (253+1-2^{53} + 1)253+1-2^{53}+1-9007199254740991
MIN_VALUEthe smallest positive representable number—that is, the positive number closest to zero (without actually being zero)210742^{-1074}5e-324
NEGATIVE_INFINITYnegative infinity-\infty-Infinity
POSITIVE_INFINITYthe value identicial to the global Infinity++\inftyInfinity (+Infinity)
函数属性描述返回类型
isFinite(number)determine whether the passed value is a finite number or notboolean
isInteger(number)determine whether the passed value is an integer or notboolean
isNaN(number)determine whether the passed value is NaN or not (typeof number === 'number' && number !== number)boolean
isSafeInteger(number)determine whether the passed value is a safe integer (between 253+1-2^{53} + 1 and 25312^{53} - 1) or notboolean
parseFloat(string)the function identicial to the global parseFloatnumber (float)
parseInt(string, radix)the function identicial to the global parseIntnumber (integer)

注意

  1. Number.isFinite(number) 在判断给定的参数是否为 Infinity 时,不会先将其转换为 Number,这与全局的 isFinite(number) 函数不同。因此,Number.isNaN(number) 也有类似的行为。

其他属性 (Other Properties)

  • Math: 这是一个具有数学常量和函数属性与方法的内置对象,但它不是一个函数对象。Math 只能处理 Number 类型,不能与 BigInt 一起使用。

Math

值属性描述符号表示数值
Ethe base of the natural logarithmsee2.7182818284590452354
LN10the natural logarithm of 10ln(10)\ln(10)2.302585092994046
LN2the natural logarithm of 2ln(2)\ln(2)0.6931471805599453
LOG10Ethe base-10 logarithm of eelog10(e)\log_{10}(e)0.4342944819032518
LOG2Ethe base-2 logarithm of eelog2(e)\log_{2}(e)1.4426950408889634
PIthe ratio of the circumference of a circle to its diameterπ\pi3.1415926535897932
SQRT1_2the square root of ½12\frac{1}{\sqrt{2}}0.7071067811865476
SQRT2the square root of 22\sqrt{2}1.4142135623730951
函数属性描述符号表示返回类型
abs(x)return the absolute value of xx\lvert x \rvert number
acos(x)return the inverse cosine of xarccos(x)\arccos(x)number
acosh(x)return the inverse hyperbolic cosine of xarcosh(x)\operatorname{arcosh}(x)number
asin(x)return the inverse sine of xarcsin(x)\arcsin(x)number
asinh(x)return the inverse hyperbolic sine of xarsinh(x)\operatorname{arsinh}(x)number
atan(x)return the inverse tangent of xarctan(x)\arctan(x)number
atanh(x)return the inverse hyperbolic tangent of xarctanh(x)\operatorname{arctanh}(x)number
atan2(y, x)return the inverse tangent of the quotient y / x of the arguments y and xarctan(yx)arctan(\frac{y}{x})number
cbrt(x)return the cube root of xx3\sqrt[3]{x}number
ceil(x)return the smallest integer greater than or equal to xx\lceil x \rceilnumber
clz32(x)return the number of leading zero bits of the 32-bit integer xnumber
cos(x)return the cosine of xcos(x)\cos(x)number
cosh(x)return the hyperbolic cosine of xex+ex2\frac{e^x + e^{-x}}{2}number
exp(x)return the exponential function of xexe^xnumber
expm1(x)return the result of subtracting 1 from the exponential function of xex1e^x - 1number
floor(x)return the largest integer less than or equal to xx\lfloor x \rfloornumber
fround(x)return the nearest single precision float representation of xnumber
hypot(...args)return the square root of the sum of squares of its arguments._i=1nxn2\sqrt{\sum\_{i=1}^{n}{x_n^2}}number
imul(x, y)return the result of the 32-bit integer multiplication of x and y.number
log(x)return the natural logarithm of xln(x)\ln(x)number
log1p(x)return the natural logarithm of 1 + xln(1+x)\ln(1+x)number
log10(x)return the base 10 logarithm of xlog10x\log_{10} xnumber
log2(x)return the base 2 logarithm of xlog2x\log_{2} xnumber
max(...args)return the largest of the resulting valuesmax{f(x1),...,f(xn)}\max \left \{ f\left ( x_1 \right ) ,...,f\left ( x_n \right ) \right \}number
min(...args)return the smallest of the resulting valuesmin{f(x1),...,f(xn)}\min \left \{ f\left ( x_1 \right ) ,...,f\left ( x_n \right ) \right \}number
pow(base, exponent)return base x to the exponent power y (that is, x^y)xyx^ynumber
random()return a pseudo-random number between 0 and 1x(0,1]x \in \left (0, 1 \right ]number
round(x)return the value of the number x rounded to the nearest integerx+12\lfloor x + \frac{1}{2} \rfloornumber
sign(x)return the sign of the x, indicating whether x is positive, negative, or zero±\pm number
sin(x)return the sine of xsinx\sin xnumber
sinh(x)return the hyperbolic sine of xexex2\frac{e^x - e^{-x}}{2}number
sqrt(x)return the positive square root of xx\sqrt{x}number
tan(x)return the tangent of xtan(x)\tan(x)number
tanh(x)return the hyperbolic tangent of xe2x1e2x+1\frac{e^{2x} - 1}{e^{2x}+1}number
trunc(x)return the integer portion of x, removing any fractional digitsnumber

注意

  1. 建议使用 x ** y 来实现 x ^ y,而不是 Math.pow(x, y)
  2. 建议使用 ~~x 来获取给定数字的整数部分,而不是 Math.trunc(x)

文中所有的数学公式在 Mathcheap.xyz 中编辑辅助完成

opengraph-image.png