javaScript内置对象Math

Math

<script>
var num=Math.random();
num=num*100;
document.write(num+"<br>");
document.write(Math.ceil(num)+"<br>");
document.write(Math.floor(num)+"<br>");
document.write(Math.round(num)+"<br>");
</script>
Math.random()取随机数0-1之间的随机数
Math.ceil()向上取整,大于或者等于参数
Math.floor()向下取整,小于或者等于参数
Math.round()四舍五入

属性:
E
算数常量e,自然对数的底数,约等2.718

例:
<script>
document.write("Euler's number: " + Math.E);
</script>
结果:Euler's number: 2.718281828459045


LN2
loge2,2的自然对数,约等于0.693

例:
<script>
document.write("LN2: " + Math.LN2);
</script>
结果:LN2: 0.6931471805599453

LN10
10的自然对数,loge10,约等2.302

例:
<script>
document.write("LN10: " + Math.LN10);
</script>
结果:LN10: 2.302585092994046

LOG2E
以2为底的e的对数,log2e,约等1.414

例:
<script>
document.write("LOG2E: " + Math.LOG2E);
</script>
结果:LOG2E: 1.4426950408889634

LOG10E
以10为底的e的对数,log10e约等于0.434

例:
<script>
document.write("LOG10E: " + Math.LOG10E);
</script>

PI
返回圆周率,约等3.14159

例:
<script>
document.write("PI: " + Math.PI);
</script>
结果:PI: 3.141592653589793

SQRT1_2
返回2的平方根的倒数,约等0.707

例:
<script>
document.write("SQRT1_2: " + Math.SQRT1_2);
</script>
结果:SQRT1_2: 0.7071067811865476

SQRT2
返回2的平方根,约等1.414

例:
<script>
document.write("SQRT2: " + Math.SQRT2);
</script>
结果:SQRT2: 1.4142135623730951

方法:
abs(X)
返回数的绝对值

例:
<script>
document.write(Math.abs(7.25) + "<br />")
document.write(Math.abs(-7.25) + "<br />")
document.write(Math.abs(7.25-10))
</script>
结果:7.25 7.25 2.75

acos(X)
返回数的反余弦值

例:
<script>
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))
</script>
结果:0.8762980611683406  1.5707963267948965  3.141592653589793  0  NaN

asin(x)
返回数的反正弦值

例:
<script>
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))
</script>
结果:0.6944982656265559  0  -1.5707963267948965  1.5707963267948965  NaN

atan(x)
介于-PI/2与PI/2弧度之间的数值来返回x的反正切值

例:
<script>
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))
</script>
结果:0.4636476090008061  -0.4636476090008061  1.373400766945016  1.4711276743037347  -1.373400766945016    1.4711276743037347

atan2(y,x)
返回从x轴到点(x,y)的角度,介于介于-PI/2与PI/2弧度之间

例:
<script>
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))
</script>
结果:0.7853981633974483  -2.356194490192345  0.7853981633974483  0.4636476090008061  -2.356194490192345  -0.7853981633974483

ceil(x)
对数进行上舍入

例:
<script>
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))
</script>
结果:1  1  5  6  -5  -5

cos(x)
返回余弦

例:
<script>
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))
</script>
结果:-0.9899924966004454  -0.9899924966004454  1  -1  1

exp(x)
返回e的指数,近似2.71828

例:
<script>
document.write(Math.exp(1) + "<br />")
document.write(Math.exp(-1) + "<br />")
document.write(Math.exp(5) + "<br />")
document.write(Math.exp(10) + "<br />")
</script>
结果:2.718281828459045  0.36787944117144233  148.4131591025766  22026.465794806718

floor(x)
对数进行下舍入

例:
<script>
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))
</script>
结果:0  0  5  5  -6  -6

log(x)
返回自然对数,e为底

例:
<script>
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))
</script>
结果:1.0000066849139877  0.6931471805599453  0  -Infinity  NaN

max(x,y)
返回x和y中最高值

例:
<script>
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))
</script>
结果:7  5  -3  7.3

min(x,y)
返回x和y中最低值

例:
<script>
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))
</script>
结果:5  -3  -5  7.25

pow(x,y)
返回x的y次幂

例:
<script>
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 />")
</script>
结果:1  0  1  1  8  -8  16  16

random()
返回0-1之间的随机数

例:
<script>
document.write(Math.random())
</script>
结果:0.4213156693924963

round(x)
把数四舍五入为最近的整数

例:
<script>
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))
</script>
结果:1  1  0  -4  -5

sin(x)
返回数的正弦

例:
<script>
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)
</script>
结果:
0.1411200080598672  -0.1411200080598672  0  1.2246063538223772e-16  1

sqrt(x)
返回数的平方根

例:
<script>
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);
</script>
结果:0  1  3  0.8  NaN

tan(x)
返回角的正切

例:
<script>
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))
</script>
结果:0.5463024898437905  -0.5463024898437905  -3.380515006246586  0.6483608274590866  3.380515006246586  -0.6483608274590866

toSource()
返回该对象的源代码

valueOf();
返回Math对象的原始值

猜你喜欢

转载自blog.csdn.net/abenazhan/article/details/77162687