JavaScript手册 
目录
此参考中包含
的内容
轻松上手
简介
操作符
语句
核心
文档
窗口
表单
浏览器
事件和
事件句柄
LiveWire
数据库服务
进程管理服务
实用工具
全局函数
LiveConnect
的Java包
索引
版权
 
【目录】 【上一页】 【下一页】 【索引】

Math

A built-in object that has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi.

Core object.
实现版本Navigator 2.0, LiveWire 1.0

创建源

The Math object is a top-level, predefined JavaScript object. You can automatically access it without using a constructor or calling a method.

描述

All properties and methods of Math are static. You refer to the constant PI as Math.PI and you call the sine function as Math.sin(x), where x is the method's argument. Constants are defined with the full precision of real numbers in JavaScript.

It is often convenient to use the with statement when a section of code uses several Math constants and methods, so you don't have to type "Math" repeatedly. For example,

with (Math) {
   a = PI * r*r
   y = r*sin(theta)
   x = r*cos(theta)
}

属性概览

EEuler's constant and the base of natural logarithms, approximately 2.718.
LN10Natural logarithm of 10, approximately 2.302.
LN2Natural logarithm of 2, approximately 0.693.
LOG10EBase 10 logarithm of E (approximately 0.434).
LOG2EBase 2 logarithm of E (approximately 1.442).
PIRatio of the circumference of a circle to its diameter, approximately 3.14159.
SQRT1_2Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.
SQRT2Square root of 2, approximately 1.414.

方法概览

absReturns the absolute value of a number.
acosReturns the arccosine (in radians) of a number.
asinReturns the arcsine (in radians) of a number.
atanReturns the arctangent (in radians) of a number.
atan2Returns the arctangent of the quotient of its arguments.
ceilReturns the smallest integer greater than or equal to a number.
cosReturns the cosine of a number.
expReturns Enumber, where number is the argument, and E is Euler's constant, the base of the natural logarithms.
floorReturns the largest integer less than or equal to a number.
logReturns the natural logarithm (base E) of a number.
maxReturns the greater of two numbers.
minReturns the lesser of two numbers.
powReturns base to the exponent power, that is, baseexponent.
randomReturns a pseudo-random number between 0 and 1.
roundReturns the value of a number rounded to the nearest integer.
sinReturns the sine of a number.
sqrtReturns the square root of a number.
tanReturns the tangent of a number.

属性

E

Euler's constant and the base of natural logarithms, approximately 2.718.

属性源Math
静态, 只读
实现版本Navigator 2.0, LiveWire 1.0

示例

The following function returns Euler's constant:

function getEuler() {
   return Math.E
}

描述

Because E is a static property of Math, you always use it as Math.E, rather than as a property of a Math object you created.

LN10

The natural logarithm of 10, approximately 2.302.

属性源Math
静态, 只读
实现版本Navigator 2.0, LiveWire 1.0

示例

The following function returns the natural log of 10:

function getNatLog10() {
   return Math.LN10
}

描述

Because LN10 is a static property of Math, you always use it as Math.LN10, rather than as a property of a Math object you created.

LN2

The natural logarithm of 2, approximately 0.693.

属性源Math
静态, 只读
实现版本Navigator 2.0, LiveWire 1.0

示例

The following function returns the natural log of 2:

function getNatLog2() {
   return Math.LN2
}

描述

Because LN2 is a static property of Math, you always use it as Math.LN2, rather than as a property of a Math object you created.

LOG10E

The base 10 logarithm of E (approximately 0.434).

属性源Math
静态, 只读
实现版本Navigator 2.0, LiveWire 1.0

示例

The following function returns the base 10 logarithm of E:

function getLog10e() {
   return Math.LOG10E
}

描述

Because LOG10E is a static property of Math, you always use it as Math.LOG10E, rather than as a property of a Math object you created.

LOG2E

The base 2 logarithm of E (approximately 1.442).

属性源Math
静态, 只读
实现版本Navigator 2.0, LiveWire 1.0

示例

The following function returns the base 2 logarithm of E:

function getLog2e() {
   return Math.LOG2E
}

描述

Because LOG2E is a static property of Math, you always use it as Math.LOG2E, rather than as a property of a Math object you created.

PI

The ratio of the circumference of a circle to its diameter, approximately 3.14159.

属性源Math
静态, 只读
实现版本Navigator 2.0, LiveWire 1.0

示例

The following function returns the value of pi:

function getPi() {
   return Math.PI
}

描述

Because PI is a static property of Math, you always use it as Math.PI, rather than as a property of a Math object you created.

SQRT1_2

The square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.

属性源Math
静态, 只读
实现版本Navigator 2.0, LiveWire 1.0

示例

The following function returns 1 over the square root of 2:

function getRoot1_2() {
   return Math.SQRT1_2
}

描述

Because SQRT1_2 is a static property of Math, you always use it as Math.SQRT1_2, rather than as a property of a Math object you created.

SQRT2

The square root of 2, approximately 1.414.

