LEARNING JAVASCRIPT - Trang 256

// logarithmic convenience constants -- these can be accessed through library
// calls, but they're commonly used enough to warrant convenience constants

Math

.

LN2

// the natural logarithm of 2: ~0.693

Math

.

LN10

// the natural logarithm of 10: ~2.303

Math

.

LOG2E

// the base 2 logarithm of Math.E: ~1.433

Math

.

LOG10E

// the base 10 logarithm of Math.E: 0.434

// algebraic convenience constants

Math

.

SQRT1_2

// the square root of 1/2: ~0.707

Math

.

SQRT2

// the square root of 2: ~1.414

Algebraic Functions

Exponentiation

The basic exponentiation function is

Math.pow

, and there are convenience functions

for square root, cube root, and powers of e, as shown in

Table 16-1

.

Table 16-1. Exponentiation functions

Function

Description

Examples

Math.pow(x, y)

x

y

Math

.

pow

(

2

,

3

)

// 8

Math

.

pow

(

1.7

,

2.3

)

// ~3.39

Math.sqrt(x)

x

Equivalent to

Math.pow(x, 0.5)

Math

.

sqrt

(

16

)

// 4

Math

.

sqrt

(

15.5

)

// ~3.94

Math.cbrt(x)

Cube root of x
Equivalent to

Math.pow(x,

1/3)

Math

.

cbrt

(

27

)

// 3

Math

.

cbrt

(

22

)

// ~2.8

Math.exp(x)

e

x

Equivalent to

Math.pow(Math.E, x)

Math

.

exp

(

1

)

// ~2.718

Math

.

exp

(

5.5

)

// ~244.7

Math.expm1(x)

e

x

− 1 Equivalent to

Math.exp(x) - 1

Math

.

expm1

(

1

)

// ~1.718

Math

.

expm1

(

5.5

)

// ~243.7

Math.hypot(x1, x2,...)

Square root of sum of arguments:

x1

2

+ x2

2

+ . . .

Math

.

hypot

(

3

,

4

)

// 5

Math

.

hypot

(

2

,

3

,

4

)

// ~5.36

232 | Chapter 16: Math