Javascript math object

Source: Internet
Author: User
Tags natural logarithm

MathObject

Math objects are built-in objects learned in high school mathematics. It knows all the formulas used to solve the most complex mathematical problem. If it is given a number, it can calculate the result.

The math object has several attributes, mainly the special values of the mathematics field. The following table lists the attributes:

Genus

Description

E

Value E: The base of the natural logarithm.

Ln10

Natural logarithm of 10

Ln2

Natural logarithm of 2

Log2e

Base-2 logarithm of E

Log10e

Base-10 logarithm of E

Pi

Value π

Sqrt1_2

Square root of 1/2

Sqrt2

Square root of 2

Although the meaning and usage of these values are not discussed in this book, you can use them if you know what they are.

Math objects also include many methods specifically used to execute simple and complex mathematical calculations.

Methods min () and max () are used to determine the maximum and minimum values of a group of numbers. Both methods can accept any number of parameters:

For numbers 3, 54, 32, and 16, max () returns 54, min () returns 3. Using these methods, you do not need to use a loop or if statement to determine the maximum value of a group of numbers.

Another method ABS () returns the absolute value of a number. The absolute value is a positive version of a negative number (the absolute value of a positive number is itself ).

In this example, ABS (-1) returns 1, ABS (1) also returns 1.

The next method is used to round decimal places to integers. There are three methods for processing the rounding operation: Ceil (), floor (), and round (). They are processed differently:

The Q method Ceil () indicates the rounding up function, which always rounds up the number to the nearest value.

The Q method floor () indicates the downward rounding function. The number is always rounded down to the nearest value.

The Q method round () indicates the standard rounding function. If the difference between the number and the next integer does not exceed 0.5, It is rounded up. Otherwise, it is rounded down. This Is A Rounding Rule learned in junior high school.

To describe how each method is processed, consider using a value of 25.5:

For Ceil () and round (), if 25.5 is passed, 26 is returned, while floor () returns 25. Do not use these methods alternately, because the results may not match the expectation at the end.

Another set of methods is related to exponential usage. These methods include exp () for math. log () is used to return the natural logarithm of a specific number. Pow () is used to raise the specified number to the specified power. SQRT () is used to return the square root of a specified number.

Method exp () and log () are essentially opposite functions. exp () converts math. the EIS upgraded to a specific power. Log () Determines math. the number of times the e index is equal to the specified value. For example:

Here, we first use exp () to raise math. eto the 10 power, and then log () returns 10, which is equal to the required index of the number inum. Many people are confused about this. High school students and mathematics students all over the world are overwhelmed by such problems. If you have no idea about the logarithm of nature, you may never need to write it.Code.

The POW () method is used to raise a number to the specified power. For example, to raise 2 to the 10 power (expressed as 210 in mathematics ):

The first parameter of POW () is the base number. In this example, it is 2. The second parameter is the power to be raised. In this example, it is 10.

We do not recommendMath. eAsPow ()The base of the method. Best useExp ()PairMath. eThe power-up operation is performed because it is a dedicated operation, and the calculated value is more accurate.

The last method in this group is SQRT (), which is used to return the square root of a specified number. It has only one parameter, that is, the number that requires the square root. Requires the square root of 4. Only one line of code is required:

Of course, the square root of 4 is 2, which is the output of this line of code.

You may ask "Why must the square root use the index "? In fact, the square root of a number is its power of 1/2. For example, 21/2 is the square root of 2.

The math object also has a complete set of trigonometric functions. The following table lists these methods:

method

description

ACOs (x)

returns the arc cosine of x

asin (x)

returns the arc sine of x

atan (x)

returns the arc tangent of x

atan2 (Y, x)

returns the arc cosine of Y/x

CoS (x)

returns the cosine of x

sin (x)

returns the sine of x

Tan (x)

returns the tangent of x

Even if these methods are defined by the ECMA-262, the results are also determined by the implementation, because each value has many computational methods, so that different implementations generate results with Different accuracy.

The last method of the math object is random (). This method returns a random number between 0 and 1, excluding 0 and 1. This is a common tool for sites that display random quotes or news on the home page. You can call the random () method in the following format and select a random number within a certain range:

Here we use the method floor (), because random () returns a small value, that is, multiply it by a number, and then add a number to get a small value. Generally, you want to select a random integer. Therefore, you must use the floor () method. If you want to select a number ranging from 1 to 10, the Code is as follows:

There may be 10 values (1 to 10), and the first of these values is 1. If you want to select a value between 2 and 10, the Code is as follows:

From 2 to 10, there are only 9 numbers, so the total number of options is 9, where the first value is 2. In many cases, it is easier to use the function that calculates the total number of options and the first available value:

Using Functions, you can easily select random items in array:

Here, the second parameter of the selectfrom () function is the length of the array minus 1, that is, the position of the last element in the array.

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.