# Math

Math 是一个内置对象,它拥有一些数学常数属性和数学函数方法。

# 静态属性

# E

Math.E 属性表示自然对数的底数(或称为基数),e,约等于 2.718。

function getNapier():number {
  return Math.E;
}
console.log(getNapier());
// expected output: 2.718281828459045

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# LN10

Math.LN10 属性表示 10 的自然对数,约为 2.302

function getNatLog10():number {
  return Math.LN10;
}
console.log(getNatLog10());
// expected output: 2.302585092994046

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# LN2

Math.LN2 属性表示 2 的自然对数,约为 0.693

function getNatLog2():number {
  return Math.LN2;
}
console.log(getNatLog2());
// expected output: 0.6931471805599453

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# LOG2E

Math.LOG2E 属性表示以 2 为底数,e 的对数,约为 1.442

function getLog2e():number {
  return Math.LOG2E;
}
console.log(getLog2e());
// expected output: 1.4426950408889634

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# LOG10E

Math.LOG10E 属性表示以 10 为底数,e 的对数,约为 0.434

function getLog10e():number {
  return Math.LOG10E;
}
console.log(getLog10e());
// expected output: 0.4342944819032518

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# PI

Math.PI 表示一个圆的周长与直径的比例,约为 3.14159

function calculateCircumference (radius:number):number {
  return 2 * Math.PI * radius;
}
console.log(calculateCircumference(1));
// expected output: 6.283185307179586

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# SQRT1_2

Math.SQRT1_2 属性表示 1/2 的平方根,约为 0.707

function getRoot1_2():number {
  return Math.SQRT1_2;
}
console.log(getRoot1_2());
// expected output: 0.7071067811865476

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# SQRT2

Math.SQRT2 属性表示 2 的平方根,约为 1.414

function getRoot2():number {
  return Math.SQRT2;
}
console.log(getRoot2());
// expected output: 1.4142135623730951

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# 静态方法

# clz32(x)

Returns the number of leading zero bits in the 32-bit binary representation of a number.

Parameters

name type required description
x number YES A numeric expression.

Return value

Type
number
console.log(Math.clz32(1));
// expected output: 31

console.log(Math.clz32(1000));
// expected output: 22

console.log(Math.clz32());
// expected output: 32

console.log(Math.clz32(3.5));
// expected output: 30

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# sign(x)

Returns the sign of the x, indicating whether x is positive, negative or zero.

Parameters

name type required description
x number YES The numeric expression to test

Return value

Type
number
console.log(Math.sign(3));
// expected output: 1

console.log(Math.sign(-3));
// expected output: -1

console.log(Math.sign(0));
// expected output: 0

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# log10(x)

Returns the base 10 logarithm of a number.

Parameters

name type required description
x number YES A numeric expression.

Return value

Type
number
console.log(Math.log10(10));
// expected output: 1

console.log(Math.log10(100));
// expected output: 2

console.log(Math.log10(1));
// expected output: 0

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# log2(x)

Returns the base 2 logarithm of a number.

Parameters

name type required description
x number YES A numeric expression.

Return value

Type
number
console.log(Math.log2(2));
// expected output: 1

console.log(Math.log2(1024));
// expected output: 10

console.log(Math.log2(1));
// expected output: 0

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# log1p(x)

Returns the natural logarithm of 1 + x.

Parameters

name type required description
x number YES A numeric expression.

Return value

Type
number
console.log(Math.log1p(Math.E-1));
// expected output: 1

console.log(Math.log1p(0));
// expected output: 0

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# expm1(x)

Returns the result of (e^x - 1), which is an implementation-dependent approximation to subtracting 1 from the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms).

Parameters

name type required description
x number YES A numeric expression.

Return value

Type
number
console.log(Math.expm1(1));
// expected output: 1.718281828459045

console.log(Math.expm1(-38));
// expected output: -1

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# cosh(x)

Returns the hyperbolic cosine of a number.

Parameters

name type required description
x number YES A numeric expression that contains an angle measured in radians.

Return value

Type
number
console.log(Math.cosh(0));
// expected output: 1

console.log(Math.cosh(1));
// expected output: 1.5430806348152437

console.log(Math.cosh(-1));
// expected output: 1.5430806348152437

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# sinh(x)

Returns the hyperbolic sine of a number.

Parameters

name type required description
x number YES A numeric expression that contains an angle measured in radians.

Return value

Type
number
console.log(Math.sinh(0));
// expected output: 0

console.log(Math.sinh(1));
// expected output: 1.1752011936438014

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# tanh(x)

Returns the hyperbolic tangent of a number.

Parameters