属性源Math
静态, 只读
实现版本Navigator 2.0, LiveWire 1.0

示例

The following function returns the square root of 2:

function getRoot2() {
   return Math.SQRT2
}

描述

Because SQRT2 is a static property of Math, you always use it as Math.SQRT2, rather than as a property of a Math object you created.

方法

abs

Returns the absolute value of a number.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

abs(x)

参数

xA number

示例

The following function returns the absolute value of the variable x:

function getAbs(x) {
   return Math.abs(x)
}

描述

Because abs is a static method of Math, you always use it as Math.abs(), rather than as a method of a Math object you created.

acos

Returns the arccosine (in radians) of a number.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

acos(x)

参数

xA number

描述

The acos method returns a numeric value between 0 and pi radians. If the value of number is outside this range, it returns 0.

Because acos is a static method of Math, you always use it as Math.acos(), rather than as a method of a Math object you created.

示例

The following function returns the arccosine of the variable x:

function getAcos(x) {
   return Math.acos(x)
}
If you pass -1 to getAcos, it returns 3.141592653589793; if you pass 2, it returns 0 because 2 is out of range.

参看

Math.asin, Math.atan, Math.atan2, Math.cos, Math.sin, Math.tan

asin

Returns the arcsine (in radians) of a number.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

asin(x)

参数

xA number

描述

The asin method returns a numeric value between -pi/2 and pi/2 radians. If the value of number is outside this range, it returns 0.

Because asin is a static method of Math, you always use it as Math.asin(), rather than as a method of a Math object you created.

示例

The following function returns the arcsine of the variable x:

function getAsin(x) {
   return Math.asin(x)
}
If you pass getAsin the value 1, it returns 1.570796326794897 (pi/2); if you pass it the value 2, it returns 0 because 2 is out of range.

参看

Math.acos, Math.atan, Math.atan2, Math.cos, Math.sin, Math.tan

atan

Returns the arctangent (in radians) of a number.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

atan(x)

参数

xA number

描述

The atan method returns a numeric value between -pi/2 and pi/2 radians.

Because atan is a static method of Math, you always use it as Math.atan(), rather than as a method of a Math object you created.

示例

The following function returns the arctangent of the variable x:

function getAtan(x) {
   return Math.atan(x)
}
If you pass getAtan the value 1, it returns 0.7853981633974483; if you pass it the value .5, it returns 0.4636476090008061.

参看

Math.acos, Math.asin, Math.atan2, Math.cos, Math.sin, Math.tan

atan2

Returns the arctangent of the quotient of its arguments.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

atan2(y, x)

参数

y, xNumber

描述

The atan2 method returns a numeric value between -pi and pi representing the angle theta of an (x,y) point. This is the counterclockwise angle, measured in radians, between the positive X axis, and the point (x,y). Note that the arguments to this function pass the y-coordinate first and the x-coordinate second.

atan2 is passed separate x and y arguments, and atan is passed the ratio of those two arguments.

Because atan2 is a static method of Math, you always use it as Math.atan2(), rather than as a method of a Math object you created.

示例

The following function returns the angle of the polar coordinate:

function getAtan2(x,y) {
   return Math.atan2(x,y)
}
If you pass getAtan2 the values (90,15), it returns 1.4056476493802699; if you pass it the values (15,90), it returns 0.16514867741462683.

参看

Math.acos, Math.asin, Math.atan, Math.cos, Math.sin, Math.tan

ceil

Returns the smallest integer greater than or equal to a number.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

ceil(x)

参数

xA number

描述

Because ceil is a static method of Math, you always use it as Math.ceil(), rather than as a method of a Math object you created.

示例

The following function returns the ceil value of the variable x:

function getCeil(x) {
   return Math.ceil(x)
}
If you pass 45.95 to getCeil, it returns 46; if you pass -45.95, it returns -45.

参看

Math.floor

cos

Returns the cosine of a number.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

cos(x)

参数

xA number

描述

The cos method returns a numeric value between -1 and 1, which represents the cosine of the angle.

Because cos is a static method of Math, you always use it as Math.cos(), rather than as a method of a Math object you created.

示例

The following function returns the cosine of the variable x:

function getCos(x) {
   return Math.cos(x)
}
If x equals Math.PI/2, getCos returns 6.123031769111886e-017; if x equals Math.PI, getCos returns -1.

参看

Math.acos, Math.asin, Math.atan, Math.atan2, Math.sin, Math.tan

exp

Returns Ex, where x is the argument, and E is Euler's constant, the base of the natural logarithms.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

exp(x)

参数

xA number

描述

Because exp is a static method of Math, you always use it as Math.exp(), rather than as a method of a Math object you created.

示例

The following function returns the exponential value of the variable x:

function getExp(x) {
   return Math.exp(x)
}
If you pass getExp the value 1, it returns 2.718281828459045.

参看

Math.E, Math.log, Math.pow

floor

Returns the largest integer less than or equal to a number.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

floor(x)

参数

xA number

描述

