Javascript之Math对象详解

2022-10-18,,,

math对象不同于上述的对象,它可以说是一个公共数学类,里面有很多数学方法,用于各种数学运算
但是math对象不需要构造,对于其中的方法直接使用即可

1、常量(即属性)

e       返回算术常量 e,即自然对数的底数(约等于2.718)

e 返回算术常量 e,即自然对数的底数(约等于2.718)
ln2 返回 2 的自然对数(约等于0.693)
ln10 返回 10 的自然对数(约等于2.302)
log2e 返回以 2 为底的 e 的对数(约等于 1.414)
log10e 返回以 10 为底的 e 的对数(约等于0.434)
pi 返回圆周率(约等于3.14159)
sqrt1_2 返回返回 2 的平方根的倒数(约等于 0.707)
sqrt2 返回 2 的平方根(约等于 1.414)

下面是它们的值:

复制代码 代码如下:document.write("math.e = "+math.e+"<br>");
document.write("math.ln2 = "+math.ln2+"<br>");
document.write("math.ln10 = "+math.ln10+"<br>");
document.write("math.log2e = "+math.log2e+"<br>");
document.write("math.log10e = "+math.log10e+"<br>");
document.write("math.pi = "+math.pi+"<br>");
document.write("math.sqrt1_2 = "+math.sqrt1_2+"<br>");
document.write("math.sqrt2 = "+math.sqrt2+"<br>");

输出结果:

math.e = 2.718281828459045
math.ln2 = 0.6931471805599453
math.ln10 = 2.302585092994046
math.log2e = 1.4426950408889634
math.log10e = 0.4342944819032518
math.pi = 3.141592653589793
math.sqrt1_2 = 0.7071067811865476
math.sqrt2 = 1.4142135623730951

2、abs() 方法可返回数的绝对值

math.abs(x);x必须为一个数值,此数可以是整数,小数都可以
document.write(math.abs(-2.77));//输出2.77

3、acos(x) 返回数的反余弦值。

math.acos(x);x必须是 -1.0 ~ 1.0 之间的数
如果x不在上述范围,则返回nan

4、asin() 方法可返回一个数的反正弦值。

math.asin(x);x必须是一个数值,该值介于 -1.0 ~ 1.0 之间。
如果参数 x 超过了 -1.0 ~ 1.0 的范围,那么浏览器将返回 nan。

5、atan() 方法可返回数字的反正切值。

math.atan(x);x 必需。必须是一个数值。
返回的值是 -pi/2 到 pi/2 之间的弧度值。

6、atan2() 方法可返回从 x 轴到点 (x,y) 之间的角度。

math.atan2(y,x)
-pi 到 pi 之间的值,是从 x 轴正向逆时针旋转到点 (x,y) 时经过的角度。

7、ceil() 方法可对一个数进行上舍入。

什么是上舍入?即大于等于 x,并且与它最接近的整数。
math.ceil(x);x 必需。必须是一个数值。

复制代码 代码如下:document.write(math.ceil(0.60) + "<br />")
document.write(math.ceil(0.40) + "<br />")
document.write(math.ceil(5) + "<br />")
document.write(math.ceil(5.1) + "<br />")
document.write(math.ceil(-5.1) + "<br />")
document.write(math.ceil(-5.9))

输出为:

1
1
5
6
-5
-5

对于负数,你懂的

8、cos() 方法可返回一个数字的余弦值。

math.cos(x);x 必需。必须是一个数值。 返回的是 -1.0 到 1.0 之间的数。、
x其实要求是输入一个弧度值,例如--->
π代表的是180°等,π即math.pi
document.write(math.cos(math.pi));
输出为-1

但是假如:

复制代码 代码如下:document.write(math.cos(math.pi/2));
输出为:6.123233995736766e-17

而:

复制代码 代码如下:document.write(math.cos(math.pi/3));
输出为:0.5000000000000001

为什么会出现这些怪异的数字呢?

其实大家都知道document.write(math.cos(math.pi/2));应该输出0,而在javascript中可能没有求的0,所以就用了一个非常非常小的数代替
类似的document.write(math.cos(math.pi/3));应该是0.5才对,但是却在最后面多了一位
这些是小问题,没啥好说的,本身寄存器就不可能表示所有数的,因此在计算过程中出现差错也很正常

9、exp() 方法可返回 e 的 x 次幂的值。

math.exp(x);x 必需。任意数值或表达式。被用作指数。
返回 e 的 x 次幂。e 代表自然对数的底数,其值近似为 2.71828。
document.write(math.exp(1) + "<br />");//输出2.718281828459045

10、floor() 方法可对一个数进行下舍入。

和ceil()方法相对应,floor()方法是对一个数进行下舍入,即小于等于 x,且与 x 最接近的整数。
math.floor(x);