name type required description
x number YES A numeric expression that contains an angle measured in radians.

Return value

Type
number
console.log(Math.tanh(-1));
// Expected output: -0.7615941559557649

console.log(Math.tanh(0));
// Expected output: 0

console.log(Math.tanh(1));
// Expected output: 0.7615941559557649

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# acosh(x)

Returns the inverse hyperbolic cosine of a number.

Parameters

name type required description
x number YES A numeric expression that contains an angle measured in radians.

Return value

Type
number
console.log(Math.acosh(1));
// expected output: 0

console.log(Math.acosh(2));
// expected output: 1.3169578969248166

console.log(Math.acosh(2.5));
// expected output: 1.566799236972411

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# asinh(x)

Returns the inverse hyperbolic sine of a number.

Parameters

name type required description
x number YES A numeric expression that contains an angle measured in radians.

Return value

Type
number
console.log(Math.asinh(1));
// expected output: 0.881373587019543

console.log(Math.asinh(0));
// expected output: 0

console.log(Math.asinh(-1));
// expected output: -0.881373587019543

console.log(Math.asinh(2));
// expected output: 1.4436354751788103

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# atanh(x)

Returns the inverse hyperbolic tangent of a number.

Parameters

name type required description
x number YES A numeric expression that contains an angle measured in radians.

Return value

Type
number
console.log(Math.atanh(0));
// expected output: 0

console.log(Math.atanh(0.5));
// expected output: 0.5493061443340548

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# trunc(x)

Returns the integral part of the a numeric expression, x, removing any fractional digits. If x is already an integer, the result is x.

Parameters

name type required description
x number YES A numeric expression.

Return value

Type
number
console.log(Math.trunc(13.37));
// Expected output: 13

console.log(Math.trunc(42.84));
// Expected output: 42

console.log(Math.trunc(0.123));
// Expected output: 0

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# fround(x)

Returns the nearest single precision float representation of a number.

Parameters

name type required description
x number YES A numeric expression.

Return value

Type
number
console.log(Math.fround(1.5));
// expected output: 1.5

console.log(Math.fround(1.337));
// expected output: 1.3370000123977661

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 3.9.0 x

# abs(x)

RMath.abs(x) 函数返回一个数字的绝对值。

Parameters

name type required description
x number YES 一个数字

Return value

Type description
number x 的绝对值。如果 x 是负数(包括 -0),则返回 -x。否则,返回 x
function difference(a:number, b:number):number {
  return Math.abs(a - b);
}

console.log(difference(3, 5));
// expected output: 2

console.log(difference(5, 3));
// expected output: 2

console.log(difference(1.23456, 7.89012));
// expected output: 6.6555599999999995

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# acos(x)

Math.acos() 返回一个数的反余弦值(单位为弧度)

Parameters

name type required description
x number YES 一个数值.

Return value

Type
number
console.log(Math.acos(-1));
// expected output: 3.141592653589793

console.log(Math.acos(0));
// expected output: 1.5707963267948966

console.log(Math.acos(1));
// expected output: 0

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# asin(x)

Math.asin() 方法返回一个数值的反正弦(单位为弧度)

Parameters

name type required description
x number YES 一个数值

Return value

Type
number
console.log(Math.asin(-1));
// expected output: -1.5707963267948966 (-pi/2)

console.log(Math.asin(0));
// expected output: 0

console.log(Math.asin(0.5));
// expected output: 0.5235987755982989

console.log(Math.asin(1));
// expected output: 1.5707963267948966

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# atan(x)

Math.atan() 函数返回一个数值的反正切(以弧度为单位)

Parameters

name type required description
x number YES 一个数值

Return value

Type
number
console.log(Math.atan(1));
// expected output: 0.7853981633974483

console.log(Math.atan(0));
// expected output: 0

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# atan2(y, x)

Math.atan2() 返回从原点 (0,0) 到 (x,y) 点的线段与 x 轴正方向之间的平面角度 (弧度值),也就是 Math.atan2(y,x)

Parameters

name type required description
y number YES 数值
x number YES 数值

Return value

Type
number
console.log(Math.atan2(90, 15));
// expected output: 1.4056476493802699

console.log(Math.atan2(15, 90));
// expected output: 0.16514867741462683

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# ceil(x)

Math.ceil() 函数总是四舍五入并返回大于等于给定数字的最小整数。

Parameters

name type required description
x number YES 一个数值

Return value

Type
number
console.log(Math.ceil(0.95));
// expected output: 1

console.log(Math.ceil(4));
// expected output: 4

console.log(Math.ceil(7.004));
// expected output: 8

console.log(Math.ceil(-7.004));
// expected output: -7

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# cos(x)

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

