JS-Math对象

来源:互联网 发布:有哪些域名交易网站 编辑:程序博客网 时间:2024/06/05 21:12

属性

1. E

代表算术常量 e,即自然对数的底数,其值近似于 2.71828

Math.E

document.write("Euler's number: " + Math.E);

2. LN2

是 loge2,即 2 的自然对数,其值近似于 0.69314718055994528623

Math.LN2

document.write("LN2: " + Math.LN2);

3. LN10

是 loge10,即 10 的自然对数,其值近似于 2.3025850929940459011

Math.LN10

document.write("LN10: " + Math.LN10);

4. LOG2E

是 log2e,即 2 为底 e 的对数,其值近似于 1.442695040888963387

Math.LOG2E

document.write("LOG2E: " + Math.LOG2E);

5. LOG10E

是 log10e,即 10 为底 e 的对数,其值近似于 0.43429448190325181667

Math.LOG10E

document.write("LOG10E: " + Math.LOG10E);

6. PI

是 π,即圆的周长和它的直径之比。这个值近似为 3.141592653589793

Math.PI

document.write("PI: " + Math.PI);

7. SQRT1_2

返回 2 的平方根的倒数。这个值近似为 0.7071067811865476

Math.SQRT1_2

document.write("SQRT1_2: " + Math.SQRT1_2);

8. SQRT2

返回 2 的平方根。这个值近似为 1.4142135623730951

Math.SQRT2

document.write("SQRT2: " + Math.SQRT2);

方法

1. abs

返回数的绝对值

返回值

x 的绝对值。

Math.abs(x)

参数

  • 必需
    • x 必须是一个数值。
document.write(Math.abs(7.25) + "<br />")document.write(Math.abs(-7.25) + "<br />")document.write(Math.abs(7.25-10))

2. acos

返回一个数的反余弦

返回值

x 的反余弦值。返回的值是 0 到 PI 之间的弧度值。

注释:如果参数 x 超过了 -1.0 ~ 1.0 的范围,那么浏览器将返回 NaN。

注释:如果参数 x 取值 -1,那么将返回 PI。

Math.acos(x)

参数

  • 必需
    • x 必须是 -1.0 ~ 1.0 之间的数。
document.write(Math.acos(0.64) + "<br />")document.write(Math.acos(0) + "<br />")document.write(Math.acos(-1) + "<br />")document.write(Math.acos(1) + "<br />")document.write(Math.acos(2))结果:    0.8762980611683406    1.5707963267948965    3.141592653589793    0    NaN

3. asin

返回一个数的反正弦值

返回值

x 的反正弦值。返回的值是 -PI/2 到 PI/2 之间的弧度值。

注释:如果参数 x 超过了 -1.0 ~ 1.0 的范围,那么浏览器将返回 NaN。

注释:如果参数 x 取值 -1,那么将返回 PI/2。

Math.asin(x)

参数

  • 必需
    • x 必须是 -1.0 ~ 1.0 之间的数。
document.write(Math.asin(0.64) + "<br />")document.write(Math.asin(0) + "<br />")document.write(Math.asin(-1) + "<br />")document.write(Math.asin(1) + "<br />")document.write(Math.asin(2))结果:    0.6944982656265559    0    -1.5707963267948965    1.5707963267948965    NaN

4. atan

返回数字的反正切值

返回值

x 的反正切值。返回的值是 -PI/2 到 PI/2 之间的弧度值。

Math.atan(x)

参数

  • 必需
    • x 必须是 -1.0 ~ 1.0 之间的数。
document.write(Math.atan(0.50) + "<br />")document.write(Math.atan(-0.50) + "<br />")document.write(Math.atan(5) + "<br />")document.write(Math.atan(10) + "<br />")document.write(Math.atan(-5) + "<br />")document.write(Math.atan(-10))结果:    0.4636476090008061    -0.4636476090008061    1.373400766945016    1.4711276743037347    -1.373400766945016    -1.4711276743037347

5. atan2

返回从 x 轴到点 (x,y) 之间的角度

返回值

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

注释:请注意这个函数的参数顺序,Y 坐标在 X 坐标之前传递。

Math.atan2(x,y)

参数

  • 必需
    • x 指定点的 X 坐标。
    • y 指定点的 Y 坐标。
document.write(Math.atan2(0.50,0.50) + "<br />")document.write(Math.atan2(-0.50,-0.50) + "<br />")document.write(Math.atan2(5,5) + "<br />")document.write(Math.atan2(10,20) + "<br />")document.write(Math.atan2(-5,-5) + "<br />")document.write(Math.atan2(-10,10))结果:    0.7853981633974483    -2.356194490192345    0.7853981633974483    0.4636476090008061    -2.356194490192345    -0.7853981633974483

6. ceil

可对一个数进行上舍入

返回值

大于等于 x,并且与它最接近的整数。

说明

ceil() 方法执行的是向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数。

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

7. cos

返回一个数字的余弦值

返回值

x 的余弦值。返回的是 -1.0 到 1.0 之间的数。

Math.cos(x)

参数

  • 必需
    • x 必须是一个数值。