复制代码 代码如下:document.write(math.floor(0.60) + "<br />")
document.write(math.floor(0.40) + "<br />")
document.write(math.floor(5) + "<br />")
document.write(math.floor(5.1) + "<br />")
document.write(math.floor(-5.1) + "<br />")
document.write(math.floor(-5.9))

输出为:

0
0
5
5
-6
-6

对于负数,你懂的

11、log() 方法可返回一个数的自然对数。

math.log(x);//参数 x 必须大于 0,大于0则结果为nan,等于0则为-infinity

复制代码 代码如下:document.write(math.log(2.7183) + "<br />")
document.write(math.log(2) + "<br />")
document.write(math.log(1) + "<br />")
document.write(math.log(0) + "<br />")
document.write(math.log(-1))

输出为:

1.0000066849139877
0.6931471805599453
0
-infinity
nan
从上面我们可以看出

12、max() 方法可返回两个指定的数中带有较大的值的那个数。

math.max(x...),//x 为0或多个值。在 ecmascript v3 之前,该方法只有两个参数。
返回值:
参数中最大的值。
如果没有参数,则返回 -infinity。
如果有某个参数为 nan,或是不能转换成数字的非数字值,则返回 nan。
如下例:

复制代码 代码如下:document.write(math.max(5,3,8,1));//8
document.write(math.max(5,3,8,'m'));//nan
document.write(math.max(5));//5
document.write(math.max());//-infinity

13、min() 方法可返回指定的数字中带有最低值的数字。

math.min(x,y);x为0或多个值。在 ecmascript v3 之前,该方法只有两个参数。
返回值:
参数中最小的值。
如果没有参数,则返回 infinity。
如果有某个参数为 nan,或是不能转换成数字的非数字值,则返回 nan。
和max()方法使用类似

14、pow() 方法可返回 x 的 y 次幂的值。

math.pow(x,y);//
x 必需。底数。必须是数字。
y 必需。幂数。必须是数字。
返回值:
如果结果是虚数或负数,则该方法将返回 nan。如果由于指数过大而引起浮点溢出,则该方法将返回 infinity。
如下例:

复制代码 代码如下:document.write(math.pow()+'<br>');
document.write(math.pow(2)+'<br>');
document.write(math.pow(2,2)+'<br>');
document.write(math.pow(2,2,2)+'<br>');
document.write(math.pow('m',2)+'<br>');

输出:

nan
nan
4
4
nan

15、random() 方法可返回介于 0 ~ 1 之间的一个随机数。

math.random();//无参
返回:
0.0 ~ 1.0 之间的一个伪随机数。
何为伪随机数?
真正意义的随机数是某次随机事件产生的结果,经过无数次后表现为呈现某种概率论,它是不可预测的
而伪随机数是根据伪随机算法实现的,它是采用了一种模拟随机的算法,因此被称为伪随机数
复制代码 代码如下:document.write(math.random())
0.12645312909485157

16、round() 方法可把一个数字舍入为最接近的整数。

math.round(x),x 必需。必须是数字。
对于 0.5,该方法将进行上舍入。
例如,3.5 将舍入为 4,而 -3.5 将舍入为 -3。
其实就感觉此方法是用ceil()和floor()方法结合实现的

复制代码 代码如下:document.write(math.round(0.60) + "<br />")
document.write(math.round(0.50) + "<br />")
document.write(math.round(0.49) + "<br />")
document.write(math.round(-4.40) + "<br />")
document.write(math.round(-4.60))

输出为:

1
1
0
-4
-5

17、sin() 方法可返回一个数字的正弦。

math.sin(x),x 必需。一个以弧度表示的角。将角度乘以 0.017453293 (2pi/360)即可转换为弧度。
返回值:
参数 x 的正弦值。返回值在 -1.0 到 1.0 之间。
复制代码 代码如下:document.write(math.sin(3) + "<br />")
document.write(math.sin(-3) + "<br />")
document.write(math.sin(0) + "<br />")
document.write(math.sin(math.pi) + "<br />")
document.write(math.sin(math.pi/2)

输出为:

0.1411200080598672
-0.1411200080598672
0
1.2246063538223772e-16
1

18、sqrt() 方法可返回一个数的平方根。

math.sqrt(x);//x 必需,必须是大于等于 0 的数。
返回值:
参数 x 的平方根。如果 x 小于 0,则返回 nan。
它相当于math.pow(x,0.5);

19、tan() 方法可返回一个表示某个角的正切的数字。

math.tan(x),//x 必需。一个以弧度表示的角。将角度乘以 0.017453293 (2pi/360)即可转换为弧度。

查看更多javascript的语法,大家可以关注:《javascript 参考教程》、《javascript代码风格指南》,也希望大家多多支持。

《Javascript之Math对象详解.doc》

下载本文的Word格式文档,以方便收藏与打印。