Home > Web Front-end > JS Tutorial > Javascript Math Object_Basic Knowledge

Javascript Math Object_Basic Knowledge

WBOY
Release: 2016-05-16 18:48:21
Original
991 people have browsed it

Math对象

Math对象是在高中数学课就学过的内置对象。它知道解决最复杂的数学问题的所有公式,如果给它要处理的数字,即能计算出结果。

Math对象有几个属性,主要是数学界的专用值。下表类出了这些属性:

属  性

说    明

E

值e,自然对数的底

LN10

10的自然对数

LN2

2的自然对数

LOG2E

以2为底E的对数

LOG10E

以10为底E的对数

PI

值π

SQRT1_2

1/2的平方根

SQRT2

2的平方根

虽然这些值的意义与用法不在本书讨论范围内,但如果清楚它们是什么,在需要时,即可使用它们。

Math对象还包括许多专门用于执行简单的及复杂的数学计算的方法。

方法min()和max()用于判断一组数中的最大值和最小值。这两个方法都可接受任意多个参数:

对于数字3、54、32和16,max()返回54,min()返回3。用这些方法,可免去用循环或if语句来判断一组数中的最大值。

另一个方法abs()返回数字的绝对值。绝对值是负数的正值版本(正数的绝对值就是它自身)。

这个例子中,abs(-1)返回1,abs(1)也返回1。

下一组方法用于把小数舍入成整数。处理舍入操作的方法有三个,即ceil()、floor()和round(),它们的处理方法不同:

q 方法ceil()表示向上舍入函数,总是把数字向上舍入到最接近的值。

q 方法floor()表示向下舍入函数,总是把数字向下舍入到最接近的值。

q 方法round()表示标准的舍入函数,如果数字与下一个整数的差不超过0.5,则向上舍入,否则向下舍入。这是在初中学过的舍入规则。

为说明每种方法的处理方式,考虑使用值25.5:

对于ceil()和round(),传递25.5,返回的是26,而floor()返回的是25。注意不要交替使用这些方法,因为最后可能得到与预期不符的结果。

另一组方法与指数的用法有关。这些方法包括exp(),用于把Math.E升到指定的幂;log()用于返回特定数字的自然对数;pow()用于把指定的数字升到指定的幂;sqrt()用于返回指定数字的平方根。

方法exp()和log()本质上功能相反,exp()把Math.E升到特定的幂,log()则判断Math.E的多少次指数才等于指定的值。例如:

这里,首先用exp()把Math.E升到10次幂,然后log()返回10,即等于数字iNum必需的指数。很多人都对此感到迷茫。全世界的高中生和数学系的大学生都被此类问题难倒过。如果你对自然对数一无所知,那么有可能永远都不需要为它编写代码。

方法pow()用于把数字升到指定的幂,如把2升到10次幂(在数学中表示为210):

pow()的第一个参数是基数,此例子中是2。第二个参数是要升到的幂,此例子中是10。

不建议把Math.E作为pow()方法的基数。最好使用exp()Math.E进行升幂运算,因为它是专用运算,计算出的值更精确。

The last method in this set of methods is sqrt(), which returns the square root of the specified number. It has only one parameter, the number whose square root is required. To ask for the square root of 4, you only need to use one line of code:

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

You may ask "Why does the square root have to use an exponent"? In fact, the square root of a number is its 1/2 power. For example, 21/2 is the square root of 2.

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

Fang method

方    法

说    明

acos(x)

返回x的反余弦值

asin(x)

返回x的反正弦值

atan(x)

返回x的反正切值

atan2(y,x)

返回y/x的反余弦值

cos(x)

返回x的余弦值

sin(x)

返回x的正弦值

tan(x)

返回x的正切值

Say Ming

acos(x)

Returns the arc cosine of x

asin(x)

Returns the arcsine of x

atan(x)

Returns the arctangent of x

atan2(y,x)

Returns the inverse cosine of y/x

cos(x)

Returns the cosine value of x

sin(x)

Returns the sine value of x

tan(x)

Returns the tangent value of x

Even though these methods are defined by ECMA-262, the results are implementation-dependent, as there are many ways to calculate each value, making the results generated by different implementations have different accuracy. The last method of the Math object is random(), which 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 their home page. You can call the random() method in the following form to select a random number within a certain range: The method floor() is used here because random() returns decimal values. That is to say, if you multiply it by a number and then add a number, the result will still be a decimal value. Usually you want to choose a random integer value. Therefore, the floor() method must be used. If you want to select a number between 1 and 10, the code is as follows: There are 10 possible values ​​(1 to 10), 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. Many times it's easier to use a function that counts the total number of options and the first available value: It’s easy to select random items in an Array using functions: Here, the second parameter of the selectFrom() function is the length of the array minus 1, which is the position of the last element in the array.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template