Details about attributes and methods of Math objects in js

Source: Internet
Author: User
Tags natural logarithm
Math is a built-in js object and does not need to be created, so it does not need to be instantiated. It only has static attributes and static method Math object attributes: 1. E attribute is approximately 2.718. 2. The LN2 attribute returns the natural logarithm of 2.

Math is a built-in js object. It does not need to be created, so it does not need to be instantiated. It only has static attributes and static methods.

Math object attributes:

1. The E attribute is approximately 2.718.

2. The LN2 attribute returns the natural logarithm of 2.

3. the PI property returns the ratio of the circumference of the circle to its diameter, which is approximately 3.141592653589793.

4. The LN10 attribute returns the natural logarithm of 10.

5. The LOG2E attribute returns the base-2 logarithm of e (the base of the natural logarithm.

6. The LOG10E attribute returns the base-10 logarithm of e (the base of the natural logarithm ).

7. The SQRT1_2 attribute returns the square root of 0.5, or one of the square root points of 2.

8. The SQRT2 property returns the square root of 2.

 

Math object method:

1. abs () returns the absolute value of the value.

Syntax example: Math. abs (num)

2. provides a set of trigonometric functions used to calculate numerical values.

Syntax example: Math. cos (num)

3. Math provides three methods for rounding a value.

(1). Math. ceil (num) returns the smallest integer greater than or equal to num.

(2). Math. floor (num) returns the maximum integer smaller than or equal to num.

(3). Math. round () rounds the decimal places of num to an integer.

4. random () generates random decimal places between 0 and 1

Syntax example: Math. random (num) can limit the range of Math. round (Math. random () * (n-m) + n) to return random integers from m to n.

5. max () returns the maximum number of multiple values.

Syntax example: Math. max (num1, num2 ,...)

6. min () returns the decimal number in the given value

Syntax example: Math. min (num1, num2 ,...)

7. pow (m, n) power operation m is a power operation base n is a power operation index

Syntax example: Math. pow (2, 3)

8. logarithm operation and square operation

Log (n) calculates the natural logarithm of n Based on e.

Sqrt (n) calculates the square root of n


DEMO code:
Script // math attributes with (document) {write ("Math. E =" + Math. E +"
"); Write (" Math. LN2 = "+ Math. LN2 +"
"); Write (" Math. PI = "+ Math. PI +"
");} // Math method with (document) {write (" Math. abs (-100) = "+ Math. abs (-100) +"
"); Write (" Math. cos (0) = "+ Math. cos (0) +"
"); Write (" Math. ceil (10.40) = "+ Math. ceil (10.40) +"
"); Write (" Math. floor (10.40) = "+ Math. floor (10.40) +"
"); Write (" Math. round (10.40) = "+ Math. round (10.50) +"
"); Write (" Math. random () = "+ Math. random () +"
"); Write (" Math. max (, 0) = "+ Math. max (, 0) +"
"); Write (" Math. min (, 0) = "+ Math. min (, 0) +"
"); Write (" Math. pow (2, 3) = "+ Math. pow (2, 3) +"
"); Write (" Math. log (2) = "+ Math. log (2) +"
"); Write (" Math. sqrt (2) = "+ Math. sqrt (2) +"
");} Script

The running result is as follows:
Math. E = 2.718281828459045
Math. LN2 = 0.6931471805599453
Math. PI = 3.141592653589793
Math. abs (-100) = 100
Math. cos (0) = 1
Math. ceil (10.40) = 11
Math. floor (10.40) = 10
Math. round (10.40) = 11
Math. random () = 0.5239217229778909
Math. max (1, 5, 0) = 5
Math. min (1, 5, 0) = 0
Math. pow (2, 3) = 8
Math. log (2) = 0.6931471805599453
Math. sqrt (2) = 1.4142135623730951
1231
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.