Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Math

From MDC

目录

[编辑] 摘要

核心对象

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.

[编辑] 创建

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)
}

[编辑] 属性

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

LN2: Natural logarithm of 2, approximately 0.693.

LN10: Natural logarithm of 10, approximately 2.302.

LOG2E: Base 2 logarithm of E, approximately 1.442.

LOG10E: Base 10 logarithm of E, approximately 0.434.

PI: Ratio of the circumference of a circle to its diameter, approximately 3.14159.

SQRT1_2: Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.

SQRT2: Square root of 2, approximately 1.414.

[编辑] 方法

abs: 返回数字的绝对值。

acos: Returns the arccosine (in radians) of a number.

asin: Returns the arcsine (in radians) of a number.

atan: Returns the arctangent (in radians) of a number.

atan2: Returns the arctangent of the quotient of its arguments.

ceil: 返回大或者等于参数的最小整数(进一法取整)。

cos: Returns the cosine of a number.

exp: Returns Enumber, where number is the argument, and E is Euler's constant, the base of the natural logarithms.

floor: 返回小于或者等于参数的整数(去尾法取整)。

log: 返回以E为低的对数

max: 返回参数列表中的最大数

min: 返回参数列表中的最小数。

pow: Returns base to the exponent power, that is, base exponent.

random: 返回0-1之间的一个伪随机数。

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

sin: 返回正弦值 of a number.

sqrt: 返回平方根

tan: Returns the tangent of a number.