document.write(Math.cos(3) + "<br />")document.write(Math.cos(-3) + "<br />")document.write(Math.cos(0) + "<br />")document.write(Math.cos(Math.PI) + "<br />")document.write(Math.cos(2*Math.PI))结果:    -0.9899924966004454    -0.9899924966004454    1    -1    1

8. exp

返回 e 的 x 次幂的值

返回值

返回 e 的 x 次幂。e 代表自然对数的底数,其值近似为 2.71828。

Math.exp(x)

参数

  • 必需
    • x 任意数值或表达式。被用作指数。
document.write(Math.exp(1) + "<br />")document.write(Math.exp(-1) + "<br />")document.write(Math.exp(5) + "<br />")document.write(Math.exp(10) + "<br />")结果:    2.718281828459045    0.36787944117144233    148.4131591025766    22026.465794806718

9. floor

对一个数进行下舍入

返回值

小于等于 x,且与 x 最接近的整数。

说明

floor() 方法执行的是向下取整计算,它返回的是小于或等于函数参数,并且与之最接近的整数。

Math.floor(x)

参数

  • 必需
    • 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

10. log

返回一个数的自然对数

返回值

x 的自然对数。

说明

参数 x 必须大于 0。

Math.log(x)

参数

  • 必需
    • x 任意数值或表达式。
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

11. max

返回两个指定的数中带有较大的值的那个数

返回值

参数中最大的值。如果没有参数,则返回 -Infinity。如果有某个参数为 NaN,或是不能转换成数字的非数字值,则返回 NaN。

Math.max(x...)

参数

x 0 或多个值。在 ECMASCript v3 之前,该方法只有两个参数。

document.write(Math.max(5,7) + "<br />")document.write(Math.max(-3,5) + "<br />")document.write(Math.max(-3,-5) + "<br />")document.write(Math.max(7.25,7.30))结果:    7    5    -3    7.3

12. min

返回指定的数字中带有最低值的数字

返回值

参数中最小的值。如果没有参数,则返回 Infinity。如果有某个参数为 NaN,或是不能转换成数字的非数字值,则返回 NaN。

Math.min(x...)

参数

x 0 或多个值。在 ECMASCript v3 之前,该方法只有两个参数。

document.write(Math.min(5,7) + "<br />")document.write(Math.min(-3,5) + "<br />")document.write(Math.min(-3,-5) + "<br />")document.write(Math.min(7.25,7.30))结果:    5    -3    -5    7.25

13. pow

可返回 x 的 y 次幂的值

返回值

x 的 y 次幂。

说明

如果结果是虚数或负数,则该方法将返回 NaN。如果由于指数过大而引起浮点溢出,则该方法将返回 Infinity。

Math.pow(x,y)

参数

  • 必需
    • x 底数。必须是数字。
    • y 幂数。必须是数字。
document.write(Math.pow(0,0) + "<br />")document.write(Math.pow(0,1) + "<br />")document.write(Math.pow(1,1) + "<br />")document.write(Math.pow(1,10) + "<br />")document.write(Math.pow(2,3) + "<br />")document.write(Math.pow(-2,3) + "<br />")document.write(Math.pow(2,4) + "<br />")document.write(Math.pow(-2,4) + "<br />")结果:    1    0    1    1    8    -8    16    16

14. random

可返回介于 0 ~ 1 之间的一个随机数

返回值

0.0 ~ 1.0 之间的一个伪随机数。

Math.random()

document.write(Math.random())

15. round

可把一个数字舍入为最接近的整数

返回值

与 x 最接近的整数。

说明

对于 0.5,该方法将进行上舍入。
例如,3.5 将舍入为 4,而 -3.5 将舍入为 -3。

Math.round(x)

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

16. sin

返回一个数字的正弦

返回值

参数 x 的正弦值。返回值在 -1.0 到 1.0 之间。

Math.sin(x)

参数

  • 必需
    • x 一个以弧度表示的角。将角度乘以 0.017453293 (2PI/360)即可转换为弧度。
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

17. sqrt

可返回一个数的平方根

返回值

参数 x 的平方根。如果 x 小于 0,则返回 NaN。

Math.sqrt(x)

参数

  • 必需
    • x 必须是大于等于 0 的数。
var a=Math.sqrt(0);var b=Math.sqrt(1);var c=Math.sqrt(9);var d=Math.sqrt(0.64);var e=Math.sqrt(-9);结果    0    1    3    0.8    NaN

18. tan

返回一个表示某个角的正切的数字

返回值

参数 x 的正切值。

Math.tan(x)

参数

  • 必需
    • x 一个以弧度表示的角。将角度乘以 0.017453293 (2PI/360)即可转换为弧度。
document.write(Math.tan(0.50) + "<br />")document.write(Math.tan(-0.50) + "<br />")document.write(Math.tan(5) + "<br />")document.write(Math.tan(10) + "<br />")document.write(Math.tan(-5) + "<br />")document.write(Math.tan(-10))结果    0.5463024898437905    -0.5463024898437905    -3.380515006246586    0.6483608274590866    3.380515006246586    -0.6483608274590866

文档内容出自 W3cSchool和菜鸟教程,
如需查看更详细的有关内容 请登录 http://www.w3school.com.cn/ 和 http://www.runoob.com/