Parameters

name type required description
x number YES 一个以弧度为单位的数值。

Return value

Type
number
console.log(Math.cos(0));
// expected output: 1

console.log(Math.cos(1));
// expected output: 0.5403023058681398

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# exp(x)

Math.exp() 函数返回 e^x,x 表示参数,e 是欧拉常数(Euler's constant),自然对数的底数。

Parameters

name type required description
x number YES 一个数值

Return value

Type
number
console.log(Math.exp(-1));
// expected output: 0.36787944117144233

console.log(Math.exp(0));
// expected output: 1

console.log(Math.exp(1));
// expected output: 2.718281828459045

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# floor(x)

Math.floor() 函数总是返回小于等于一个给定数字的最大整数。

Parameters

name type required description
x number YES 一个数字。

Return value

Type
number
console.log(Math.floor(5.95));
// expected output: 5

console.log(Math.floor(5.05));
// expected output: 5

console.log(Math.floor(5));
// expected output: 5

console.log(Math.floor(-5.05));
// expected output: -6

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# log(x)

Math.log() 函数返回一个数的自然对数

Parameters

name type required description
x number YES 一个数字。

Return value

Type
number
console.log(Math.log(1));
// expected output: 0

console.log(Math.log(10));
// expected output: 2.302585092994046

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# max(values)

Math.max() 函数返回作为输入参数的最大数字,如果没有参数,则返回 -Infinity

Parameters

name type required description
values number[] YES 0 个或多个数字,将在其中选择,并返回最大的值。

Return value

Type description
number 给定数值中最大的数。如果任一参数不能转换为数值,则返回 NaN。如果没有提供参数,返回 -Infinity。
console.log(Math.max(1, 3, 2));
// expected output: 3

console.log(Math.max(-1, -3, -2));
// expected output: -1

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# min(values)

Math.min() 函数返回作为输入参数的数字中最小的一个,如果没有参数,则返回 Infinity。

Parameters

name type required description
values number[] YES 0 个或多个数字,将在其中选择,并返回最小值。

Return value

Type description
number 给定数值中最小的数。如果任一参数不能转换为数值,则返回 NaN。如果没有提供参数,返回 Infinity。
console.log(Math.min(2, 3, 1));
// expected output: 1

console.log(Math.min(-2, -3, -1));
// expected output: -3

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# pow(x, y)

Math.pow() 函数返回基数(base)的指数(exponent)次幂,即 base^exponent。

Parameters

name type required description
x number YES 基数
y number YES 指数

Return value

Type
number
console.log(Math.pow(7, 3));
// expected output: 343

console.log(Math.pow(4, 0.5));
// expected output: 2

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# random()

Math.random() 函数返回一个浮点数,伪随机数在范围从0 到小于1,也就是说,从 0(包括 0)往上,但是不包括 1(排除 1),然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。

Return value

Type description
number 一个浮点型伪随机数字,在0(包括 0)和1(不包括)之间。
function getRandomInt(max:number):number {
  return Math.floor(Math.random() * max);
}

console.log(getRandomInt(3));
// expected output: 0, 1 or 2

console.log(getRandomInt(1));
// expected output: 0

console.log(Math.random());
// expected output: a number from 0 to <1

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# round(x)

Math.round() 函数返回一个数字四舍五入后最接近的整数。

Parameters

name type required description
x number YES 一个数值。

Return value

Type description
number 给定数字的值四舍五入到最接近的整数。
console.log(Math.round(20.49));
// expected output: 20

console.log(Math.round(20.5));
// expected output: 21

console.log(Math.round(-20.5));
// expected output: -20

console.log(Math.round(-20.51));
// expected output: -21

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# sin(x)

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

Parameters

name type required description
x number YES 一个数值(以弧度为单位)。

Return value

Type
number
console.log(Math.sin(0));
// expected output: 0

console.log(Math.sin(1));
// expected output: 0.8414709848078965

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# sqrt(x)

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

Parameters

name type required description
x number YES 一个数值

Return value

Type
number
function calcHypotenuse(a:number, b:number):number {
  return (Math.sqrt((a * a) + (b * b)));
}

console.log(calcHypotenuse(3, 4));
// expected output: 5

console.log(calcHypotenuse(5, 12));
// expected output: 13

console.log(calcHypotenuse(0, 0));
// expected output: 0

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x

# tan(x)

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

Parameters

name type required description
x number YES 一个数值,表示一个角(单位:弧度)。

Return value

Type
number
console.log(Math.tan(0));
// expected output: 0

console.log(Math.tan(1));
// expected output: 1.5574077246549023

Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
4.4 3.9.0 9.0 x