LEARNING JAVASCRIPT - Trang 257

Logarithmic Functions

The basic natural logarithm function is

Math.log

. In some languages, “log” refers to

“log base 10” and “ln” refers to “natural logarithm,” so keep in mind that in JavaScript,

“log” means “natural logarithm.” ES6 introduced

Math.log10

for convenience.

Table 16-2. Logarithmic functions

Function

Description

Examples

Math.log(x)

Natural logarithm of x

Math

.

log

(

Math

.

E

)

// 1

Math

.

log

(

17.5

)

// ~2.86

Math.log10(x)

Base 10 logarithm of x
Equivalent to

Math.log(x)/

Math.log(10)

Math

.

log10

(

10

)

// 1

Math

.

log10

(

16.7

)

// ~1.22

Math.log2(x)

Base 2 logarithm of x
Equivalent to

Math.log(x)/

Math.log(2)

Math

.

log2

(

2

)

// 1

Math

.

log2

(

5

)

// ~2.32

Math.log1p(x)

Natural logarithm of 1 + x
Equivalent to

Math.log(1 + x)

Math

.

log1p

(

Math

.

E

-

1

)

// 1

Math

.

log1p

(

17.5

)

// ~2.92

Miscellaneous

Table 16-3

lists miscellaneous numeric functions that allow you to perform common

operations such as finding the absolute value, ceiling, floor, or sign of a number, as

well as finding the minimum or maximum number in a list.

Table 16-3. Number miscellaneous algebraic functions

Function

Description

Examples

Math.abs(x)

Absolute value of x

Math

.

abs

(

-

5.5

)

// 5.5

Math

.

abs

(

5.5

)

// 5.5

Math.sign(x)

The sign of x: if x is negative,
–1; if x is positive, 1; and if x
is 0, 0

Math

.

sign

(

-

10.5

)

// -1

Math

.

sign

(

6.77

)

// 1

Math.ceil(x)

The ceiling of x: the smallest
integer greater than or equal
to x

Math

.

ceil

(

2.2

)

// 3

Math

.

ceil

(

-

3.8

)

// -3

Math.floor(x)

The floor of x: the largest
integer less than or equal to x

Math

.

floor

(

2.8

)

// 2

Math

.

floor

(

-

3.2

)

// -4

Algebraic Functions | 233

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.