Because floor is a static method of Math, you always use it as Math.floor(), rather than as a method of a Math object you created.

示例

The following function returns the floor value of the variable x:

function getFloor(x) {
   return Math.floor(x)
}
If you pass 45.95 to getFloor, it returns 45; if you pass -45.95, it returns -46.

参看

Math.ceil

log

Returns the natural logarithm (base E) of a number.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

log(x)

参数

xA number

描述

If the value of number is outside the suggested range, the return value is always -1.797693134862316e+308.

Because log is a static method of Math, you always use it as Math.log(), rather than as a method of a Math object you created.

示例

The following function returns the natural log of the variable x:

function getLog(x) {
   return Math.log(x)
}
If you pass getLog the value 10, it returns 2.302585092994046; if you pass it the value 0, it returns -1.797693134862316e+308 because 0 is out of range.

参看

Math.exp, Math.pow

max

Returns the larger of two numbers.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

max(x,y)

参数

x, yNumbers.

描述

Because max is a static method of Math, you always use it as Math.max(), rather than as a method of a Math object you created.

示例

The following function evaluates the variables x and y:

function getMax(x,y) {
   return Math.max(x,y)
}
If you pass getMax the values 10 and 20, it returns 20; if you pass it the values -10 and -20, it returns -10.

参看

Math.min

min

Returns the smaller of two numbers.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

min(x,y)

参数

x, yNumbers.

描述

Because min is a static method of Math, you always use it as Math.min(), rather than as a method of a Math object you created.

示例

The following function evaluates the variables x and y:

function getMin(x,y) {
   return Math.min(x,y)
}
If you pass getMin the values 10 and 20, it returns 10; if you pass it the values -10 and -20, it returns -20.

参看

Math.max

pow

Returns base to the exponent power, that is, baseexponent.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

pow(x,y)

参数

baseThe base number
exponentThe exponent to which to raise base

描述

Because pow is a static method of Math, you always use it as Math.pow(), rather than as a method of a Math object you created.

示例

function raisePower(x,y) {
   return Math.pow(x,y)
}
If x is 7 and y is 2, raisePower returns 49 (7 to the power of 2).

参看

Math.exp, Math.log

random

Returns a pseudo-random number between 0 and 1. The random number generator is seeded from the current time, as in Java.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0: Unix only
Navigator 3.0, LiveWire 1.0: all platforms

语法

random()

参数

无。

描述

Because random is a static method of Math, you always use it as Math.random(), rather than as a method of a Math object you created.

示例

//Returns a random number between 0 and 1
function getRandom() {
   return Math.random()
}

round

Returns the value of a number rounded to the nearest integer.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

round(x)

参数

xA number

描述

If the fractional portion of number is .5 or greater, the argument is rounded to the next highest integer. If the fractional portion of number is less than .5, the argument is rounded to the next lowest integer.

Because round is a static method of Math, you always use it as Math.round(), rather than as a method of a Math object you created.

示例

//Displays the value 20
document.write("The rounded value is " + Math.round(20.49))
//Displays the value 21
document.write("<P>The rounded value is " + Math.round(20.5))
//Displays the value -20
document.write("<P>The rounded value is " + Math.round(-20.5))
//Displays the value -21
document.write("<P>The rounded value is " + Math.round(-20.51))
In server-side JavaScript, you can display the same output by calling the write function instead of using document.write.

sin

Returns the sine of a number.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

sin(x)

参数

xA number

描述

The sin method returns a numeric value between -1 and 1, which represents the sine of the argument.

Because sin is a static method of Math, you always use it as Math.sin(), rather than as a method of a Math object you created.

示例

The following function returns the sine of the variable x:

function getSine(x) {
   return Math.sin(x)
}
If you pass getSine the value Math.PI/2, it returns 1.

参看

Math.acos, Math.asin, Math.atan, Math.atan2, Math.cos, Math.tan

sqrt

Returns the square root of a number.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

sqrt(x)

参数

xA number

描述

If the value of number is outside the required range, sqrt returns 0.

Because sqrt is a static method of Math, you always use it as Math.sqrt(), rather than as a method of a Math object you created.

示例

The following function returns the square root of the variable x:

function getRoot(x) {
   return Math.sqrt(x)
}
If you pass getRoot the value 9, it returns 3; if you pass it the value 2, it returns 1.414213562373095.

tan

Returns the tangent of a number.

方法源Math
静态
实现版本Navigator 2.0, LiveWire 1.0

语法

tan(x)

参数

xA number

描述

The tan method returns a numeric value that represents the tangent of the angle.

Because tan is a static method of Math, you always use it as Math.tan(), rather than as a method of a Math object you created.

示例

The following function returns the tangent of the variable x:

function getTan(x) {
   return Math.tan(x)
}
If you pass Math.PI/4 to getTan, it returns 0.9999999999999999.

参看

Math.acos, Math.asin, Math.atan, Math.atan2, Math.cos, Math.sin


【目录】 【上一页】 【下一页】 【索引】

回页面顶部